2 * AVS3-P2/IEEE1857.10 video parser.
3 * Copyright (c) 2020 Zhenyu Wang <wangzhenyu@pkusz.edu.cn>
4 * Bingjie Han <hanbj@pkusz.edu.cn>
5 * Huiwen Ren <hwrenx@gmail.com>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 static int avs3_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
30 int pic_found = pc->frame_start_found;
31 uint32_t state = pc->state;
35 for (; cur < buf_size; ++cur) {
36 state = (state << 8) | buf[cur];
37 if (AVS3_ISPIC(buf[cur])){
48 for (; cur < buf_size; ++cur) {
49 state = (state << 8) | buf[cur];
50 if ((state & 0xFFFFFF00) == 0x100 && AVS3_ISUNIT(state & 0xFF)) {
51 pc->frame_start_found = 0;
58 pc->frame_start_found = pic_found;
64 static void parse_avs3_nal_units(AVCodecParserContext *s, const uint8_t *buf,
65 int buf_size, AVCodecContext *avctx)
71 if (buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1) {
72 if (buf[3] == AVS3_SEQ_START_CODE) {
74 int profile, ratecode;
76 init_get_bits(&gb, buf + 4, buf_size - 4);
79 s->pict_type = AV_PICTURE_TYPE_I;
81 profile = get_bits(&gb, 8);
82 // Skip bits: level(8)
94 if (profile == AVS3_PROFILE_BASELINE_MAIN10) {
95 int sample_precision = get_bits(&gb, 3);
96 if (sample_precision == 1) {
97 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
98 } else if (sample_precision == 2) {
99 avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
101 avctx->pix_fmt = AV_PIX_FMT_NONE;
105 // Skip bits: resv(1)
109 ratecode = get_bits(&gb, 4);
111 // Skip bits: resv(1)
117 avctx->has_b_frames = !get_bits(&gb, 1);
119 avctx->framerate.num = avctx->time_base.den = ff_avs3_frame_rate_tab[ratecode].num;
120 avctx->framerate.den = avctx->time_base.num = ff_avs3_frame_rate_tab[ratecode].den;
122 s->width = s->coded_width = avctx->width;
123 s->height = s->coded_height = avctx->height;
125 av_log(avctx, AV_LOG_DEBUG,
126 "AVS3 parse seq HDR: profile %d; coded size: %dx%d; frame rate code: %d\n",
127 profile, avctx->width, avctx->height, ratecode);
129 } else if (buf[3] == AVS3_INTRA_PIC_START_CODE) {
131 s->pict_type = AV_PICTURE_TYPE_I;
132 } else if (buf[3] == AVS3_INTER_PIC_START_CODE){
135 int pic_code_type = buf[8] & 0x3;
136 if (pic_code_type == 1 || pic_code_type == 3) {
137 s->pict_type = AV_PICTURE_TYPE_P;
139 s->pict_type = AV_PICTURE_TYPE_B;
147 static int avs3_parse(AVCodecParserContext *s, AVCodecContext *avctx,
148 const uint8_t **poutbuf, int *poutbuf_size,
149 const uint8_t *buf, int buf_size)
151 ParseContext *pc = s->priv_data;
154 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
157 next = avs3_find_frame_end(pc, buf, buf_size);
158 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
165 parse_avs3_nal_units(s, buf, buf_size, avctx);
168 *poutbuf_size = buf_size;
173 const AVCodecParser ff_avs3_parser = {
174 .codec_ids = { AV_CODEC_ID_AVS3 },
175 .priv_data_size = sizeof(ParseContext),
176 .parser_parse = avs3_parse,
177 .parser_close = ff_parse_close,