LCOV - code coverage report
Current view: top level - libavcodec - h264_redundant_pps_bsf.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 71 0.0 %
Date: 2017-12-15 18:13:28 Functions: 0 5 0.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 <string.h>
      20             : 
      21             : #include "libavutil/common.h"
      22             : #include "libavutil/mem.h"
      23             : 
      24             : #include "bsf.h"
      25             : #include "cbs.h"
      26             : #include "cbs_h264.h"
      27             : #include "h264.h"
      28             : 
      29             : 
      30             : typedef struct H264RedundantPPSContext {
      31             :     CodedBitstreamContext *input;
      32             :     CodedBitstreamContext *output;
      33             : 
      34             :     CodedBitstreamFragment access_unit;
      35             : 
      36             :     int global_pic_init_qp;
      37             :     int current_pic_init_qp;
      38             : } H264RedundantPPSContext;
      39             : 
      40             : 
      41           0 : static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
      42             :                                         H264RawPPS *pps)
      43             : {
      44             :     // Record the current value of pic_init_qp in order to fix up
      45             :     // following slices, then overwrite with the global value.
      46           0 :     ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
      47           0 :     pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
      48             : 
      49             :     // Some PPSs have this set, so it must be set in all of them.
      50             :     // (Slices which do not use such a PPS on input will still have
      51             :     // *_weight_l*flag as zero and therefore write equivalently.)
      52           0 :     pps->weighted_pred_flag = 1;
      53             : 
      54           0 :     return 0;
      55             : }
      56             : 
      57           0 : static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
      58             :                                           H264RawSliceHeader *slice)
      59             : {
      60             :     int qp;
      61             : 
      62           0 :     qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
      63           0 :     slice->slice_qp_delta = qp - ctx->global_pic_init_qp;
      64             : 
      65           0 :     return 0;
      66             : }
      67             : 
      68           0 : static int h264_redundant_pps_filter(AVBSFContext *bsf, AVPacket *out)
      69             : {
      70           0 :     H264RedundantPPSContext *ctx = bsf->priv_data;
      71             :     AVPacket *in;
      72           0 :     CodedBitstreamFragment *au = &ctx->access_unit;
      73             :     int au_has_sps;
      74             :     int err, i;
      75             : 
      76           0 :     err = ff_bsf_get_packet(bsf, &in);
      77           0 :     if (err < 0)
      78           0 :         return err;
      79             : 
      80           0 :     err = ff_cbs_read_packet(ctx->input, au, in);
      81           0 :     if (err < 0)
      82           0 :         return err;
      83             : 
      84           0 :     au_has_sps = 0;
      85           0 :     for (i = 0; i < au->nb_units; i++) {
      86           0 :         CodedBitstreamUnit *nal = &au->units[i];
      87             : 
      88           0 :         if (nal->type == H264_NAL_SPS)
      89           0 :             au_has_sps = 1;
      90           0 :         if (nal->type == H264_NAL_PPS) {
      91           0 :             h264_redundant_pps_fixup_pps(ctx, nal->content);
      92           0 :             if (!au_has_sps) {
      93           0 :                 av_log(ctx, AV_LOG_VERBOSE, "Deleting redundant PPS "
      94           0 :                        "at %"PRId64".\n", in->pts);
      95           0 :                 ff_cbs_delete_unit(ctx->input, au, i);
      96             :             }
      97             :         }
      98           0 :         if (nal->type == H264_NAL_SLICE ||
      99           0 :             nal->type == H264_NAL_IDR_SLICE) {
     100           0 :             H264RawSlice *slice = nal->content;
     101           0 :             h264_redundant_pps_fixup_slice(ctx, &slice->header);
     102             :         }
     103             :     }
     104             : 
     105           0 :     err = ff_cbs_write_packet(ctx->output, out, au);
     106           0 :     if (err < 0)
     107           0 :         return err;
     108             : 
     109           0 :     ff_cbs_fragment_uninit(ctx->output, au);
     110             : 
     111           0 :     err = av_packet_copy_props(out, in);
     112           0 :     if (err < 0)
     113           0 :         return err;
     114             : 
     115           0 :     av_packet_free(&in);
     116             : 
     117           0 :     return 0;
     118             : }
     119             : 
     120           0 : static int h264_redundant_pps_init(AVBSFContext *bsf)
     121             : {
     122           0 :     H264RedundantPPSContext *ctx = bsf->priv_data;
     123           0 :     CodedBitstreamFragment *au = &ctx->access_unit;
     124             :     int err, i;
     125             : 
     126           0 :     err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf);
     127           0 :     if (err < 0)
     128           0 :         return err;
     129             : 
     130           0 :     err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
     131           0 :     if (err < 0)
     132           0 :         return err;
     133             : 
     134           0 :     ctx->global_pic_init_qp = 26;
     135             : 
     136           0 :     if (bsf->par_in->extradata) {
     137           0 :         err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
     138           0 :         if (err < 0) {
     139           0 :             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
     140           0 :             return err;
     141             :         }
     142             : 
     143           0 :         for (i = 0; i < au->nb_units; i++) {
     144           0 :             if (au->units[i].type == H264_NAL_PPS)
     145           0 :                 h264_redundant_pps_fixup_pps(ctx, au->units[i].content);
     146             :         }
     147             : 
     148           0 :         err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
     149           0 :         if (err < 0) {
     150           0 :             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
     151           0 :             return err;
     152             :         }
     153             : 
     154           0 :         ff_cbs_fragment_uninit(ctx->output, au);
     155             :     }
     156             : 
     157           0 :     return 0;
     158             : }
     159             : 
     160           0 : static void h264_redundant_pps_close(AVBSFContext *bsf)
     161             : {
     162           0 :     H264RedundantPPSContext *ctx = bsf->priv_data;
     163           0 :     ff_cbs_close(&ctx->input);
     164           0 :     ff_cbs_close(&ctx->output);
     165           0 : }
     166             : 
     167             : static const enum AVCodecID h264_redundant_pps_codec_ids[] = {
     168             :     AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
     169             : };
     170             : 
     171             : const AVBitStreamFilter ff_h264_redundant_pps_bsf = {
     172             :     .name           = "h264_redundant_pps",
     173             :     .priv_data_size = sizeof(H264RedundantPPSContext),
     174             :     .init           = &h264_redundant_pps_init,
     175             :     .close          = &h264_redundant_pps_close,
     176             :     .filter         = &h264_redundant_pps_filter,
     177             :     .codec_ids      = h264_redundant_pps_codec_ids,
     178             : };

Generated by: LCOV version 1.13