FFmpeg
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 
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 
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 
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) {
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
AVCodec.
Definition: codec.h:202
FF_CODEC_CAP_INIT_THREADSAFE
#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:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:54
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
ff_wrapped_avframe_encoder
const AVCodec ff_wrapped_avframe_encoder
Definition: wrapped_avframe.c:112
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_BUFFER_FLAG_READONLY
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:114
decode.h
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:422
AV_CODEC_ID_WRAPPED_AVFRAME
@ AV_CODEC_ID_WRAPPED_AVFRAME
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: codec_id.h:572
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:356
NULL
#define NULL
Definition: coverity.c:32
wrapped_avframe_decode
static int wrapped_avframe_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
Definition: wrapped_avframe.c:79
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
wrapped_avframe_release_buffer
static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
Definition: wrapped_avframe.c:36
frame.h
buffer.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
wrapped_avframe_encode
static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: wrapped_avframe.c:43
internal.h
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:462
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:435
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
avcodec.h
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ff_decode_frame_props
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition: decode.c:1489
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_wrapped_avframe_decoder
const AVCodec ff_wrapped_avframe_decoder
Definition: wrapped_avframe.c:121
ff_attach_decode_data
int ff_attach_decode_data(AVFrame *frame)
Definition: decode.c:1628
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AV_PKT_FLAG_TRUSTED
#define AV_PKT_FLAG_TRUSTED
The packet comes from a trusted source.
Definition: packet.h:442