Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Coda multi-standard codec IP - MPEG-2 helper functions
0004  *
0005  * Copyright (C) 2019 Pengutronix, Philipp Zabel
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/videodev2.h>
0010 #include "coda.h"
0011 
0012 int coda_mpeg2_profile(int profile_idc)
0013 {
0014     switch (profile_idc) {
0015     case 5:
0016         return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SIMPLE;
0017     case 4:
0018         return V4L2_MPEG_VIDEO_MPEG2_PROFILE_MAIN;
0019     case 3:
0020         return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SNR_SCALABLE;
0021     case 2:
0022         return V4L2_MPEG_VIDEO_MPEG2_PROFILE_SPATIALLY_SCALABLE;
0023     case 1:
0024         return V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH;
0025     default:
0026         return -EINVAL;
0027     }
0028 }
0029 
0030 int coda_mpeg2_level(int level_idc)
0031 {
0032     switch (level_idc) {
0033     case 10:
0034         return V4L2_MPEG_VIDEO_MPEG2_LEVEL_LOW;
0035     case 8:
0036         return V4L2_MPEG_VIDEO_MPEG2_LEVEL_MAIN;
0037     case 6:
0038         return V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH_1440;
0039     case 4:
0040         return V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH;
0041     default:
0042         return -EINVAL;
0043     }
0044 }
0045 
0046 /*
0047  * Check if the buffer starts with the MPEG-2 sequence header (with or without
0048  * quantization matrix) and extension header, for example:
0049  *
0050  *   00 00 01 b3 2d 01 e0 34 08 8b a3 81
0051  *               10 11 11 12 12 12 13 13 13 13 14 14 14 14 14 15
0052  *               15 15 15 15 15 16 16 16 16 16 16 16 17 17 17 17
0053  *               17 17 17 17 18 18 18 19 18 18 18 19 1a 1a 1a 1a
0054  *               19 1b 1b 1b 1b 1b 1c 1c 1c 1c 1e 1e 1e 1f 1f 21
0055  *   00 00 01 b5 14 8a 00 01 00 00
0056  *
0057  * or:
0058  *
0059  *   00 00 01 b3 08 00 40 15 ff ff e0 28
0060  *   00 00 01 b5 14 8a 00 01 00 00
0061  *
0062  * Returns the detected header size in bytes or 0.
0063  */
0064 u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size)
0065 {
0066     static const u8 sequence_header_start[4] = { 0x00, 0x00, 0x01, 0xb3 };
0067     static const union {
0068         u8 extension_start[4];
0069         u8 start_code_prefix[3];
0070     } u = { { 0x00, 0x00, 0x01, 0xb5 } };
0071 
0072     if (size < 22 ||
0073         memcmp(buf, sequence_header_start, 4) != 0)
0074         return 0;
0075 
0076     if ((size == 22 ||
0077          (size >= 25 && memcmp(buf + 22, u.start_code_prefix, 3) == 0)) &&
0078         memcmp(buf + 12, u.extension_start, 4) == 0)
0079         return 22;
0080 
0081     if ((size == 86 ||
0082          (size > 89 && memcmp(buf + 86, u.start_code_prefix, 3) == 0)) &&
0083         memcmp(buf + 76, u.extension_start, 4) == 0)
0084         return 86;
0085 
0086     return 0;
0087 }