LCOV - code coverage report
Current view: top level - libavcodec - wrapped_avframe.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 42 45.2 %
Date: 2017-12-14 01:15:32 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  * AVFrame wrapper
       3             :  * Copyright (c) 2015 Luca Barbato
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * Simple wrapper to store an AVFrame and forward it as AVPacket.
      25             :  */
      26             : 
      27             : #include "avcodec.h"
      28             : #include "decode.h"
      29             : #include "internal.h"
      30             : 
      31             : #include "libavutil/internal.h"
      32             : #include "libavutil/frame.h"
      33             : #include "libavutil/buffer.h"
      34             : #include "libavutil/pixdesc.h"
      35             : 
      36          45 : static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
      37             : {
      38          45 :     AVFrame *frame = (AVFrame *)data;
      39             : 
      40          45 :     av_frame_free(&frame);
      41          45 : }
      42             : 
      43          45 : static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt,
      44             :                      const AVFrame *frame, int *got_packet)
      45             : {
      46          45 :     AVFrame *wrapped = av_frame_clone(frame);
      47             :     uint8_t *data;
      48          45 :     int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE;
      49             : 
      50          45 :     if (!wrapped)
      51           0 :         return AVERROR(ENOMEM);
      52             : 
      53          45 :     data = av_mallocz(size);
      54          45 :     if (!data) {
      55           0 :         av_frame_free(&wrapped);
      56           0 :         return AVERROR(ENOMEM);
      57             :     }
      58             : 
      59          45 :     pkt->buf = av_buffer_create(data, size,
      60             :                                 wrapped_avframe_release_buffer, NULL,
      61             :                                 AV_BUFFER_FLAG_READONLY);
      62          45 :     if (!pkt->buf) {
      63           0 :         av_frame_free(&wrapped);
      64           0 :         av_freep(&data);
      65           0 :         return AVERROR(ENOMEM);
      66             :     }
      67             : 
      68          45 :     av_frame_move_ref((AVFrame*)data, wrapped);
      69          45 :     av_frame_free(&wrapped);
      70             : 
      71          45 :     pkt->data = data;
      72          45 :     pkt->size = sizeof(*wrapped);
      73             : 
      74          45 :     pkt->flags |= AV_PKT_FLAG_KEY;
      75          45 :     *got_packet = 1;
      76          45 :     return 0;
      77             : }
      78             : 
      79           0 : static int wrapped_avframe_decode(AVCodecContext *avctx, void *data,
      80             :                                   int *got_frame, AVPacket *pkt)
      81             : {
      82             :     AVFrame *in, *out;
      83             :     int err;
      84             : 
      85           0 :     if (!(pkt->flags & AV_PKT_FLAG_TRUSTED)) {
      86             :         // This decoder is not usable with untrusted input.
      87           0 :         return AVERROR(EPERM);
      88             :     }
      89             : 
      90           0 :     if (pkt->size < sizeof(AVFrame))
      91           0 :         return AVERROR(EINVAL);
      92             : 
      93           0 :     in  = (AVFrame*)pkt->data;
      94           0 :     out = data;
      95             : 
      96           0 :     err = ff_decode_frame_props(avctx, out);
      97           0 :     if (err < 0)
      98           0 :         return err;
      99             : 
     100           0 :     av_frame_move_ref(out, in);
     101             : 
     102           0 :     err = ff_attach_decode_data(out);
     103           0 :     if (err < 0) {
     104           0 :         av_frame_unref(out);
     105           0 :         return err;
     106             :     }
     107             : 
     108           0 :     *got_frame = 1;
     109           0 :     return 0;
     110             : }
     111             : 
     112             : AVCodec ff_wrapped_avframe_encoder = {
     113             :     .name           = "wrapped_avframe",
     114             :     .long_name      = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
     115             :     .type           = AVMEDIA_TYPE_VIDEO,
     116             :     .id             = AV_CODEC_ID_WRAPPED_AVFRAME,
     117             :     .encode2        = wrapped_avframe_encode,
     118             :     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
     119             : };
     120             : 
     121             : AVCodec ff_wrapped_avframe_decoder = {
     122             :     .name           = "wrapped_avframe",
     123             :     .long_name      = NULL_IF_CONFIG_SMALL("AVPacket to AVFrame passthrough"),
     124             :     .type           = AVMEDIA_TYPE_VIDEO,
     125             :     .id             = AV_CODEC_ID_WRAPPED_AVFRAME,
     126             :     .decode         = wrapped_avframe_decode,
     127             :     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
     128             : };

Generated by: LCOV version 1.13