LCOV - code coverage report
Current view: top level - libavcodec - h265_metadata_bsf.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 78 172 45.3 %
Date: 2017-12-13 02:34:56 Functions: 5 5 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/common.h"
      20             : #include "libavutil/opt.h"
      21             : 
      22             : #include "bsf.h"
      23             : #include "cbs.h"
      24             : #include "cbs_h265.h"
      25             : #include "hevc.h"
      26             : 
      27             : enum {
      28             :     PASS,
      29             :     INSERT,
      30             :     REMOVE,
      31             : };
      32             : 
      33             : typedef struct H265MetadataContext {
      34             :     const AVClass *class;
      35             : 
      36             :     CodedBitstreamContext *cbc;
      37             :     CodedBitstreamFragment access_unit;
      38             : 
      39             :     H265RawAUD aud_nal;
      40             : 
      41             :     int aud;
      42             : 
      43             :     AVRational sample_aspect_ratio;
      44             : 
      45             :     int video_format;
      46             :     int video_full_range_flag;
      47             :     int colour_primaries;
      48             :     int transfer_characteristics;
      49             :     int matrix_coefficients;
      50             : 
      51             :     int chroma_sample_loc_type;
      52             : 
      53             :     AVRational tick_rate;
      54             :     int poc_proportional_to_timing_flag;
      55             :     int num_ticks_poc_diff_one;
      56             : 
      57             :     int crop_left;
      58             :     int crop_right;
      59             :     int crop_top;
      60             :     int crop_bottom;
      61             : } H265MetadataContext;
      62             : 
      63             : 
      64          46 : static int h265_metadata_update_vps(AVBSFContext *bsf,
      65             :                                     H265RawVPS *vps)
      66             : {
      67          46 :     H265MetadataContext *ctx = bsf->priv_data;
      68             : 
      69          46 :     if (ctx->tick_rate.num && ctx->tick_rate.den) {
      70             :         int num, den;
      71             : 
      72           0 :         av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
      73             :                   UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
      74             : 
      75           0 :         vps->vps_time_scale        = num;
      76           0 :         vps->vps_num_units_in_tick = den;
      77             : 
      78           0 :         vps->vps_timing_info_present_flag = 1;
      79             : 
      80           0 :         if (ctx->num_ticks_poc_diff_one > 0) {
      81           0 :             vps->vps_num_ticks_poc_diff_one_minus1 =
      82           0 :                 ctx->num_ticks_poc_diff_one - 1;
      83           0 :             vps->vps_poc_proportional_to_timing_flag = 1;
      84           0 :         } else if (ctx->num_ticks_poc_diff_one == 0) {
      85           0 :             vps->vps_poc_proportional_to_timing_flag = 0;
      86             :         }
      87             :     }
      88             : 
      89          46 :     return 0;
      90             : }
      91             : 
      92          48 : static int h265_metadata_update_sps(AVBSFContext *bsf,
      93             :                                     H265RawSPS *sps)
      94             : {
      95          48 :     H265MetadataContext *ctx = bsf->priv_data;
      96          48 :     int need_vui = 0;
      97             :     int crop_unit_x, crop_unit_y;
      98             : 
      99          48 :     if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
     100             :         // Table E-1.
     101             :         static const AVRational sar_idc[] = {
     102             :             {   0,  0 }, // Unspecified (never written here).
     103             :             {   1,  1 }, {  12, 11 }, {  10, 11 }, {  16, 11 },
     104             :             {  40, 33 }, {  24, 11 }, {  20, 11 }, {  32, 11 },
     105             :             {  80, 33 }, {  18, 11 }, {  15, 11 }, {  64, 33 },
     106             :             { 160, 99 }, {   4,  3 }, {   3,  2 }, {   2,  1 },
     107             :         };
     108             :         int num, den, i;
     109             : 
     110           0 :         av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
     111           0 :                   ctx->sample_aspect_ratio.den, 65535);
     112             : 
     113           0 :         for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
     114           0 :             if (num == sar_idc[i].num &&
     115           0 :                 den == sar_idc[i].den)
     116           0 :                 break;
     117             :         }
     118           0 :         if (i == FF_ARRAY_ELEMS(sar_idc)) {
     119           0 :             sps->vui.aspect_ratio_idc = 255;
     120           0 :             sps->vui.sar_width  = num;
     121           0 :             sps->vui.sar_height = den;
     122             :         } else {
     123           0 :             sps->vui.aspect_ratio_idc = i;
     124             :         }
     125           0 :         sps->vui.aspect_ratio_info_present_flag = 1;
     126           0 :         need_vui = 1;
     127             :     }
     128             : 
     129             : #define SET_OR_INFER(field, value, present_flag, infer) do { \
     130             :         if (value >= 0) { \
     131             :             field = value; \
     132             :             need_vui = 1; \
     133             :         } else if (!present_flag) \
     134             :             field = infer; \
     135             :     } while (0)
     136             : 
     137          96 :     if (ctx->video_format             >= 0 ||
     138          96 :         ctx->video_full_range_flag    >= 0 ||
     139          96 :         ctx->colour_primaries         >= 0 ||
     140          96 :         ctx->transfer_characteristics >= 0 ||
     141          48 :         ctx->matrix_coefficients      >= 0) {
     142             : 
     143           0 :         SET_OR_INFER(sps->vui.video_format, ctx->video_format,
     144             :                      sps->vui.video_signal_type_present_flag, 5);
     145             : 
     146           0 :         SET_OR_INFER(sps->vui.video_full_range_flag,
     147             :                      ctx->video_full_range_flag,
     148             :                      sps->vui.video_signal_type_present_flag, 0);
     149             : 
     150           0 :         if (ctx->colour_primaries         >= 0 ||
     151           0 :             ctx->transfer_characteristics >= 0 ||
     152           0 :             ctx->matrix_coefficients      >= 0) {
     153             : 
     154           0 :             SET_OR_INFER(sps->vui.colour_primaries,
     155             :                          ctx->colour_primaries,
     156             :                          sps->vui.colour_description_present_flag, 2);
     157             : 
     158           0 :             SET_OR_INFER(sps->vui.transfer_characteristics,
     159             :                          ctx->transfer_characteristics,
     160             :                          sps->vui.colour_description_present_flag, 2);
     161             : 
     162           0 :             SET_OR_INFER(sps->vui.matrix_coefficients,
     163             :                          ctx->matrix_coefficients,
     164             :                          sps->vui.colour_description_present_flag, 2);
     165             : 
     166           0 :             sps->vui.colour_description_present_flag = 1;
     167             :         }
     168           0 :         sps->vui.video_signal_type_present_flag = 1;
     169           0 :         need_vui = 1;
     170             :     }
     171             : 
     172          48 :     if (ctx->chroma_sample_loc_type >= 0) {
     173           0 :         sps->vui.chroma_sample_loc_type_top_field =
     174           0 :             ctx->chroma_sample_loc_type;
     175           0 :         sps->vui.chroma_sample_loc_type_bottom_field =
     176           0 :             ctx->chroma_sample_loc_type;
     177           0 :         sps->vui.chroma_loc_info_present_flag = 1;
     178           0 :         need_vui = 1;
     179             :     }
     180             : 
     181          48 :     if (ctx->tick_rate.num && ctx->tick_rate.den) {
     182             :         int num, den;
     183             : 
     184           0 :         av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
     185             :                   UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
     186             : 
     187           0 :         sps->vui.vui_time_scale        = num;
     188           0 :         sps->vui.vui_num_units_in_tick = den;
     189             : 
     190           0 :         sps->vui.vui_timing_info_present_flag = 1;
     191           0 :         need_vui = 1;
     192             : 
     193           0 :         if (ctx->num_ticks_poc_diff_one > 0) {
     194           0 :             sps->vui.vui_num_ticks_poc_diff_one_minus1 =
     195           0 :                 ctx->num_ticks_poc_diff_one - 1;
     196           0 :             sps->vui.vui_poc_proportional_to_timing_flag = 1;
     197           0 :         } else if (ctx->num_ticks_poc_diff_one == 0) {
     198           0 :             sps->vui.vui_poc_proportional_to_timing_flag = 0;
     199             :         }
     200             :     }
     201             : 
     202          48 :     if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
     203           0 :         crop_unit_x = 1;
     204           0 :         crop_unit_y = 1;
     205             :     } else {
     206          48 :         crop_unit_x = 1 + (sps->chroma_format_idc < 3);
     207          48 :         crop_unit_y = 1 + (sps->chroma_format_idc < 2);
     208             :     }
     209             : #define CROP(border, unit) do { \
     210             :         if (ctx->crop_ ## border >= 0) { \
     211             :             if (ctx->crop_ ## border % unit != 0) { \
     212             :                 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
     213             :                        "must be a multiple of %d.\n", #border, unit); \
     214             :                 return AVERROR(EINVAL); \
     215             :             } \
     216             :             sps->conf_win_ ## border ## _offset = \
     217             :                 ctx->crop_ ## border / unit; \
     218             :             sps->conformance_window_flag = 1; \
     219             :         } \
     220             :     } while (0)
     221          48 :     CROP(left,   crop_unit_x);
     222          48 :     CROP(right,  crop_unit_x);
     223          48 :     CROP(top,    crop_unit_y);
     224          48 :     CROP(bottom, crop_unit_y);
     225             : #undef CROP
     226             : 
     227          48 :     if (need_vui)
     228           0 :         sps->vui_parameters_present_flag = 1;
     229             : 
     230          48 :     return 0;
     231             : }
     232             : 
     233        4198 : static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *out)
     234             : {
     235        4198 :     H265MetadataContext *ctx = bsf->priv_data;
     236        4198 :     AVPacket *in = NULL;
     237        4198 :     CodedBitstreamFragment *au = &ctx->access_unit;
     238             :     int err, i;
     239             : 
     240        4198 :     err = ff_bsf_get_packet(bsf, &in);
     241        4198 :     if (err < 0)
     242        2099 :         goto fail;
     243             : 
     244        2099 :     err = ff_cbs_read_packet(ctx->cbc, au, in);
     245        2099 :     if (err < 0) {
     246           0 :         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
     247           0 :         goto fail;
     248             :     }
     249             : 
     250        2099 :     if (au->nb_units == 0) {
     251           0 :         av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
     252           0 :         err = AVERROR_INVALIDDATA;
     253           0 :         goto fail;
     254             :     }
     255             : 
     256             :     // If an AUD is present, it must be the first NAL unit.
     257        2099 :     if (au->units[0].type == HEVC_NAL_AUD) {
     258          60 :         if (ctx->aud == REMOVE)
     259           0 :             ff_cbs_delete_unit(ctx->cbc, au, 0);
     260             :     } else {
     261        2039 :         if (ctx->aud == INSERT) {
     262           0 :             H265RawAUD *aud = &ctx->aud_nal;
     263           0 :             int pic_type = 0, temporal_id = 8, layer_id = 0;
     264             : 
     265           0 :             for (i = 0; i < au->nb_units; i++) {
     266           0 :                 const H265RawNALUnitHeader *nal = au->units[i].content;
     267           0 :                 if (!nal)
     268           0 :                     continue;
     269           0 :                 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
     270           0 :                     temporal_id = nal->nuh_temporal_id_plus1 - 1;
     271             : 
     272           0 :                 if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
     273           0 :                     const H265RawSlice *slice = au->units[i].content;
     274           0 :                     layer_id = nal->nuh_layer_id;
     275           0 :                     if (slice->header.slice_type == HEVC_SLICE_B &&
     276             :                         pic_type < 2)
     277           0 :                         pic_type = 2;
     278           0 :                     if (slice->header.slice_type == HEVC_SLICE_P &&
     279             :                         pic_type < 1)
     280           0 :                         pic_type = 1;
     281             :                 }
     282             :             }
     283             : 
     284           0 :             aud->nal_unit_header = (H265RawNALUnitHeader) {
     285             :                 .nal_unit_type         = HEVC_NAL_AUD,
     286             :                 .nuh_layer_id          = layer_id,
     287           0 :                 .nuh_temporal_id_plus1 = temporal_id + 1,
     288             :             };
     289           0 :             aud->pic_type = pic_type;
     290             : 
     291           0 :             err = ff_cbs_insert_unit_content(ctx->cbc, au,
     292             :                                              0, HEVC_NAL_AUD, aud);
     293           0 :             if (err) {
     294           0 :                 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
     295           0 :                 goto fail;
     296             :             }
     297             :         }
     298             :     }
     299             : 
     300        9798 :     for (i = 0; i < au->nb_units; i++) {
     301        7699 :         if (au->units[i].type == HEVC_NAL_VPS) {
     302          26 :             err = h265_metadata_update_vps(bsf, au->units[i].content);
     303          26 :             if (err < 0)
     304           0 :                 goto fail;
     305             :         }
     306        7699 :         if (au->units[i].type == HEVC_NAL_SPS) {
     307          27 :             err = h265_metadata_update_sps(bsf, au->units[i].content);
     308          27 :             if (err < 0)
     309           0 :                 goto fail;
     310             :         }
     311             :     }
     312             : 
     313        2099 :     err = ff_cbs_write_packet(ctx->cbc, out, au);
     314        2099 :     if (err < 0) {
     315           0 :         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
     316           0 :         goto fail;
     317             :     }
     318             : 
     319        2099 :     err = av_packet_copy_props(out, in);
     320        2099 :     if (err < 0)
     321           0 :         goto fail;
     322             : 
     323        2099 :     err = 0;
     324        4198 : fail:
     325        4198 :     ff_cbs_fragment_uninit(ctx->cbc, au);
     326             : 
     327        4198 :     av_packet_free(&in);
     328             : 
     329        4198 :     return err;
     330             : }
     331             : 
     332          19 : static int h265_metadata_init(AVBSFContext *bsf)
     333             : {
     334          19 :     H265MetadataContext *ctx = bsf->priv_data;
     335          19 :     CodedBitstreamFragment *au = &ctx->access_unit;
     336             :     int err, i;
     337             : 
     338          19 :     err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_HEVC, bsf);
     339          19 :     if (err < 0)
     340           0 :         return err;
     341             : 
     342          19 :     if (bsf->par_in->extradata) {
     343          19 :         err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
     344          19 :         if (err < 0) {
     345           0 :             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
     346           0 :             goto fail;
     347             :         }
     348             : 
     349          80 :         for (i = 0; i < au->nb_units; i++) {
     350          61 :             if (au->units[i].type == HEVC_NAL_VPS) {
     351          20 :                 err = h265_metadata_update_vps(bsf, au->units[i].content);
     352          20 :                 if (err < 0)
     353           0 :                     goto fail;
     354             :             }
     355          61 :             if (au->units[i].type == HEVC_NAL_SPS) {
     356          21 :                 err = h265_metadata_update_sps(bsf, au->units[i].content);
     357          21 :                 if (err < 0)
     358           0 :                     goto fail;
     359             :             }
     360             :         }
     361             : 
     362          19 :         err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
     363          19 :         if (err < 0) {
     364           0 :             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
     365           0 :             goto fail;
     366             :         }
     367             :     }
     368             : 
     369          19 :     err = 0;
     370          19 : fail:
     371          19 :     ff_cbs_fragment_uninit(ctx->cbc, au);
     372          19 :     return err;
     373             : }
     374             : 
     375          19 : static void h265_metadata_close(AVBSFContext *bsf)
     376             : {
     377          19 :     H265MetadataContext *ctx = bsf->priv_data;
     378          19 :     ff_cbs_close(&ctx->cbc);
     379          19 : }
     380             : 
     381             : #define OFFSET(x) offsetof(H265MetadataContext, x)
     382             : static const AVOption h265_metadata_options[] = {
     383             :     { "aud", "Access Unit Delimiter NAL units",
     384             :         OFFSET(aud), AV_OPT_TYPE_INT,
     385             :         { .i64 = PASS }, PASS, REMOVE, 0, "aud" },
     386             :     { "pass",   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PASS   }, .unit = "aud" },
     387             :     { "insert", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = INSERT }, .unit = "aud" },
     388             :     { "remove", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE }, .unit = "aud" },
     389             : 
     390             :     { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
     391             :         OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
     392             :         { .dbl = 0.0 }, 0, 65535 },
     393             : 
     394             :     { "video_format", "Set video format (table E-2)",
     395             :         OFFSET(video_format), AV_OPT_TYPE_INT,
     396             :         { .i64 = -1 }, -1, 7 },
     397             :     { "video_full_range_flag", "Set video full range flag",
     398             :         OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
     399             :         { .i64 = -1 }, -1, 1 },
     400             :     { "colour_primaries", "Set colour primaries (table E-3)",
     401             :         OFFSET(colour_primaries), AV_OPT_TYPE_INT,
     402             :         { .i64 = -1 }, -1, 255 },
     403             :     { "transfer_characteristics", "Set transfer characteristics (table E-4)",
     404             :         OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
     405             :         { .i64 = -1 }, -1, 255 },
     406             :     { "matrix_coefficients", "Set matrix coefficients (table E-5)",
     407             :         OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
     408             :         { .i64 = -1 }, -1, 255 },
     409             : 
     410             :     { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
     411             :         OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
     412             :         { .i64 = -1 }, -1, 6 },
     413             : 
     414             :     { "tick_rate",
     415             :         "Set VPS and VUI tick rate (num_units_in_tick / time_scale)",
     416             :         OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
     417             :         { .dbl = 0.0 }, 0, UINT_MAX },
     418             :     { "num_ticks_poc_diff_one",
     419             :         "Set VPS and VUI number of ticks per POC increment",
     420             :         OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
     421             :         { .i64 = -1 }, -1, INT_MAX },
     422             : 
     423             :     { "crop_left", "Set left border crop offset",
     424             :         OFFSET(crop_left), AV_OPT_TYPE_INT,
     425             :         { .i64 = -1 }, -1, HEVC_MAX_WIDTH },
     426             :     { "crop_right", "Set right border crop offset",
     427             :         OFFSET(crop_right), AV_OPT_TYPE_INT,
     428             :         { .i64 = -1 }, -1, HEVC_MAX_WIDTH },
     429             :     { "crop_top", "Set top border crop offset",
     430             :         OFFSET(crop_top), AV_OPT_TYPE_INT,
     431             :         { .i64 = -1 }, -1, HEVC_MAX_HEIGHT },
     432             :     { "crop_bottom", "Set bottom border crop offset",
     433             :         OFFSET(crop_bottom), AV_OPT_TYPE_INT,
     434             :         { .i64 = -1 }, -1, HEVC_MAX_HEIGHT },
     435             : 
     436             :     { NULL }
     437             : };
     438             : 
     439             : static const AVClass h265_metadata_class = {
     440             :     .class_name = "h265_metadata_bsf",
     441             :     .item_name  = av_default_item_name,
     442             :     .option     = h265_metadata_options,
     443             :     .version    = LIBAVUTIL_VERSION_INT,
     444             : };
     445             : 
     446             : static const enum AVCodecID h265_metadata_codec_ids[] = {
     447             :     AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
     448             : };
     449             : 
     450             : const AVBitStreamFilter ff_hevc_metadata_bsf = {
     451             :     .name           = "hevc_metadata",
     452             :     .priv_data_size = sizeof(H265MetadataContext),
     453             :     .priv_class     = &h265_metadata_class,
     454             :     .init           = &h265_metadata_init,
     455             :     .close          = &h265_metadata_close,
     456             :     .filter         = &h265_metadata_filter,
     457             :     .codec_ids      = h265_metadata_codec_ids,
     458             : };

Generated by: LCOV version 1.13