18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Coda multi-standard codec IP - MPEG-2 helper functions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2019 Pengutronix, Philipp Zabel 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/videodev2.h> 108c2ecf20Sopenharmony_ci#include "coda.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ciint coda_mpeg2_profile(int profile_idc) 138c2ecf20Sopenharmony_ci{ 148c2ecf20Sopenharmony_ci switch (profile_idc) { 158c2ecf20Sopenharmony_ci case 5: 168c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE; 178c2ecf20Sopenharmony_ci case 4: 188c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN; 198c2ecf20Sopenharmony_ci case 3: 208c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE; 218c2ecf20Sopenharmony_ci case 2: 228c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE; 238c2ecf20Sopenharmony_ci case 1: 248c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH; 258c2ecf20Sopenharmony_ci default: 268c2ecf20Sopenharmony_ci return -EINVAL; 278c2ecf20Sopenharmony_ci } 288c2ecf20Sopenharmony_ci} 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciint coda_mpeg2_level(int level_idc) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci switch (level_idc) { 338c2ecf20Sopenharmony_ci case 10: 348c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW; 358c2ecf20Sopenharmony_ci case 8: 368c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN; 378c2ecf20Sopenharmony_ci case 6: 388c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440; 398c2ecf20Sopenharmony_ci case 4: 408c2ecf20Sopenharmony_ci return V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH; 418c2ecf20Sopenharmony_ci default: 428c2ecf20Sopenharmony_ci return -EINVAL; 438c2ecf20Sopenharmony_ci } 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* 478c2ecf20Sopenharmony_ci * Check if the buffer starts with the MPEG-2 sequence header (with or without 488c2ecf20Sopenharmony_ci * quantization matrix) and extension header, for example: 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * 00 00 01 b3 2d 01 e0 34 08 8b a3 81 518c2ecf20Sopenharmony_ci * 10 11 11 12 12 12 13 13 13 13 14 14 14 14 14 15 528c2ecf20Sopenharmony_ci * 15 15 15 15 15 16 16 16 16 16 16 16 17 17 17 17 538c2ecf20Sopenharmony_ci * 17 17 17 17 18 18 18 19 18 18 18 19 1a 1a 1a 1a 548c2ecf20Sopenharmony_ci * 19 1b 1b 1b 1b 1b 1c 1c 1c 1c 1e 1e 1e 1f 1f 21 558c2ecf20Sopenharmony_ci * 00 00 01 b5 14 8a 00 01 00 00 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * or: 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * 00 00 01 b3 08 00 40 15 ff ff e0 28 608c2ecf20Sopenharmony_ci * 00 00 01 b5 14 8a 00 01 00 00 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * Returns the detected header size in bytes or 0. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ciu32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci static const u8 sequence_header_start[4] = { 0x00, 0x00, 0x01, 0xb3 }; 678c2ecf20Sopenharmony_ci static const union { 688c2ecf20Sopenharmony_ci u8 extension_start[4]; 698c2ecf20Sopenharmony_ci u8 start_code_prefix[3]; 708c2ecf20Sopenharmony_ci } u = { { 0x00, 0x00, 0x01, 0xb5 } }; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci if (size < 22 || 738c2ecf20Sopenharmony_ci memcmp(buf, sequence_header_start, 4) != 0) 748c2ecf20Sopenharmony_ci return 0; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if ((size == 22 || 778c2ecf20Sopenharmony_ci (size >= 25 && memcmp(buf + 22, u.start_code_prefix, 3) == 0)) && 788c2ecf20Sopenharmony_ci memcmp(buf + 12, u.extension_start, 4) == 0) 798c2ecf20Sopenharmony_ci return 22; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if ((size == 86 || 828c2ecf20Sopenharmony_ci (size > 89 && memcmp(buf + 86, u.start_code_prefix, 3) == 0)) && 838c2ecf20Sopenharmony_ci memcmp(buf + 76, u.extension_start, 4) == 0) 848c2ecf20Sopenharmony_ci return 86; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci} 88