Skip to content

Commit c4dc8cf

Browse files
committed
avcodec/vp9_parser: reimplement using CBS
Signed-off-by: James Almer <jamrial@gmail.com>
1 parent f058a9a commit c4dc8cf

File tree

3 files changed

+117
-33
lines changed

3 files changed

+117
-33
lines changed

libavcodec/cbs_vp9.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ typedef struct VP9ReferenceFrameState {
187187
int subsampling_x; // RefSubsamplingX
188188
int subsampling_y; // RefSubsamplingY
189189
int bit_depth; // RefBitDepth
190+
int intra_only;
190191
} VP9ReferenceFrameState;
191192

192193
typedef struct CodedBitstreamVP9Context {

libavcodec/cbs_vp9_syntax_template.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
379379
.subsampling_x = vp9->subsampling_x,
380380
.subsampling_y = vp9->subsampling_y,
381381
.bit_depth = vp9->bit_depth,
382+
.intra_only = current->intra_only,
382383
};
383384
}
384385
}

libavcodec/vp9_parser.c

Lines changed: 115 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
2-
* VP9 compatible video decoder
2+
* VP9 parser
33
*
4-
* Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5-
* Copyright (C) 2013 Clément Bœsch <u pkh me>
4+
* Copyright (C) 2018 James Almer <jamrial@gmail.com>
65
*
76
* This file is part of FFmpeg.
87
*
@@ -21,50 +20,133 @@
2120
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2221
*/
2322

24-
#include "libavutil/intreadwrite.h"
25-
#include "libavcodec/get_bits.h"
23+
#include "cbs.h"
24+
#include "cbs_vp9.h"
2625
#include "parser.h"
2726

28-
static int parse(AVCodecParserContext *ctx,
29-
AVCodecContext *avctx,
30-
const uint8_t **out_data, int *out_size,
31-
const uint8_t *data, int size)
27+
typedef struct VP9ParseContext {
28+
CodedBitstreamContext *cbc;
29+
CodedBitstreamFragment temporal_unit;
30+
} VP9ParseContext;
31+
32+
static const enum AVPixelFormat pix_fmts_8bit[2][2] = {
33+
{ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P },
34+
{ AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
35+
};
36+
static const enum AVPixelFormat pix_fmts_10bit[2][2] = {
37+
{ AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV440P },
38+
{ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10 },
39+
};
40+
static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
41+
{ AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P },
42+
{ AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 },
43+
};
44+
45+
static int vp9_parser_parse(AVCodecParserContext *ctx,
46+
AVCodecContext *avctx,
47+
const uint8_t **out_data, int *out_size,
48+
const uint8_t *data, int size)
3249
{
33-
GetBitContext gb;
34-
int res, profile, keyframe;
50+
VP9ParseContext *s = ctx->priv_data;
51+
CodedBitstreamFragment *td = &s->temporal_unit;
52+
CodedBitstreamVP9Context *vp9 = s->cbc->priv_data;
53+
int ret;
3554

3655
*out_data = data;
3756
*out_size = size;
3857

39-
if (!size || (res = init_get_bits8(&gb, data, size)) < 0)
40-
return size; // parsers can't return errors
41-
get_bits(&gb, 2); // frame marker
42-
profile = get_bits1(&gb);
43-
profile |= get_bits1(&gb) << 1;
44-
if (profile == 3) profile += get_bits1(&gb);
45-
if (profile > 3)
46-
return size;
47-
48-
avctx->profile = profile;
49-
50-
if (get_bits1(&gb)) {
51-
keyframe = 0;
52-
} else {
53-
keyframe = !get_bits1(&gb);
58+
ctx->key_frame = -1;
59+
ctx->pict_type = AV_PICTURE_TYPE_NONE;
60+
ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
61+
62+
s->cbc->log_ctx = avctx;
63+
64+
ret = ff_cbs_read(s->cbc, td, data, size);
65+
if (ret < 0) {
66+
av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n");
67+
goto end;
5468
}
69+
for (int i = 0; i < td->nb_units; i++) {
70+
CodedBitstreamUnit *unit = &td->units[i];
71+
VP9RawFrame *frame = unit->content;
72+
VP9RawFrameHeader *hdr = &frame->header;
73+
int subsampling_x, subsampling_y, bit_depth, intra_only;
74+
75+
if (hdr->show_existing_frame) {
76+
VP9ReferenceFrameState *ref = &vp9->ref[hdr->frame_to_show_map_idx];
77+
78+
ctx->width = ref->frame_width;
79+
ctx->height = ref->frame_height;
80+
81+
subsampling_x = ref->subsampling_x;
82+
subsampling_y = ref->subsampling_y;
83+
bit_depth = ref->bit_depth;
84+
intra_only = ref->intra_only;
85+
86+
ctx->key_frame = 0;
87+
} else if (!hdr->show_frame) {
88+
continue;
89+
} else {
90+
ctx->width = vp9->frame_width;
91+
ctx->height = vp9->frame_height;
92+
93+
subsampling_x = vp9->subsampling_x;
94+
subsampling_y = vp9->subsampling_y;
95+
bit_depth = vp9->bit_depth;
96+
intra_only = 0;
97+
98+
ctx->key_frame = !hdr->frame_type;
99+
}
55100

56-
if (!keyframe) {
57-
ctx->pict_type = AV_PICTURE_TYPE_P;
58-
ctx->key_frame = 0;
59-
} else {
60-
ctx->pict_type = AV_PICTURE_TYPE_I;
61-
ctx->key_frame = 1;
101+
avctx->profile = vp9->profile;
102+
103+
ctx->pict_type = (ctx->key_frame || intra_only) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
104+
ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
105+
106+
switch (bit_depth) {
107+
case 8:
108+
ctx->format = pix_fmts_8bit [subsampling_x][subsampling_y];
109+
break;
110+
case 10:
111+
ctx->format = pix_fmts_10bit[subsampling_x][subsampling_y];
112+
break;
113+
case 12:
114+
ctx->format = pix_fmts_12bit[subsampling_x][subsampling_y];
115+
break;
116+
}
62117
}
63118

119+
end:
120+
ff_cbs_fragment_uninit(s->cbc, td);
121+
122+
s->cbc->log_ctx = NULL;
123+
64124
return size;
65125
}
66126

127+
static av_cold int vp9_parser_init(AVCodecParserContext *ctx)
128+
{
129+
VP9ParseContext *s = ctx->priv_data;
130+
int ret;
131+
132+
ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VP9, NULL);
133+
if (ret < 0)
134+
return ret;
135+
136+
return 0;
137+
}
138+
139+
static void vp9_parser_close(AVCodecParserContext *ctx)
140+
{
141+
VP9ParseContext *s = ctx->priv_data;
142+
143+
ff_cbs_close(&s->cbc);
144+
}
145+
67146
AVCodecParser ff_vp9_parser = {
68147
.codec_ids = { AV_CODEC_ID_VP9 },
69-
.parser_parse = parse,
148+
.priv_data_size = sizeof(VP9ParseContext),
149+
.parser_init = vp9_parser_init,
150+
.parser_close = vp9_parser_close,
151+
.parser_parse = vp9_parser_parse,
70152
};

0 commit comments

Comments
 (0)