2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/avassert.h"
20 #include "libavutil/intmath.h"
21 #include "libavutil/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
31 typedef struct VP9RawReorderFrame {
42 unsigned int show_existing_frame;
43 unsigned int frame_to_show;
45 unsigned int frame_type;
46 unsigned int show_frame;
47 unsigned int refresh_frame_flags;
50 typedef struct VP9RawReorderContext {
52 VP9RawReorderFrame *slot[FRAME_SLOTS];
53 VP9RawReorderFrame *next_frame;
54 } VP9RawReorderContext;
56 static void vp9_raw_reorder_frame_free(VP9RawReorderFrame **frame)
59 av_packet_free(&(*frame)->packet);
63 static void vp9_raw_reorder_clear_slot(VP9RawReorderContext *ctx, int s)
66 ctx->slot[s]->slots &= ~(1 << s);
67 if (ctx->slot[s]->slots == 0)
68 vp9_raw_reorder_frame_free(&ctx->slot[s]);
74 static int vp9_raw_reorder_frame_parse(AVBSFContext *bsf, VP9RawReorderFrame *frame)
79 unsigned int frame_marker;
80 unsigned int profile_low_bit, profile_high_bit, reserved_zero;
81 unsigned int error_resilient_mode;
82 unsigned int frame_sync_code;
84 err = init_get_bits(&bc, frame->packet->data, 8 * frame->packet->size);
88 frame_marker = get_bits(&bc, 2);
89 if (frame_marker != 2) {
90 av_log(bsf, AV_LOG_ERROR, "Invalid frame marker: %u.\n",
92 return AVERROR_INVALIDDATA;
95 profile_low_bit = get_bits1(&bc);
96 profile_high_bit = get_bits1(&bc);
97 frame->profile = (profile_high_bit << 1) | profile_low_bit;
98 if (frame->profile == 3) {
99 reserved_zero = get_bits1(&bc);
100 if (reserved_zero != 0) {
101 av_log(bsf, AV_LOG_ERROR, "Profile reserved_zero bit set: "
102 "unsupported profile or invalid bitstream.\n");
103 return AVERROR_INVALIDDATA;
107 frame->show_existing_frame = get_bits1(&bc);
108 if (frame->show_existing_frame) {
109 frame->frame_to_show = get_bits(&bc, 3);
113 frame->frame_type = get_bits1(&bc);
114 frame->show_frame = get_bits1(&bc);
115 error_resilient_mode = get_bits1(&bc);
117 if (frame->frame_type == 0) {
118 frame_sync_code = get_bits(&bc, 24);
119 if (frame_sync_code != 0x498342) {
120 av_log(bsf, AV_LOG_ERROR, "Invalid frame sync code: %06x.\n",
122 return AVERROR_INVALIDDATA;
124 frame->refresh_frame_flags = 0xff;
126 unsigned int intra_only;
128 if (frame->show_frame == 0)
129 intra_only = get_bits1(&bc);
132 if (error_resilient_mode == 0) {
133 // reset_frame_context
137 frame_sync_code = get_bits(&bc, 24);
138 if (frame_sync_code != 0x498342) {
139 av_log(bsf, AV_LOG_ERROR, "Invalid frame sync code: "
140 "%06x.\n", frame_sync_code);
141 return AVERROR_INVALIDDATA;
143 if (frame->profile > 0) {
144 unsigned int color_space;
145 if (frame->profile >= 2) {
149 color_space = get_bits(&bc, 3);
150 if (color_space != 7 /* CS_RGB */) {
153 if (frame->profile == 1 || frame->profile == 3) {
158 if (frame->profile == 1 || frame->profile == 3)
162 frame->refresh_frame_flags = get_bits(&bc, 8);
164 frame->refresh_frame_flags = get_bits(&bc, 8);
171 static int vp9_raw_reorder_make_output(AVBSFContext *bsf,
173 VP9RawReorderFrame *last_frame)
175 VP9RawReorderContext *ctx = bsf->priv_data;
176 VP9RawReorderFrame *next_output = last_frame,
177 *next_display = last_frame, *frame;
180 for (s = 0; s < FRAME_SLOTS; s++) {
181 frame = ctx->slot[s];
184 if (frame->needs_output && (!next_output ||
185 frame->sequence < next_output->sequence))
187 if (frame->needs_display && (!next_display ||
188 frame->pts < next_display->pts))
189 next_display = frame;
192 if (!next_output && !next_display)
195 if (!next_display || (next_output &&
196 next_output->sequence < next_display->sequence))
199 frame = next_display;
201 if (frame->needs_output && frame->needs_display &&
202 next_output == next_display) {
203 av_log(bsf, AV_LOG_DEBUG, "Output and display frame "
204 "%"PRId64" (%"PRId64") in order.\n",
205 frame->sequence, frame->pts);
207 av_packet_move_ref(out, frame->packet);
209 frame->needs_output = frame->needs_display = 0;
210 } else if (frame->needs_output) {
211 if (frame->needs_display) {
212 av_log(bsf, AV_LOG_DEBUG, "Output frame %"PRId64" "
213 "(%"PRId64") for later display.\n",
214 frame->sequence, frame->pts);
216 av_log(bsf, AV_LOG_DEBUG, "Output unshown frame "
217 "%"PRId64" (%"PRId64") to keep order.\n",
218 frame->sequence, frame->pts);
221 av_packet_move_ref(out, frame->packet);
224 frame->needs_output = 0;
228 av_assert0(!frame->needs_output && frame->needs_display);
230 if (frame->slots == 0) {
231 av_log(bsf, AV_LOG_ERROR, "Attempting to display frame "
232 "which is no longer available?\n");
233 frame->needs_display = 0;
234 return AVERROR_INVALIDDATA;
237 s = ff_ctz(frame->slots);
238 av_assert0(s < FRAME_SLOTS);
240 av_log(bsf, AV_LOG_DEBUG, "Display frame %"PRId64" "
241 "(%"PRId64") from slot %d.\n",
242 frame->sequence, frame->pts, s);
244 err = av_new_packet(out, 2);
248 init_put_bits(&pb, out->data, 2);
253 put_bits(&pb, 1, frame->profile & 1);
255 put_bits(&pb, 1, (frame->profile >> 1) & 1);
256 if (frame->profile == 3) {
260 // show_existing_frame
262 // frame_to_show_map_idx
265 while (put_bits_count(&pb) < 16)
269 out->pts = out->dts = frame->pts;
271 frame->needs_display = 0;
277 static int vp9_raw_reorder_filter(AVBSFContext *bsf, AVPacket *out)
279 VP9RawReorderContext *ctx = bsf->priv_data;
280 VP9RawReorderFrame *frame;
284 if (ctx->next_frame) {
285 frame = ctx->next_frame;
288 err = ff_bsf_get_packet(bsf, &in);
290 if (err == AVERROR_EOF)
291 return vp9_raw_reorder_make_output(bsf, out, NULL);
295 if (in->data[in->size - 1] & 0xe0 == 0xc0) {
296 av_log(bsf, AV_LOG_ERROR, "Input in superframes is not "
299 return AVERROR(ENOSYS);
302 frame = av_mallocz(sizeof(*frame));
305 return AVERROR(ENOMEM);
309 frame->pts = in->pts;
310 frame->sequence = ++ctx->sequence;
311 err = vp9_raw_reorder_frame_parse(bsf, frame);
313 av_log(bsf, AV_LOG_ERROR, "Failed to parse input "
314 "frame: %d.\n", err);
318 frame->needs_output = 1;
319 frame->needs_display = frame->pts != AV_NOPTS_VALUE;
321 if (frame->show_existing_frame)
322 av_log(bsf, AV_LOG_DEBUG, "Show frame %"PRId64" "
323 "(%"PRId64"): show %u.\n", frame->sequence,
324 frame->pts, frame->frame_to_show);
326 av_log(bsf, AV_LOG_DEBUG, "New frame %"PRId64" "
327 "(%"PRId64"): type %u show %u refresh %02x.\n",
328 frame->sequence, frame->pts, frame->frame_type,
329 frame->show_frame, frame->refresh_frame_flags);
331 ctx->next_frame = frame;
334 for (s = 0; s < FRAME_SLOTS; s++) {
335 if (!(frame->refresh_frame_flags & (1 << s)))
337 if (ctx->slot[s] && ctx->slot[s]->needs_display &&
338 ctx->slot[s]->slots == (1 << s)) {
339 // We are overwriting this slot, which is last reference
340 // to the frame previously present in it. In order to be
341 // a valid stream, that frame must already have been
342 // displayed before the pts of the current frame.
343 err = vp9_raw_reorder_make_output(bsf, out, ctx->slot[s]);
345 av_log(bsf, AV_LOG_ERROR, "Failed to create "
346 "output overwriting slot %d: %d.\n",
348 // Clear the slot anyway, so we don't end up
349 // in an infinite loop.
350 vp9_raw_reorder_clear_slot(ctx, s);
351 return AVERROR_INVALIDDATA;
355 vp9_raw_reorder_clear_slot(ctx, s);
358 for (s = 0; s < FRAME_SLOTS; s++) {
359 if (!(frame->refresh_frame_flags & (1 << s)))
361 ctx->slot[s] = frame;
363 frame->slots = frame->refresh_frame_flags;
365 if (!frame->refresh_frame_flags) {
366 err = vp9_raw_reorder_make_output(bsf, out, frame);
368 av_log(bsf, AV_LOG_ERROR, "Failed to create output "
369 "for transient frame.\n");
370 ctx->next_frame = NULL;
371 return AVERROR_INVALIDDATA;
373 if (!frame->needs_display) {
374 vp9_raw_reorder_frame_free(&frame);
375 ctx->next_frame = NULL;
380 ctx->next_frame = NULL;
381 return AVERROR(EAGAIN);
384 vp9_raw_reorder_frame_free(&frame);
388 static void vp9_raw_reorder_close(AVBSFContext *bsf)
390 VP9RawReorderContext *ctx = bsf->priv_data;
393 for (s = 0; s < FRAME_SLOTS; s++)
394 vp9_raw_reorder_clear_slot(ctx, s);
397 static const enum AVCodecID vp9_raw_reorder_codec_ids[] = {
398 AV_CODEC_ID_VP9, AV_CODEC_ID_NONE,
401 const AVBitStreamFilter ff_vp9_raw_reorder_bsf = {
402 .name = "vp9_raw_reorder",
403 .priv_data_size = sizeof(VP9RawReorderContext),
404 .close = &vp9_raw_reorder_close,
405 .filter = &vp9_raw_reorder_filter,
406 .codec_ids = vp9_raw_reorder_codec_ids,