FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wrapped_avframe.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 29 39 74.4%
Functions: 3 3 100.0%
Branches: 7 14 50.0%

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