Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
35 | 74 | static void wrapped_avframe_release_buffer(void *unused, uint8_t *data) | |
36 | { | ||
37 | 74 | AVFrame *frame = (AVFrame *)data; | |
38 | |||
39 | 74 | av_frame_free(&frame); | |
40 | 74 | } | |
41 | |||
42 | 74 | static int wrapped_avframe_encode(AVCodecContext *avctx, AVPacket *pkt, | |
43 | const AVFrame *frame, int *got_packet) | ||
44 | { | ||
45 | 74 | AVFrame *wrapped = av_frame_clone(frame); | |
46 | uint8_t *data; | ||
47 | 74 | int size = sizeof(*wrapped) + AV_INPUT_BUFFER_PADDING_SIZE; | |
48 | |||
49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (!wrapped) |
50 | ✗ | return AVERROR(ENOMEM); | |
51 | |||
52 | 74 | data = av_mallocz(size); | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (!data) { |
54 | ✗ | av_frame_free(&wrapped); | |
55 | ✗ | return AVERROR(ENOMEM); | |
56 | } | ||
57 | |||
58 | 74 | pkt->buf = av_buffer_create(data, size, | |
59 | wrapped_avframe_release_buffer, NULL, | ||
60 | AV_BUFFER_FLAG_READONLY); | ||
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (!pkt->buf) { |
62 | ✗ | av_frame_free(&wrapped); | |
63 | ✗ | av_freep(&data); | |
64 | ✗ | return AVERROR(ENOMEM); | |
65 | } | ||
66 | |||
67 | 74 | av_frame_move_ref((AVFrame*)data, wrapped); | |
68 | 74 | av_frame_free(&wrapped); | |
69 | |||
70 | 74 | pkt->data = data; | |
71 | 74 | pkt->size = sizeof(*wrapped); | |
72 | |||
73 | 74 | pkt->flags |= AV_PKT_FLAG_KEY; | |
74 | 74 | *got_packet = 1; | |
75 | 74 | return 0; | |
76 | } | ||
77 | |||
78 | 717 | static int wrapped_avframe_decode(AVCodecContext *avctx, AVFrame *out, | |
79 | int *got_frame, AVPacket *pkt) | ||
80 | { | ||
81 | AVFrame *in; | ||
82 | int err; | ||
83 | |||
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
|
717 | if (!(pkt->flags & AV_PKT_FLAG_TRUSTED)) { |
85 | // This decoder is not usable with untrusted input. | ||
86 | ✗ | return AVERROR(EPERM); | |
87 | } | ||
88 | |||
89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
|
717 | if (pkt->size < sizeof(AVFrame)) |
90 | ✗ | return AVERROR(EINVAL); | |
91 | |||
92 | 717 | in = (AVFrame*)pkt->data; | |
93 | |||
94 | 717 | err = av_frame_ref(out, in); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
|
717 | if (err < 0) |
96 | ✗ | return err; | |
97 | |||
98 | 717 | err = ff_decode_frame_props(avctx, out); | |
99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
|
717 | if (err < 0) |
100 | ✗ | return err; | |
101 | |||
102 | 717 | *got_frame = 1; | |
103 | 717 | return 0; | |
104 | } | ||
105 | |||
106 | const FFCodec ff_wrapped_avframe_encoder = { | ||
107 | .p.name = "wrapped_avframe", | ||
108 | CODEC_LONG_NAME("AVFrame to AVPacket passthrough"), | ||
109 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
110 | .p.id = AV_CODEC_ID_WRAPPED_AVFRAME, | ||
111 | .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
112 | FF_CODEC_ENCODE_CB(wrapped_avframe_encode), | ||
113 | }; | ||
114 | |||
115 | const FFCodec ff_wrapped_avframe_decoder = { | ||
116 | .p.name = "wrapped_avframe", | ||
117 | CODEC_LONG_NAME("AVPacket to AVFrame passthrough"), | ||
118 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
119 | .p.id = AV_CODEC_ID_WRAPPED_AVFRAME, | ||
120 | FF_CODEC_DECODE_CB(wrapped_avframe_decode), | ||
121 | }; | ||
122 |