2 * VP9 compatible video decoder
4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
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
24 #include "libavutil/intreadwrite.h"
25 #include "libavcodec/get_bits.h"
28 typedef struct VP9ParseContext {
35 static int parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
37 VP9ParseContext *s = ctx->priv_data;
39 int res, profile, keyframe, invisible;
41 if ((res = init_get_bits8(&gb, buf, size)) < 0)
43 get_bits(&gb, 2); // frame marker
44 profile = get_bits1(&gb);
45 profile |= get_bits1(&gb) << 1;
46 if (profile == 3) profile += get_bits1(&gb);
52 keyframe = !get_bits1(&gb);
53 invisible = !get_bits1(&gb);
57 ctx->pict_type = AV_PICTURE_TYPE_P;
60 ctx->pict_type = AV_PICTURE_TYPE_I;
65 if (ctx->pts == AV_NOPTS_VALUE)
67 s->pts = AV_NOPTS_VALUE;
68 } else if (ctx->pts != AV_NOPTS_VALUE) {
70 ctx->pts = AV_NOPTS_VALUE;
76 static int parse(AVCodecParserContext *ctx,
77 AVCodecContext *avctx,
78 const uint8_t **out_data, int *out_size,
79 const uint8_t *data, int size)
81 VP9ParseContext *s = ctx->priv_data;
92 if (s->n_frames > 0) {
96 for (i = 0; i < s->n_frames ;i++)
97 size_sum += s->size[i];
98 size_sum += s->marker_size;
100 if (size_sum != size) {
101 av_log(avctx, AV_LOG_ERROR, "Inconsistent input frame sizes %d %d\n",
107 if (s->n_frames > 0) {
109 *out_size = s->size[--s->n_frames];
110 parse_frame(ctx, *out_data, *out_size);
112 return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
115 marker = data[size - 1];
116 if ((marker & 0xe0) == 0xc0) {
117 int nbytes = 1 + ((marker >> 3) & 0x3);
118 int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
120 if (size >= idx_sz && data[size - idx_sz] == marker) {
121 const uint8_t *idx = data + size + 1 - idx_sz;
125 #define case_n(a, rd) \
127 while (n_frames--) { \
130 if (sz == 0 || sz > size) { \
134 av_log(avctx, AV_LOG_ERROR, \
135 "Invalid superframe packet size: %u frame size: %d\n", \
143 s->n_frames = n_frames; \
145 s->size[n_frames] = sz; \
150 s->marker_size = size; \
151 parse_frame(ctx, *out_data, *out_size); \
152 return s->n_frames > 0 ? *out_size : full_size
155 case_n(2, AV_RL16(idx));
156 case_n(3, AV_RL24(idx));
157 case_n(4, AV_RL32(idx));
164 parse_frame(ctx, data, size);
169 AVCodecParser ff_vp9_parser = {
170 .codec_ids = { AV_CODEC_ID_VP9 },
171 .priv_data_size = sizeof(VP9ParseContext),
172 .parser_parse = parse,