]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/av1_parser.c
avcodec: Constify all the AVCodecParsers
[ffmpeg] / libavcodec / av1_parser.c
index 08721c356aa0f23649c18613525fbd1ad49c0ee0..578f5293c87afd56f9c0c5c0b7f1d96f3157cb0a 100644 (file)
@@ -45,6 +45,10 @@ static const enum AVPixelFormat pix_fmts_12bit[2][2] = {
     { AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12 },
 };
 
+static const enum AVPixelFormat pix_fmts_rgb[3] = {
+    AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
+};
+
 static int av1_parser_parse(AVCodecParserContext *ctx,
                             AVCodecContext *avctx,
                             const uint8_t **out_data, int *out_size,
@@ -53,6 +57,8 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
     AV1ParseContext *s = ctx->priv_data;
     CodedBitstreamFragment *td = &s->temporal_unit;
     CodedBitstreamAV1Context *av1 = s->cbc->priv_data;
+    AV1RawSequenceHeader *seq;
+    AV1RawColorConfig *color;
     int ret;
 
     *out_data = data;
@@ -67,12 +73,12 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
     if (avctx->extradata_size && !s->parsed_extradata) {
         s->parsed_extradata = 1;
 
-        ret = ff_cbs_read(s->cbc, td, avctx->extradata, avctx->extradata_size);
+        ret = ff_cbs_read_extradata_from_codec(s->cbc, td, avctx);
         if (ret < 0) {
             av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
         }
 
-        ff_cbs_fragment_reset(s->cbc, td);
+        ff_cbs_fragment_reset(td);
     }
 
     ret = ff_cbs_read(s->cbc, td, data, size);
@@ -86,13 +92,13 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
         goto end;
     }
 
+    seq = av1->sequence_header;
+    color = &seq->color_config;
+
     for (int i = 0; i < td->nb_units; i++) {
         CodedBitstreamUnit *unit = &td->units[i];
         AV1RawOBU *obu = unit->content;
-        AV1RawSequenceHeader *seq = av1->sequence_header;
-        AV1RawColorConfig *color = &seq->color_config;
         AV1RawFrameHeader *frame;
-        int frame_type;
 
         if (unit->type == AV1_OBU_FRAME)
             frame = &obu->obu.frame.header;
@@ -101,33 +107,18 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
         else
             continue;
 
-        if (frame->show_existing_frame) {
-            AV1ReferenceFrameState *ref = &av1->ref[frame->frame_to_show_map_idx];
-
-            if (!ref->valid) {
-                av_log(avctx, AV_LOG_ERROR, "Invalid reference frame\n");
-                goto end;
-            }
-
-            ctx->width  = ref->frame_width;
-            ctx->height = ref->frame_height;
-            frame_type  = ref->frame_type;
+        if (obu->header.spatial_id > 0)
+            continue;
 
-            ctx->key_frame = 0;
-        } else if (!frame->show_frame) {
+        if (!frame->show_frame && !frame->show_existing_frame)
             continue;
-        } else {
-            ctx->width  = av1->frame_width;
-            ctx->height = av1->frame_height;
-            frame_type  = frame->frame_type;
 
-            ctx->key_frame = frame_type == AV1_FRAME_KEY;
-        }
+        ctx->width  = frame->frame_width_minus_1 + 1;
+        ctx->height = frame->frame_height_minus_1 + 1;
 
-        avctx->profile = seq->seq_profile;
-        avctx->level   = seq->seq_level_idx[0];
+        ctx->key_frame = frame->frame_type == AV1_FRAME_KEY && !frame->show_existing_frame;
 
-        switch (frame_type) {
+        switch (frame->frame_type) {
         case AV1_FRAME_KEY:
         case AV1_FRAME_INTRA_ONLY:
             ctx->pict_type = AV_PICTURE_TYPE_I;
@@ -140,35 +131,43 @@ static int av1_parser_parse(AVCodecParserContext *ctx,
             break;
         }
         ctx->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
+    }
 
-        switch (av1->bit_depth) {
-        case 8:
-            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
-                                             : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
-            break;
-        case 10:
-            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
-                                             : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
-            break;
-        case 12:
-            ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
-                                             : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
-            break;
-        }
-        av_assert2(ctx->format != AV_PIX_FMT_NONE);
-
-        if (ctx->width != avctx->width || ctx->height != avctx->height) {
-            ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
-            if (ret < 0)
-                goto end;
-        }
+    switch (av1->bit_depth) {
+    case 8:
+        ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY8
+                                         : pix_fmts_8bit [color->subsampling_x][color->subsampling_y];
+        break;
+    case 10:
+        ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY10
+                                         : pix_fmts_10bit[color->subsampling_x][color->subsampling_y];
+        break;
+    case 12:
+        ctx->format = color->mono_chrome ? AV_PIX_FMT_GRAY12
+                                         : pix_fmts_12bit[color->subsampling_x][color->subsampling_y];
+        break;
     }
+    av_assert2(ctx->format != AV_PIX_FMT_NONE);
+
+    if (!color->subsampling_x && !color->subsampling_y &&
+        color->matrix_coefficients       == AVCOL_SPC_RGB &&
+        color->color_primaries           == AVCOL_PRI_BT709 &&
+        color->transfer_characteristics  == AVCOL_TRC_IEC61966_2_1)
+        ctx->format = pix_fmts_rgb[color->high_bitdepth + color->twelve_bit];
+
+    avctx->profile = seq->seq_profile;
+    avctx->level   = seq->seq_level_idx[0];
+
+    avctx->colorspace = (enum AVColorSpace) color->matrix_coefficients;
+    avctx->color_primaries = (enum AVColorPrimaries) color->color_primaries;
+    avctx->color_trc = (enum AVColorTransferCharacteristic) color->transfer_characteristics;
+    avctx->color_range = color->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
 
     if (avctx->framerate.num)
         avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
 
 end:
-    ff_cbs_fragment_reset(s->cbc, td);
+    ff_cbs_fragment_reset(td);
 
     s->cbc->log_ctx = NULL;
 
@@ -192,7 +191,7 @@ static av_cold int av1_parser_init(AVCodecParserContext *ctx)
     if (ret < 0)
         return ret;
 
-    s->cbc->decompose_unit_types    = (CodedBitstreamUnitType *)decompose_unit_types;
+    s->cbc->decompose_unit_types    = decompose_unit_types;
     s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
 
     return 0;
@@ -202,7 +201,7 @@ static void av1_parser_close(AVCodecParserContext *ctx)
 {
     AV1ParseContext *s = ctx->priv_data;
 
-    ff_cbs_fragment_free(s->cbc, &s->temporal_unit);
+    ff_cbs_fragment_free(&s->temporal_unit);
     ff_cbs_close(&s->cbc);
 }
 
@@ -228,7 +227,7 @@ static int av1_parser_split(AVCodecContext *avctx,
     return 0;
 }
 
-AVCodecParser ff_av1_parser = {
+const AVCodecParser ff_av1_parser = {
     .codec_ids      = { AV_CODEC_ID_AV1 },
     .priv_data_size = sizeof(AV1ParseContext),
     .parser_init    = av1_parser_init,