9d504dbd062251fe5db16a29253f8dc91144b997
[libav.git] / libavcodec / wrapped_avframe.c
1 /*
2 * AVFrame wrapper
3 * Copyright (c) 2015 Luca Barbato
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
42 static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt,
43 const AVFrame *frame, int *got_packet)
44 {
45 AVFrame *wrapped = av_frame_clone(frame);
46 int ret;
47
48 if (!wrapped)
49 return AVERROR(ENOMEM);
50
51 pkt->buf = av_buffer_create((uint8_t *)wrapped, sizeof(*wrapped),
52 wrapped_avframe_release_buffer, NULL,
53 AV_BUFFER_FLAG_READONLY);
54 if (!pkt->buf) {
55 av_frame_free(&wrapped);
56 return AVERROR(ENOMEM);
57 }
58
59 pkt->data = (uint8_t *)wrapped;
60 pkt->size = sizeof(*wrapped);
61
62 pkt->flags |= AV_PKT_FLAG_KEY;
63 *got_packet = 1;
64 return 0;
65 }
66
67 AVCodec ff_wrapped_avframe_encoder = {
68 .name = "wrapped_avframe",
69 .long_name = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
70 .type = AVMEDIA_TYPE_VIDEO,
71 .id = AV_CODEC_ID_WRAPPED_AVFRAME,
72 .encode2 = wrapped_avframe_encode,
73 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
74 };