1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Coda multi-standard codec IP - MPEG-2 helper functions
4 *
5 * Copyright (C) 2019 Pengutronix, Philipp Zabel
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/videodev2.h>
10 #include "coda.h"
11
coda_mpeg2_profile(int profile_idc)12 int coda_mpeg2_profile(int profile_idc)
13 {
14 switch (profile_idc) {
15 case 5:
16 return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE;
17 case 4:
18 return V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN;
19 case 3:
20 return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE;
21 case 2:
22 return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE;
23 case 1:
24 return V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH;
25 default:
26 return -EINVAL;
27 }
28 }
29
coda_mpeg2_level(int level_idc)30 int coda_mpeg2_level(int level_idc)
31 {
32 switch (level_idc) {
33 case 10:
34 return V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW;
35 case 8:
36 return V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN;
37 case 6:
38 return V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440;
39 case 4:
40 return V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH;
41 default:
42 return -EINVAL;
43 }
44 }
45
46 /*
47 * Check if the buffer starts with the MPEG-2 sequence header (with or without
48 * quantization matrix) and extension header, for example:
49 *
50 * 00 00 01 b3 2d 01 e0 34 08 8b a3 81
51 * 10 11 11 12 12 12 13 13 13 13 14 14 14 14 14 15
52 * 15 15 15 15 15 16 16 16 16 16 16 16 17 17 17 17
53 * 17 17 17 17 18 18 18 19 18 18 18 19 1a 1a 1a 1a
54 * 19 1b 1b 1b 1b 1b 1c 1c 1c 1c 1e 1e 1e 1f 1f 21
55 * 00 00 01 b5 14 8a 00 01 00 00
56 *
57 * or:
58 *
59 * 00 00 01 b3 08 00 40 15 ff ff e0 28
60 * 00 00 01 b5 14 8a 00 01 00 00
61 *
62 * Returns the detected header size in bytes or 0.
63 */
coda_mpeg2_parse_headers(struct coda_ctx * ctx,u8 * buf,u32 size)64 u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size)
65 {
66 static const u8 sequence_header_start[4] = { 0x00, 0x00, 0x01, 0xb3 };
67 static const union {
68 u8 extension_start[4];
69 u8 start_code_prefix[3];
70 } u = { { 0x00, 0x00, 0x01, 0xb5 } };
71
72 if (size < 22 ||
73 memcmp(buf, sequence_header_start, 4) != 0)
74 return 0;
75
76 if ((size == 22 ||
77 (size >= 25 && memcmp(buf + 22, u.start_code_prefix, 3) == 0)) &&
78 memcmp(buf + 12, u.extension_start, 4) == 0)
79 return 22;
80
81 if ((size == 86 ||
82 (size > 89 && memcmp(buf + 86, u.start_code_prefix, 3) == 0)) &&
83 memcmp(buf + 76, u.extension_start, 4) == 0)
84 return 86;
85
86 return 0;
87 }
88