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