2 * Copyright (C) 2008 Michael Niedermayer
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/intreadwrite.h"
24 typedef struct VP9ParseContext {
30 static void parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
32 VP9ParseContext *s = ctx->priv_data;
35 ctx->pict_type = AV_PICTURE_TYPE_P;
38 ctx->pict_type = AV_PICTURE_TYPE_I;
43 if (ctx->pts == AV_NOPTS_VALUE)
45 s->pts = AV_NOPTS_VALUE;
48 ctx->pts = AV_NOPTS_VALUE;
52 static int parse(AVCodecParserContext *ctx,
53 AVCodecContext *avctx,
54 const uint8_t **out_data, int *out_size,
55 const uint8_t *data, int size)
57 VP9ParseContext *s = ctx->priv_data;
68 if (s->n_frames > 0) {
70 *out_size = s->size[--s->n_frames];
71 parse_frame(ctx, *out_data, *out_size);
73 return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
76 marker = data[size - 1];
77 if ((marker & 0xe0) == 0xc0) {
78 int nbytes = 1 + ((marker >> 3) & 0x3);
79 int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
81 if (size >= idx_sz && data[size - idx_sz] == marker) {
82 const uint8_t *idx = data + size + 1 - idx_sz;
86 #define case_n(a, rd) \
88 while (n_frames--) { \
95 av_log(avctx, AV_LOG_ERROR, \
96 "Superframe packet size too big: %u > %d\n", \
104 s->n_frames = n_frames; \
106 s->size[n_frames] = sz; \
111 parse_frame(ctx, *out_data, *out_size); \
115 case_n(2, AV_RL16(idx));
116 case_n(3, AV_RL24(idx));
117 case_n(4, AV_RL32(idx));
124 parse_frame(ctx, data, size);
129 AVCodecParser ff_vp9_parser = {
130 .codec_ids = { AV_CODEC_ID_VP9 },
131 .priv_data_size = sizeof(VP9ParseContext),
132 .parser_parse = parse,