coda-mpeg4.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Coda multi-standard codec IP - MPEG-4 helper functions
  4. *
  5. * Copyright (C) 2019 Pengutronix, Philipp Zabel
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/videodev2.h>
  9. #include "coda.h"
  10. int coda_mpeg4_profile(int profile_idc)
  11. {
  12. switch (profile_idc) {
  13. case 0:
  14. return V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE;
  15. case 15:
  16. return V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE;
  17. case 2:
  18. return V4L2_MPEG_VIDEO_MPEG4_PROFILE_CORE;
  19. case 1:
  20. return V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE_SCALABLE;
  21. case 11:
  22. return V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY;
  23. default:
  24. return -EINVAL;
  25. }
  26. }
  27. int coda_mpeg4_level(int level_idc)
  28. {
  29. switch (level_idc) {
  30. case 0:
  31. return V4L2_MPEG_VIDEO_MPEG4_LEVEL_0;
  32. case 1:
  33. return V4L2_MPEG_VIDEO_MPEG4_LEVEL_1;
  34. case 2:
  35. return V4L2_MPEG_VIDEO_MPEG4_LEVEL_2;
  36. case 3:
  37. return V4L2_MPEG_VIDEO_MPEG4_LEVEL_3;
  38. case 4:
  39. return V4L2_MPEG_VIDEO_MPEG4_LEVEL_4;
  40. case 5:
  41. return V4L2_MPEG_VIDEO_MPEG4_LEVEL_5;
  42. default:
  43. return -EINVAL;
  44. }
  45. }
  46. /*
  47. * Check if the buffer starts with the MPEG-4 visual object sequence and visual
  48. * object headers, for example:
  49. *
  50. * 00 00 01 b0 f1
  51. * 00 00 01 b5 a9 13 00 00 01 00 00 00 01 20 08
  52. * d4 8d 88 00 f5 04 04 08 14 30 3f
  53. *
  54. * Returns the detected header size in bytes or 0.
  55. */
  56. u32 coda_mpeg4_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size)
  57. {
  58. static const u8 vos_start[4] = { 0x00, 0x00, 0x01, 0xb0 };
  59. static const union {
  60. u8 vo_start[4];
  61. u8 start_code_prefix[3];
  62. } u = { { 0x00, 0x00, 0x01, 0xb5 } };
  63. if (size < 30 ||
  64. memcmp(buf, vos_start, 4) != 0 ||
  65. memcmp(buf + 5, u.vo_start, 4) != 0)
  66. return 0;
  67. if (size == 30 ||
  68. (size >= 33 && memcmp(buf + 30, u.start_code_prefix, 3) == 0))
  69. return 30;
  70. if (size == 31 ||
  71. (size >= 34 && memcmp(buf + 31, u.start_code_prefix, 3) == 0))
  72. return 31;
  73. if (size == 32 ||
  74. (size >= 35 && memcmp(buf + 32, u.start_code_prefix, 3) == 0))
  75. return 32;
  76. return 0;
  77. }