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 "internal.h"
29 
30 #include "libavutil/internal.h"
31 #include "libavutil/frame.h"
32 #include "libavutil/buffer.h"
33 #include "libavutil/pixdesc.h"
34 
35 static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
36 {
37  AVFrame *frame = (AVFrame *)data;
38 
39  av_frame_free(&frame);
40 }
41 
43  const AVFrame *frame, int *got_packet)
44 {
45  AVFrame *wrapped = av_frame_clone(frame);
46  uint8_t *data;
47  int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE;
48 
49  if (!wrapped)
50  return AVERROR(ENOMEM);
51 
52  data = av_mallocz(size);
53  if (!data) {
54  av_frame_free(&wrapped);
55  return AVERROR(ENOMEM);
56  }
57 
58  pkt->buf = av_buffer_create(data, size,
61  if (!pkt->buf) {
62  av_frame_free(&wrapped);
63  av_freep(&data);
64  return AVERROR(ENOMEM);
65  }
66 
67  av_frame_move_ref((AVFrame*)data, wrapped);
68  av_frame_free(&wrapped);
69 
70  pkt->data = data;
71  pkt->size = sizeof(*wrapped);
72 
73  pkt->flags |= AV_PKT_FLAG_KEY;
74  *got_packet = 1;
75  return 0;
76 }
77 
79  .name = "wrapped_avframe",
80  .long_name = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
81  .type = AVMEDIA_TYPE_VIDEO,
83  .encode2 = wrapped_avframe_encode,
84  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
85 };
AVCodec ff_wrapped_avframe_encoder
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: avcodec.h:1658
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:524
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3681
#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
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
#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:1689
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:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1640
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
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:1663
reference-counted frame API
common internal API header
Passthrough codec, AVFrames wrapped in AVPacket.
Definition: avcodec.h:687
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:485
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1732
refcounted data buffer API
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:769
#define av_freep(p)
This structure stores compressed data.
Definition: avcodec.h:1634