coda-h264.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Coda multi-standard codec IP - H.264 helper functions
  3. *
  4. * Copyright (C) 2012 Vista Silicon S.L.
  5. * Javier Martin, <javier.martin@vista-silicon.com>
  6. * Xavier Duret
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/videodev2.h>
  16. #include <coda.h>
  17. static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
  18. static const u8 *coda_find_nal_header(const u8 *buf, const u8 *end)
  19. {
  20. u32 val = 0xffffffff;
  21. do {
  22. val = val << 8 | *buf++;
  23. if (buf >= end)
  24. return NULL;
  25. } while (val != 0x00000001);
  26. return buf;
  27. }
  28. int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb)
  29. {
  30. const u8 *buf = vb2_plane_vaddr(vb, 0);
  31. const u8 *end = buf + vb2_get_plane_payload(vb, 0);
  32. /* Find SPS header */
  33. do {
  34. buf = coda_find_nal_header(buf, end);
  35. if (!buf)
  36. return -EINVAL;
  37. } while ((*buf++ & 0x1f) != 0x7);
  38. ctx->params.h264_profile_idc = buf[0];
  39. ctx->params.h264_level_idc = buf[2];
  40. return 0;
  41. }
  42. int coda_h264_filler_nal(int size, char *p)
  43. {
  44. if (size < 6)
  45. return -EINVAL;
  46. p[0] = 0x00;
  47. p[1] = 0x00;
  48. p[2] = 0x00;
  49. p[3] = 0x01;
  50. p[4] = 0x0c;
  51. memset(p + 5, 0xff, size - 6);
  52. /* Add rbsp stop bit and trailing at the end */
  53. p[size - 1] = 0x80;
  54. return 0;
  55. }
  56. int coda_h264_padding(int size, char *p)
  57. {
  58. int nal_size;
  59. int diff;
  60. diff = size - (size & ~0x7);
  61. if (diff == 0)
  62. return 0;
  63. nal_size = coda_filler_size[diff];
  64. coda_h264_filler_nal(nal_size, p);
  65. return nal_size;
  66. }
  67. int coda_h264_profile(int profile_idc)
  68. {
  69. switch (profile_idc) {
  70. case 66: return V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
  71. case 77: return V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
  72. case 88: return V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED;
  73. case 100: return V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  74. default: return -EINVAL;
  75. }
  76. }
  77. int coda_h264_level(int level_idc)
  78. {
  79. switch (level_idc) {
  80. case 10: return V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
  81. case 9: return V4L2_MPEG_VIDEO_H264_LEVEL_1B;
  82. case 11: return V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
  83. case 12: return V4L2_MPEG_VIDEO_H264_LEVEL_1_2;
  84. case 13: return V4L2_MPEG_VIDEO_H264_LEVEL_1_3;
  85. case 20: return V4L2_MPEG_VIDEO_H264_LEVEL_2_0;
  86. case 21: return V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
  87. case 22: return V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
  88. case 30: return V4L2_MPEG_VIDEO_H264_LEVEL_3_0;
  89. case 31: return V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
  90. case 32: return V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
  91. case 40: return V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  92. case 41: return V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
  93. default: return -EINVAL;
  94. }
  95. }