FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wrapped_avframe.c
Go to the documentation of this file.
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 static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
37 {
38  AVFrame *frame = (AVFrame *)data;
39 
40  av_frame_free(&frame);
41 }
42 
44  const AVFrame *frame, int *got_packet)
45 {
46  AVFrame *wrapped = av_frame_clone(frame);
47  uint8_t *data;
48  int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE;
49 
50  if (!wrapped)
51  return AVERROR(ENOMEM);
52 
53  data = av_mallocz(size);
54  if (!data) {
55  av_frame_free(&wrapped);
56  return AVERROR(ENOMEM);
57  }
58 
59  pkt->buf = av_buffer_create(data, size,
62  if (!pkt->buf) {
63  av_frame_free(&wrapped);
64  av_freep(&data);
65  return AVERROR(ENOMEM);
66  }
67 
68  av_frame_move_ref((AVFrame*)data, wrapped);
69  av_frame_free(&wrapped);
70 
71  pkt->data = data;
72  pkt->size = sizeof(*wrapped);
73 
74  pkt->flags |= AV_PKT_FLAG_KEY;
75  *got_packet = 1;
76  return 0;
77 }
78 
79 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  if (!(pkt->flags & AV_PKT_FLAG_TRUSTED)) {
86  // This decoder is not usable with untrusted input.
87  return AVERROR(EPERM);
88  }
89 
90  if (pkt->size < sizeof(AVFrame))
91  return AVERROR(EINVAL);
92 
93  in = (AVFrame*)pkt->data;
94  out = data;
95 
96  err = ff_decode_frame_props(avctx, out);
97  if (err < 0)
98  return err;
99 
100  av_frame_move_ref(out, in);
101 
102  err = ff_attach_decode_data(out);
103  if (err < 0) {
104  av_frame_unref(out);
105  return err;
106  }
107 
108  *got_frame = 1;
109  return 0;
110 }
111 
113  .name = "wrapped_avframe",
114  .long_name = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
115  .type = AVMEDIA_TYPE_VIDEO,
117  .encode2 = wrapped_avframe_encode,
118  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
119 };
120 
122  .name = "wrapped_avframe",
123  .long_name = NULL_IF_CONFIG_SMALL("AVPacket to AVFrame passthrough"),
124  .type = AVMEDIA_TYPE_VIDEO,
126  .decode = wrapped_avframe_decode,
127  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
128 };
AVCodec ff_wrapped_avframe_encoder
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: avcodec.h:1431
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:580
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition: decode.c:1665
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3408
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
int ff_attach_decode_data(AVFrame *frame)
Definition: decode.c:1809
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1430
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
ptrdiff_t size
Definition: opengl_enc.c:101
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1462
static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1413
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1436
reference-counted frame API
common internal API header
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: avcodec.h:691
static int wrapped_avframe_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:538
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1518
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
refcounted data buffer API
#define AV_PKT_FLAG_TRUSTED
The packet comes from a trusted source.
Definition: avcodec.h:1476
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:551
common internal api header.
static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
AVCodec ff_wrapped_avframe_decoder
FILE * out
Definition: movenc.c:54
#define av_freep(p)
This structure stores compressed data.
Definition: avcodec.h:1407