| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AV1 parser | ||
| 3 | * | ||
| 4 | * Copyright (C) 2018 James Almer <jamrial@gmail.com> | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #include "libavutil/attributes.h" | ||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | |||
| 26 | #include "av1_parse.h" | ||
| 27 | #include "avcodec.h" | ||
| 28 | #include "cbs.h" | ||
| 29 | #include "cbs_av1.h" | ||
| 30 | #include "parser_internal.h" | ||
| 31 | |||
| 32 | typedef struct AV1ParseContext { | ||
| 33 | CodedBitstreamContext *cbc; | ||
| 34 | CodedBitstreamFragment temporal_unit; | ||
| 35 | int parsed_extradata; | ||
| 36 | } AV1ParseContext; | ||
| 37 | |||
| 38 | static const enum AVPixelFormat pix_fmts_8bit[2][2] = { | ||
| 39 | { AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE }, | ||
| 40 | { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P }, | ||
| 41 | }; | ||
| 42 | static const enum AVPixelFormat pix_fmts_10bit[2][2] = { | ||
| 43 | { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_NONE }, | ||
| 44 | { AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10 }, | ||
| 45 | }; | ||
| 46 | static const enum AVPixelFormat pix_fmts_12bit[2][2] = { | ||
| 47 | { AV_PIX_FMT_YUV444P12, AV_PIX_FMT_NONE }, | ||
| 48 | { AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 }, | ||
| 49 | }; | ||
| 50 | |||
| 51 | static const enum AVPixelFormat pix_fmts_rgb[3] = { | ||
| 52 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, | ||
| 53 | }; | ||
| 54 | |||
| 55 | 665 | static int av1_parser_parse(AVCodecParserContext *ctx, | |
| 56 | AVCodecContext *avctx, | ||
| 57 | const uint8_t **out_data, int *out_size, | ||
| 58 | const uint8_t *data, int size) | ||
| 59 | { | ||
| 60 | 665 | AV1ParseContext *s = ctx->priv_data; | |
| 61 | 665 | CodedBitstreamFragment *td = &s->temporal_unit; | |
| 62 | 665 | const CodedBitstreamAV1Context *av1 = s->cbc->priv_data; | |
| 63 | const AV1RawSequenceHeader *seq; | ||
| 64 | const AV1RawColorConfig *color; | ||
| 65 | int ret; | ||
| 66 | |||
| 67 | 665 | *out_data = data; | |
| 68 | 665 | *out_size = size; | |
| 69 | |||
| 70 | 665 | ctx->key_frame = -1; | |
| 71 | 665 | ctx->pict_type = AV_PICTURE_TYPE_NONE; | |
| 72 | 665 | ctx->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; | |
| 73 | |||
| 74 | 665 | s->cbc->log_ctx = avctx; | |
| 75 | |||
| 76 |
4/4✓ Branch 0 taken 620 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 593 times.
|
665 | if (avctx->extradata_size && !s->parsed_extradata) { |
| 77 | 27 | s->parsed_extradata = 1; | |
| 78 | |||
| 79 | 27 | ret = ff_cbs_read_extradata_from_codec(s->cbc, td, avctx); | |
| 80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) { |
| 81 | ✗ | av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n"); | |
| 82 | } | ||
| 83 | |||
| 84 | 27 | ff_cbs_fragment_reset(td); | |
| 85 | } | ||
| 86 | |||
| 87 | 665 | ret = ff_cbs_read(s->cbc, td, NULL, data, size); | |
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 665 times.
|
665 | if (ret < 0) { |
| 89 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to parse temporal unit.\n"); | |
| 90 | ✗ | goto end; | |
| 91 | } | ||
| 92 | |||
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 665 times.
|
665 | if (!av1->sequence_header) { |
| 94 | ✗ | av_log(avctx, AV_LOG_ERROR, "No sequence header available\n"); | |
| 95 | ✗ | goto end; | |
| 96 | } | ||
| 97 | |||
| 98 | 665 | seq = av1->sequence_header; | |
| 99 | 665 | color = &seq->color_config; | |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 1803 times.
✓ Branch 1 taken 665 times.
|
2468 | for (int i = 0; i < td->nb_units; i++) { |
| 102 | 1803 | const CodedBitstreamUnit *unit = &td->units[i]; | |
| 103 | 1803 | const AV1RawOBU *obu = unit->content; | |
| 104 | const AV1RawFrameHeader *frame; | ||
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 652 times.
✓ Branch 1 taken 1151 times.
|
1803 | if (unit->type == AV1_OBU_FRAME) |
| 107 | 652 | frame = &obu->obu.frame.header; | |
| 108 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 995 times.
|
1151 | else if (unit->type == AV1_OBU_FRAME_HEADER) |
| 109 | 156 | frame = &obu->obu.frame_header; | |
| 110 | else | ||
| 111 | 995 | continue; | |
| 112 | |||
| 113 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 792 times.
|
808 | if (obu->header.spatial_id > 0) |
| 114 | 16 | continue; | |
| 115 | |||
| 116 |
4/4✓ Branch 0 taken 271 times.
✓ Branch 1 taken 521 times.
✓ Branch 2 taken 155 times.
✓ Branch 3 taken 116 times.
|
792 | if (!frame->show_frame && !frame->show_existing_frame) |
| 117 | 155 | continue; | |
| 118 | |||
| 119 | 637 | ctx->width = frame->frame_width_minus_1 + 1; | |
| 120 | 637 | ctx->height = frame->frame_height_minus_1 + 1; | |
| 121 | |||
| 122 |
3/4✓ Branch 0 taken 71 times.
✓ Branch 1 taken 566 times.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
637 | ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame; |
| 123 | |||
| 124 |
3/4✓ Branch 0 taken 71 times.
✓ Branch 1 taken 565 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
637 | switch (frame->frame_type) { |
| 125 | 71 | case AV1_FRAME_KEY: | |
| 126 | case AV1_FRAME_INTRA_ONLY: | ||
| 127 | 71 | ctx->pict_type = AV_PICTURE_TYPE_I; | |
| 128 | 71 | break; | |
| 129 | 565 | case AV1_FRAME_INTER: | |
| 130 | 565 | ctx->pict_type = AV_PICTURE_TYPE_P; | |
| 131 | 565 | break; | |
| 132 | 1 | case AV1_FRAME_SWITCH: | |
| 133 | 1 | ctx->pict_type = AV_PICTURE_TYPE_SP; | |
| 134 | 1 | break; | |
| 135 | } | ||
| 136 | 637 | ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME; | |
| 137 | |||
| 138 | /* Extract SAR from render_height_minus_1 & render_width_minus_1 */ | ||
| 139 | 637 | av_reduce(&avctx->sample_aspect_ratio.num, | |
| 140 | &avctx->sample_aspect_ratio.den, | ||
| 141 | 637 | (int64_t)ctx->height * (frame->render_width_minus_1 + 1), | |
| 142 | 637 | (int64_t)ctx->width * (frame->render_height_minus_1 + 1), | |
| 143 | INT_MAX); | ||
| 144 | } | ||
| 145 | |||
| 146 |
2/4✓ Branch 0 taken 654 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
665 | switch (av1->bit_depth) { |
| 147 | 654 | case 8: | |
| 148 | 1308 | ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8 | |
| 149 |
1/2✓ Branch 0 taken 654 times.
✗ Branch 1 not taken.
|
654 | : pix_fmts_8bit [color->subsampling_x][color->subsampling_y]; |
| 150 | 654 | break; | |
| 151 | 11 | case 10: | |
| 152 | 22 | ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10 | |
| 153 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | : pix_fmts_10bit[color->subsampling_x][color->subsampling_y]; |
| 154 | 11 | break; | |
| 155 | ✗ | case 12: | |
| 156 | ✗ | ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12 | |
| 157 | ✗ | : pix_fmts_12bit[color->subsampling_x][color->subsampling_y]; | |
| 158 | ✗ | break; | |
| 159 | } | ||
| 160 | av_assert2(ctx->format != AV_PIX_FMT_NONE); | ||
| 161 | |||
| 162 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 665 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
665 | if (!color->subsampling_x && !color->subsampling_y && |
| 163 | ✗ | color->matrix_coefficients == AVCOL_SPC_RGB && | |
| 164 | ✗ | color->color_primaries == AVCOL_PRI_BT709 && | |
| 165 | ✗ | color->transfer_characteristics == AVCOL_TRC_IEC61966_2_1) | |
| 166 | ✗ | ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit]; | |
| 167 | |||
| 168 | 665 | avctx->profile = seq->seq_profile; | |
| 169 | 665 | avctx->level = seq->seq_level_idx[0]; | |
| 170 | |||
| 171 | 665 | avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients; | |
| 172 | 665 | avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries; | |
| 173 | 665 | avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics; | |
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 665 times.
|
665 | avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; |
| 175 | |||
| 176 |
2/2✓ Branch 0 taken 395 times.
✓ Branch 1 taken 270 times.
|
665 | if (seq->timing_info_present_flag) |
| 177 | 270 | avctx->framerate = ff_av1_framerate(1LL + seq->timing_info.num_ticks_per_picture_minus_1, | |
| 178 | 270 | seq->timing_info.num_units_in_display_tick, | |
| 179 | 270 | seq->timing_info.time_scale); | |
| 180 | |||
| 181 | 395 | end: | |
| 182 | 665 | ff_cbs_fragment_reset(td); | |
| 183 | |||
| 184 | 665 | s->cbc->log_ctx = NULL; | |
| 185 | |||
| 186 | 665 | return size; | |
| 187 | } | ||
| 188 | |||
| 189 | static const CodedBitstreamUnitType decompose_unit_types[] = { | ||
| 190 | AV1_OBU_TEMPORAL_DELIMITER, | ||
| 191 | AV1_OBU_SEQUENCE_HEADER, | ||
| 192 | AV1_OBU_FRAME_HEADER, | ||
| 193 | AV1_OBU_TILE_GROUP, | ||
| 194 | AV1_OBU_FRAME, | ||
| 195 | }; | ||
| 196 | |||
| 197 | 30 | static av_cold int av1_parser_init(AVCodecParserContext *ctx) | |
| 198 | { | ||
| 199 | 30 | AV1ParseContext *s = ctx->priv_data; | |
| 200 | int ret; | ||
| 201 | |||
| 202 | 30 | ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, NULL); | |
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ret < 0) |
| 204 | ✗ | return ret; | |
| 205 | |||
| 206 | 30 | s->cbc->decompose_unit_types = decompose_unit_types; | |
| 207 | 30 | s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types); | |
| 208 | |||
| 209 | 30 | return 0; | |
| 210 | } | ||
| 211 | |||
| 212 | 30 | static av_cold void av1_parser_close(AVCodecParserContext *ctx) | |
| 213 | { | ||
| 214 | 30 | AV1ParseContext *s = ctx->priv_data; | |
| 215 | |||
| 216 | 30 | ff_cbs_fragment_free(&s->temporal_unit); | |
| 217 | 30 | ff_cbs_close(&s->cbc); | |
| 218 | 30 | } | |
| 219 | |||
| 220 | const FFCodecParser ff_av1_parser = { | ||
| 221 | PARSER_CODEC_LIST(AV_CODEC_ID_AV1), | ||
| 222 | .priv_data_size = sizeof(AV1ParseContext), | ||
| 223 | .init = av1_parser_init, | ||
| 224 | .close = av1_parser_close, | ||
| 225 | .parse = av1_parser_parse, | ||
| 226 | }; | ||
| 227 |