|
1 | 1 | /* |
2 | | - * VP9 compatible video decoder |
| 2 | + * VP9 parser |
3 | 3 | * |
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> |
6 | 5 | * |
7 | 6 | * This file is part of FFmpeg. |
8 | 7 | * |
|
21 | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | 21 | */ |
23 | 22 |
|
24 | | -#include "libavutil/intreadwrite.h" |
25 | | -#include "libavcodec/get_bits.h" |
| 23 | +#include "cbs.h" |
| 24 | +#include "cbs_vp9.h" |
26 | 25 | #include "parser.h" |
27 | 26 |
|
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) |
32 | 49 | { |
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; |
35 | 54 |
|
36 | 55 | *out_data = data; |
37 | 56 | *out_size = size; |
38 | 57 |
|
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; |
54 | 68 | } |
| 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 | + } |
55 | 100 |
|
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 | + } |
62 | 117 | } |
63 | 118 |
|
| 119 | +end: |
| 120 | + ff_cbs_fragment_uninit(s->cbc, td); |
| 121 | + |
| 122 | + s->cbc->log_ctx = NULL; |
| 123 | + |
64 | 124 | return size; |
65 | 125 | } |
66 | 126 |
|
| 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 | + |
67 | 146 | AVCodecParser ff_vp9_parser = { |
68 | 147 | .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, |
70 | 152 | }; |
0 commit comments