/src/ffmpeg/libavcodec/avs3_parser.c
Line | Count | Source |
1 | | /* |
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> |
6 | | * |
7 | | * This file is part of FFmpeg. |
8 | | * |
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. |
13 | | * |
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. |
18 | | * |
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 |
22 | | */ |
23 | | |
24 | | #include "avs3.h" |
25 | | #include "get_bits.h" |
26 | | #include "parser.h" |
27 | | #include "parser_internal.h" |
28 | | |
29 | | static int avs3_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) |
30 | 3.55k | { |
31 | 3.55k | int pic_found = pc->frame_start_found; |
32 | 3.55k | uint32_t state = pc->state; |
33 | 3.55k | int cur = 0; |
34 | | |
35 | 3.55k | if (!pic_found) { |
36 | 1.21M | for (; cur < buf_size; ++cur) { |
37 | 1.20M | state = (state << 8) | buf[cur]; |
38 | 1.20M | if (AVS3_ISPIC(buf[cur])){ |
39 | 254 | cur++; |
40 | 254 | pic_found = 1; |
41 | 254 | break; |
42 | 254 | } |
43 | 1.20M | } |
44 | 1.46k | } |
45 | | |
46 | 3.55k | if (pic_found) { |
47 | 2.34k | if (!buf_size) |
48 | 56 | return END_NOT_FOUND; |
49 | 2.07M | for (; cur < buf_size; ++cur) { |
50 | 2.07M | state = (state << 8) | buf[cur]; |
51 | 2.07M | if ((state & 0xFFFFFF00) == 0x100 && AVS3_ISUNIT(state & 0xFF)) { |
52 | 226 | pc->frame_start_found = 0; |
53 | 226 | pc->state = -1; |
54 | 226 | return cur - 3; |
55 | 226 | } |
56 | 2.07M | } |
57 | 2.28k | } |
58 | | |
59 | 3.27k | pc->frame_start_found = pic_found; |
60 | 3.27k | pc->state = state; |
61 | | |
62 | 3.27k | return END_NOT_FOUND; |
63 | 3.55k | } |
64 | | |
65 | | static void parse_avs3_nal_units(AVCodecParserContext *s, const uint8_t *buf, |
66 | | int buf_size, AVCodecContext *avctx) |
67 | 314 | { |
68 | 314 | if (buf_size < 5) { |
69 | 46 | return; |
70 | 46 | } |
71 | | |
72 | 268 | if (buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1) { |
73 | 227 | if (buf[3] == AVS3_SEQ_START_CODE) { |
74 | 6 | GetBitContext gb; |
75 | 6 | int profile, ratecode, low_delay; |
76 | | |
77 | 6 | av_unused int ret = init_get_bits(&gb, buf + 4, 100); |
78 | 6 | av_assert1(ret >= 0); |
79 | | |
80 | 6 | s->key_frame = 1; |
81 | 6 | s->pict_type = AV_PICTURE_TYPE_I; |
82 | | |
83 | 6 | profile = get_bits(&gb, 8); |
84 | | // Skip bits: level(8) |
85 | | // progressive(1) |
86 | | // field(1) |
87 | | // library(2) |
88 | | // resv(1) |
89 | | // width(14) |
90 | | // resv(1) |
91 | | // height(14) |
92 | | // chroma(2) |
93 | | // sampe_precision(3) |
94 | 6 | skip_bits(&gb, 47); |
95 | | |
96 | 6 | if (profile == AVS3_PROFILE_BASELINE_MAIN10) { |
97 | 0 | int sample_precision = get_bits(&gb, 3); |
98 | 0 | if (sample_precision == 1) { |
99 | 0 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
100 | 0 | } else if (sample_precision == 2) { |
101 | 0 | avctx->pix_fmt = AV_PIX_FMT_YUV420P10; |
102 | 0 | } else { |
103 | 0 | avctx->pix_fmt = AV_PIX_FMT_NONE; |
104 | 0 | } |
105 | 0 | } |
106 | | |
107 | | // Skip bits: resv(1) |
108 | | // aspect(4) |
109 | 6 | skip_bits(&gb, 5); |
110 | | |
111 | 6 | ratecode = get_bits(&gb, 4); |
112 | | |
113 | | // Skip bits: resv(1) |
114 | | // bitrate_low(18) |
115 | | // resv(1) |
116 | | // bitrate_high(12) |
117 | 6 | skip_bits(&gb, 32); |
118 | | |
119 | 6 | low_delay = get_bits(&gb, 1); |
120 | 6 | avctx->has_b_frames = FFMAX(avctx->has_b_frames, !low_delay); |
121 | | |
122 | 6 | avctx->framerate.num = ff_avs3_frame_rate_tab[ratecode].num; |
123 | 6 | avctx->framerate.den = ff_avs3_frame_rate_tab[ratecode].den; |
124 | | |
125 | 6 | s->width = s->coded_width = avctx->width; |
126 | 6 | s->height = s->coded_height = avctx->height; |
127 | | |
128 | 6 | av_log(avctx, AV_LOG_DEBUG, |
129 | 6 | "AVS3 parse seq HDR: profile %d; coded size: %dx%d; frame rate code: %d\n", |
130 | 6 | profile, avctx->width, avctx->height, ratecode); |
131 | | |
132 | 221 | } else if (buf[3] == AVS3_INTRA_PIC_START_CODE) { |
133 | 93 | s->key_frame = 1; |
134 | 93 | s->pict_type = AV_PICTURE_TYPE_I; |
135 | 128 | } else if (buf[3] == AVS3_INTER_PIC_START_CODE){ |
136 | 127 | s->key_frame = 0; |
137 | 127 | if (buf_size > 9) { |
138 | 127 | int pic_code_type = buf[8] & 0x3; |
139 | 127 | if (pic_code_type == 1 || pic_code_type == 3) { |
140 | 6 | s->pict_type = AV_PICTURE_TYPE_P; |
141 | 121 | } else { |
142 | 121 | s->pict_type = AV_PICTURE_TYPE_B; |
143 | 121 | } |
144 | 127 | } |
145 | 127 | } |
146 | 227 | } |
147 | 268 | } |
148 | | |
149 | | |
150 | | static int avs3_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
151 | | const uint8_t **poutbuf, int *poutbuf_size, |
152 | | const uint8_t *buf, int buf_size) |
153 | 3.55k | { |
154 | 3.55k | ParseContext *pc = s->priv_data; |
155 | 3.55k | int next; |
156 | | |
157 | 3.55k | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
158 | 0 | next = buf_size; |
159 | 3.55k | } else { |
160 | 3.55k | next = avs3_find_frame_end(pc, buf, buf_size); |
161 | 3.55k | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
162 | 3.23k | *poutbuf = NULL; |
163 | 3.23k | *poutbuf_size = 0; |
164 | 3.23k | return buf_size; |
165 | 3.23k | } |
166 | 3.55k | } |
167 | | |
168 | 314 | parse_avs3_nal_units(s, buf, buf_size, avctx); |
169 | | |
170 | 314 | *poutbuf = buf; |
171 | 314 | *poutbuf_size = buf_size; |
172 | | |
173 | 314 | return next; |
174 | 3.55k | } |
175 | | |
176 | | const FFCodecParser ff_avs3_parser = { |
177 | | PARSER_CODEC_LIST(AV_CODEC_ID_AVS3), |
178 | | .priv_data_size = sizeof(ParseContext), |
179 | | .parse = avs3_parse, |
180 | | .close = ff_parse_close, |
181 | | }; |