3 * Copyright (c) 2015 Luca Barbato
5 * This file is part of FFmpeg.
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.
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.
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
24 * Simple wrapper to store an AVFrame and forward it as AVPacket.
31 #include "libavutil/internal.h"
32 #include "libavutil/frame.h"
33 #include "libavutil/buffer.h"
34 #include "libavutil/pixdesc.h"
36 static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
38 AVFrame *frame = (AVFrame *)data;
40 av_frame_free(&frame);
43 static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt,
44 const AVFrame *frame, int *got_packet)
46 AVFrame *wrapped = av_frame_clone(frame);
48 int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE;
51 return AVERROR(ENOMEM);
53 data = av_mallocz(size);
55 av_frame_free(&wrapped);
56 return AVERROR(ENOMEM);
59 pkt->buf = av_buffer_create(data, size,
60 wrapped_avframe_release_buffer, NULL,
61 AV_BUFFER_FLAG_READONLY);
63 av_frame_free(&wrapped);
65 return AVERROR(ENOMEM);
68 av_frame_move_ref((AVFrame*)data, wrapped);
69 av_frame_free(&wrapped);
72 pkt->size = sizeof(*wrapped);
74 pkt->flags |= AV_PKT_FLAG_KEY;
79 static int wrapped_avframe_decode(AVCodecContext *avctx, void *data,
80 int *got_frame, AVPacket *pkt)
85 if (!(pkt->flags & AV_PKT_FLAG_TRUSTED)) {
86 // This decoder is not usable with untrusted input.
87 return AVERROR(EPERM);
90 if (pkt->size < sizeof(AVFrame))
91 return AVERROR(EINVAL);
93 in = (AVFrame*)pkt->data;
96 err = ff_decode_frame_props(avctx, out);
100 av_frame_move_ref(out, in);
102 err = ff_attach_decode_data(out);
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,
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,