LCOV - code coverage report
Current view: top level - libavcodec - bitstream_filter.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 91 0.0 %
Date: 2017-12-15 18:13:28 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <string.h>
      22             : 
      23             : #include "avcodec.h"
      24             : #include "libavutil/internal.h"
      25             : #include "libavutil/mem.h"
      26             : #include "libavutil/opt.h"
      27             : 
      28             : #if FF_API_OLD_BSF
      29             : FF_DISABLE_DEPRECATION_WARNINGS
      30             : 
      31           0 : const AVBitStreamFilter *av_bitstream_filter_next(const AVBitStreamFilter *f)
      32             : {
      33           0 :     const AVBitStreamFilter *filter = NULL;
      34           0 :     void *opaque = NULL;
      35             : 
      36           0 :     while (filter != f)
      37           0 :         filter = av_bsf_next(&opaque);
      38             : 
      39           0 :     return av_bsf_next(&opaque);
      40             : }
      41             : 
      42           0 : void av_register_bitstream_filter(AVBitStreamFilter *bsf)
      43             : {
      44           0 : }
      45             : 
      46             : typedef struct BSFCompatContext {
      47             :     AVBSFContext *ctx;
      48             :     int extradata_updated;
      49             : } BSFCompatContext;
      50             : 
      51           0 : AVBitStreamFilterContext *av_bitstream_filter_init(const char *name)
      52             : {
      53           0 :     AVBitStreamFilterContext *ctx = NULL;
      54           0 :     BSFCompatContext         *priv = NULL;
      55             :     const AVBitStreamFilter *bsf;
      56             : 
      57           0 :     bsf = av_bsf_get_by_name(name);
      58           0 :     if (!bsf)
      59           0 :         return NULL;
      60             : 
      61           0 :     ctx = av_mallocz(sizeof(*ctx));
      62           0 :     if (!ctx)
      63           0 :         return NULL;
      64             : 
      65           0 :     priv = av_mallocz(sizeof(*priv));
      66           0 :     if (!priv)
      67           0 :         goto fail;
      68             : 
      69             : 
      70           0 :     ctx->filter    = bsf;
      71           0 :     ctx->priv_data = priv;
      72             : 
      73           0 :     return ctx;
      74             : 
      75           0 : fail:
      76           0 :     if (priv)
      77           0 :         av_bsf_free(&priv->ctx);
      78           0 :     av_freep(&priv);
      79           0 :     av_freep(&ctx);
      80           0 :     return NULL;
      81             : }
      82             : 
      83           0 : void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc)
      84             : {
      85             :     BSFCompatContext *priv;
      86             : 
      87           0 :     if (!bsfc)
      88           0 :         return;
      89             : 
      90           0 :     priv = bsfc->priv_data;
      91             : 
      92           0 :     av_bsf_free(&priv->ctx);
      93           0 :     av_freep(&bsfc->priv_data);
      94           0 :     av_free(bsfc);
      95             : }
      96             : 
      97           0 : int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
      98             :                                AVCodecContext *avctx, const char *args,
      99             :                                uint8_t **poutbuf, int *poutbuf_size,
     100             :                                const uint8_t *buf, int buf_size, int keyframe)
     101             : {
     102           0 :     BSFCompatContext *priv = bsfc->priv_data;
     103           0 :     AVPacket pkt = { 0 };
     104             :     int ret;
     105             : 
     106           0 :     if (!priv->ctx) {
     107           0 :         ret = av_bsf_alloc(bsfc->filter, &priv->ctx);
     108           0 :         if (ret < 0)
     109           0 :             return ret;
     110             : 
     111           0 :         ret = avcodec_parameters_from_context(priv->ctx->par_in, avctx);
     112           0 :         if (ret < 0)
     113           0 :             return ret;
     114             : 
     115           0 :         priv->ctx->time_base_in = avctx->time_base;
     116             : 
     117           0 :         if (bsfc->args && bsfc->filter->priv_class) {
     118           0 :             const AVOption *opt = av_opt_next(priv->ctx->priv_data, NULL);
     119           0 :             const char * shorthand[2] = {NULL};
     120             : 
     121           0 :             if (opt)
     122           0 :                 shorthand[0] = opt->name;
     123             : 
     124           0 :             ret = av_opt_set_from_string(priv->ctx->priv_data, bsfc->args, shorthand, "=", ":");
     125           0 :             if (ret < 0)
     126           0 :                 return ret;
     127             :         }
     128             : 
     129           0 :         ret = av_bsf_init(priv->ctx);
     130           0 :         if (ret < 0)
     131           0 :             return ret;
     132             :     }
     133             : 
     134           0 :     pkt.data = (uint8_t *)buf;
     135           0 :     pkt.size = buf_size;
     136             : 
     137           0 :     ret = av_bsf_send_packet(priv->ctx, &pkt);
     138           0 :     if (ret < 0)
     139           0 :         return ret;
     140             : 
     141           0 :     *poutbuf      = NULL;
     142           0 :     *poutbuf_size = 0;
     143             : 
     144           0 :     ret = av_bsf_receive_packet(priv->ctx, &pkt);
     145           0 :     if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
     146           0 :         return 0;
     147           0 :     else if (ret < 0)
     148           0 :         return ret;
     149             : 
     150           0 :     *poutbuf = av_malloc(pkt.size + AV_INPUT_BUFFER_PADDING_SIZE);
     151           0 :     if (!*poutbuf) {
     152           0 :         av_packet_unref(&pkt);
     153           0 :         return AVERROR(ENOMEM);
     154             :     }
     155             : 
     156           0 :     *poutbuf_size = pkt.size;
     157           0 :     memcpy(*poutbuf, pkt.data, pkt.size);
     158             : 
     159           0 :     av_packet_unref(&pkt);
     160             : 
     161             :     /* drain all the remaining packets we cannot return */
     162           0 :     while (ret >= 0) {
     163           0 :         ret = av_bsf_receive_packet(priv->ctx, &pkt);
     164           0 :         av_packet_unref(&pkt);
     165             :     }
     166             : 
     167           0 :     if (!priv->extradata_updated) {
     168             :         /* update extradata in avctx from the output codec parameters */
     169           0 :         if (priv->ctx->par_out->extradata_size && (!args || !strstr(args, "private_spspps_buf"))) {
     170           0 :             av_freep(&avctx->extradata);
     171           0 :             avctx->extradata_size = 0;
     172           0 :             avctx->extradata = av_mallocz(priv->ctx->par_out->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
     173           0 :             if (!avctx->extradata)
     174           0 :                 return AVERROR(ENOMEM);
     175           0 :             memcpy(avctx->extradata, priv->ctx->par_out->extradata, priv->ctx->par_out->extradata_size);
     176           0 :             avctx->extradata_size = priv->ctx->par_out->extradata_size;
     177             :         }
     178             : 
     179           0 :         priv->extradata_updated = 1;
     180             :     }
     181             : 
     182           0 :     return 1;
     183             : }
     184             : FF_ENABLE_DEPRECATION_WARNINGS
     185             : #endif

Generated by: LCOV version 1.13