FFmpeg
Loading...
Searching...
No Matches
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 "codec_internal.h"
29#include "decode.h"
30
31#include "libavutil/frame.h"
32#include "libavutil/buffer.h"
33#include "libavutil/mem.h"
34
35static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
36{
38
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
53 if (!data) {
54 av_frame_free(&wrapped);
55 return AVERROR(ENOMEM);
56 }
57
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 int *got_frame, AVPacket *pkt)
80{
81 AVFrame *in;
82 int err;
83
84 if (!(pkt->flags & AV_PKT_FLAG_TRUSTED)) {
85 // This decoder is not usable with untrusted input.
86 return AVERROR(EPERM);
87 }
88
89 if (pkt->size < sizeof(AVFrame))
90 return AVERROR(EINVAL);
91
92 in = (AVFrame*)pkt->data;
93
94 err = av_frame_ref(out, in);
95 if (err < 0)
96 return err;
97
98 err = ff_decode_frame_props(avctx, out);
99 if (err < 0)
100 return err;
101
102 *got_frame = 1;
103 return 0;
104}
105
107 .p.name = "wrapped_avframe",
108 CODEC_LONG_NAME("AVFrame to AVPacket passthrough"),
109 .p.type = AVMEDIA_TYPE_VIDEO,
113};
114
116 .p.name = "wrapped_avframe",
117 CODEC_LONG_NAME("AVPacket to AVFrame passthrough"),
118 .p.type = AVMEDIA_TYPE_VIDEO,
121};
const FFCodec ff_wrapped_avframe_decoder
const FFCodec ff_wrapped_avframe_encoder
static FILE * out
Libavcodec external API header.
refcounted data buffer API
#define FF_CODEC_DECODE_CB(func)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition decode.c:1598
static AVPacket * pkt
static AVFrame * frame
reference-counted frame API
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
@ AV_CODEC_ID_WRAPPED_AVFRAME
Passthrough codec, AVFrames wrapped in AVPacket.
Definition codec_id.h:621
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AV_PKT_FLAG_TRUSTED
The packet comes from a trusted source.
Definition packet.h:664
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition buffer.h:114
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
#define AVERROR(e)
Definition error.h:45
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:523
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
Memory handling functions.
const char data[16]
Definition mxf.c:149
main external API structure.
Definition avcodec.h:443
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
#define av_mallocz(s)
#define av_freep(p)
int size
static int wrapped_avframe_decode(AVCodecContext *avctx, AVFrame *out, int *got_frame, AVPacket *pkt)
static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)