LCOV - code coverage report
Current view: top level - libavcodec - vp9_metadata_bsf.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 44 70.5 %
Date: 2018-05-20 11:54:08 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "libavutil/avstring.h"
      20             : #include "libavutil/common.h"
      21             : #include "libavutil/opt.h"
      22             : 
      23             : #include "bsf.h"
      24             : #include "cbs.h"
      25             : #include "cbs_vp9.h"
      26             : 
      27             : typedef struct VP9MetadataContext {
      28             :     const AVClass *class;
      29             : 
      30             :     CodedBitstreamContext *cbc;
      31             :     CodedBitstreamFragment fragment;
      32             : 
      33             :     int color_space;
      34             :     int color_range;
      35             : 
      36             :     int color_range_rgb_warned;
      37             : } VP9MetadataContext;
      38             : 
      39             : 
      40         366 : static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out)
      41             : {
      42         366 :     VP9MetadataContext *ctx = bsf->priv_data;
      43         366 :     AVPacket *in = NULL;
      44         366 :     CodedBitstreamFragment *frag = &ctx->fragment;
      45             :     int err, i;
      46             : 
      47         366 :     err = ff_bsf_get_packet(bsf, &in);
      48         366 :     if (err < 0)
      49         183 :         return err;
      50             : 
      51         183 :     err = ff_cbs_read_packet(ctx->cbc, frag, in);
      52         183 :     if (err < 0) {
      53           0 :         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
      54           0 :         goto fail;
      55             :     }
      56             : 
      57         380 :     for (i = 0; i < frag->nb_units; i++) {
      58         197 :         VP9RawFrame *frame = frag->units[i].content;
      59         197 :         VP9RawFrameHeader *header = &frame->header;
      60             : 
      61         197 :         if (ctx->color_space >= 0) {
      62           0 :             header->color_space = ctx->color_space;
      63             :         }
      64         197 :         if (ctx->color_range >= 0) {
      65           0 :             if (ctx->color_range == 0 &&
      66           0 :                 header->color_space == VP9_CS_RGB &&
      67           0 :                 !ctx->color_range_rgb_warned) {
      68           0 :                 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot "
      69             :                        "be set to limited in RGB streams.\n");
      70           0 :                 ctx->color_range_rgb_warned = 1;
      71             :             } else {
      72           0 :                 header->color_range = ctx->color_range;
      73             :             }
      74             :         }
      75             :     }
      76             : 
      77         183 :     err = ff_cbs_write_packet(ctx->cbc, out, frag);
      78         183 :     if (err < 0) {
      79           0 :         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
      80           0 :         goto fail;
      81             :     }
      82             : 
      83         183 :     err = av_packet_copy_props(out, in);
      84         183 :     if (err < 0)
      85           0 :         goto fail;
      86             : 
      87         183 :     err = 0;
      88         183 : fail:
      89         183 :     ff_cbs_fragment_uninit(ctx->cbc, frag);
      90             : 
      91         183 :     if (err < 0)
      92           0 :         av_packet_unref(out);
      93         183 :     av_packet_free(&in);
      94             : 
      95         183 :     return err;
      96             : }
      97             : 
      98          14 : static int vp9_metadata_init(AVBSFContext *bsf)
      99             : {
     100          14 :     VP9MetadataContext *ctx = bsf->priv_data;
     101             : 
     102          14 :     return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
     103             : }
     104             : 
     105          14 : static void vp9_metadata_close(AVBSFContext *bsf)
     106             : {
     107          14 :     VP9MetadataContext *ctx = bsf->priv_data;
     108          14 :     ff_cbs_close(&ctx->cbc);
     109          14 : }
     110             : 
     111             : #define OFFSET(x) offsetof(VP9MetadataContext, x)
     112             : #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
     113             : static const AVOption vp9_metadata_options[] = {
     114             :     { "color_space", "Set colour space (section 7.2.2)",
     115             :         OFFSET(color_space), AV_OPT_TYPE_INT,
     116             :         { .i64 = -1 }, -1, VP9_CS_RGB, FLAGS, "cs" },
     117             :     { "unknown",  "Unknown/unspecified",  0, AV_OPT_TYPE_CONST,
     118             :         { .i64 = VP9_CS_UNKNOWN   }, .flags = FLAGS, .unit = "cs" },
     119             :     { "bt601",    "ITU-R BT.601-7",       0, AV_OPT_TYPE_CONST,
     120             :         { .i64 = VP9_CS_BT_601    }, .flags = FLAGS, .unit = "cs" },
     121             :     { "bt709",    "ITU-R BT.709-6",       0, AV_OPT_TYPE_CONST,
     122             :         { .i64 = VP9_CS_BT_709    }, .flags = FLAGS, .unit = "cs" },
     123             :     { "smpte170", "SMPTE-170",            0, AV_OPT_TYPE_CONST,
     124             :         { .i64 = VP9_CS_SMPTE_170 }, .flags = FLAGS, .unit = "cs" },
     125             :     { "smpte240", "SMPTE-240",            0, AV_OPT_TYPE_CONST,
     126             :         { .i64 = VP9_CS_SMPTE_240 }, .flags = FLAGS, .unit = "cs" },
     127             :     { "bt2020",   "ITU-R BT.2020-2",      0, AV_OPT_TYPE_CONST,
     128             :         { .i64 = VP9_CS_BT_2020   }, .flags = FLAGS, .unit = "cs" },
     129             :     { "rgb",      "sRGB / IEC 61966-2-1", 0, AV_OPT_TYPE_CONST,
     130             :         { .i64 = VP9_CS_RGB       }, .flags = FLAGS, .unit = "cs" },
     131             : 
     132             :     { "color_range", "Set colour range (section 7.2.2)",
     133             :         OFFSET(color_range), AV_OPT_TYPE_INT,
     134             :         { .i64 = -1 }, -1, 1, FLAGS, "cr" },
     135             :     { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
     136             :         { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
     137             :     { "pc", "PC (full) range",    0, AV_OPT_TYPE_CONST,
     138             :         { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
     139             : 
     140             :     { NULL }
     141             : };
     142             : 
     143             : static const AVClass vp9_metadata_class = {
     144             :     .class_name = "vp9_metadata_bsf",
     145             :     .item_name  = av_default_item_name,
     146             :     .option     = vp9_metadata_options,
     147             :     .version    = LIBAVUTIL_VERSION_INT,
     148             : };
     149             : 
     150             : static const enum AVCodecID vp9_metadata_codec_ids[] = {
     151             :     AV_CODEC_ID_VP9, AV_CODEC_ID_NONE,
     152             : };
     153             : 
     154             : const AVBitStreamFilter ff_vp9_metadata_bsf = {
     155             :     .name           = "vp9_metadata",
     156             :     .priv_data_size = sizeof(VP9MetadataContext),
     157             :     .priv_class     = &vp9_metadata_class,
     158             :     .init           = &vp9_metadata_init,
     159             :     .close          = &vp9_metadata_close,
     160             :     .filter         = &vp9_metadata_filter,
     161             :     .codec_ids      = vp9_metadata_codec_ids,
     162             : };

Generated by: LCOV version 1.13