88 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * Coda multi-standard codec IP - MPEG-2 helper functions4 *5 * Copyright (C) 2019 Pengutronix, Philipp Zabel6 */7 8#include <linux/kernel.h>9#include <linux/videodev2.h>10#include "coda.h"11 12int 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 30int 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 without48 * quantization matrix) and extension header, for example:49 *50 * 00 00 01 b3 2d 01 e0 34 08 8b a3 8151 * 10 11 11 12 12 12 13 13 13 13 14 14 14 14 14 1552 * 15 15 15 15 15 16 16 16 16 16 16 16 17 17 17 1753 * 17 17 17 17 18 18 18 19 18 18 18 19 1a 1a 1a 1a54 * 19 1b 1b 1b 1b 1b 1c 1c 1c 1c 1e 1e 1e 1f 1f 2155 * 00 00 01 b5 14 8a 00 01 00 0056 *57 * or:58 *59 * 00 00 01 b3 08 00 40 15 ff ff e0 2860 * 00 00 01 b5 14 8a 00 01 00 0061 *62 * Returns the detected header size in bytes or 0.63 */64u32 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