FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/api/api-band-test.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 88 128 68.8%
Functions: 3 3 100.0%
Branches: 35 60 58.3%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 Ludmila Glinskih
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * draw_horiz_band test.
25 */
26
27 #include "libavutil/adler32.h"
28 #include "libavutil/mem.h"
29 #include "libavcodec/avcodec.h"
30 #include "libavformat/avformat.h"
31 #include "libavutil/imgutils.h"
32
33 uint8_t *slice_byte_buffer;
34 uint8_t slice_byte_buffer_size;
35 int draw_horiz_band_called;
36
37 3350 static void draw_horiz_band(AVCodecContext *ctx, const AVFrame *fr, int offset[4],
38 int slice_position, int type, int height)
39 {
40 int i;
41 const AVPixFmtDescriptor *pix_fmt_desc;
42 int chroma_w, chroma_h;
43 int shift_slice_position;
44 int shift_height;
45
46 3350 draw_horiz_band_called = 1;
47
48 3350 pix_fmt_desc = av_pix_fmt_desc_get(ctx->pix_fmt);
49 3350 chroma_w = -((-ctx->width) >> pix_fmt_desc->log2_chroma_w);
50 3350 chroma_h = -((-height) >> pix_fmt_desc->log2_chroma_h);
51 3350 shift_slice_position = -((-slice_position) >> pix_fmt_desc->log2_chroma_h);
52 3350 shift_height = -((-ctx->height) >> pix_fmt_desc->log2_chroma_h);
53
54
2/2
✓ Branch 0 taken 53400 times.
✓ Branch 1 taken 3350 times.
56750 for (i = 0; i < height; i++) {
55 53400 memcpy(slice_byte_buffer + ctx->width * slice_position + i * ctx->width,
56 53400 fr->data[0] + offset[0] + i * fr->linesize[0], ctx->width);
57 }
58
2/2
✓ Branch 0 taken 26700 times.
✓ Branch 1 taken 3350 times.
30050 for (i = 0; i < chroma_h; i++) {
59 26700 memcpy(slice_byte_buffer + ctx->width * ctx->height + chroma_w * shift_slice_position + i * chroma_w,
60 26700 fr->data[1] + offset[1] + i * fr->linesize[1], chroma_w);
61 }
62
2/2
✓ Branch 0 taken 26700 times.
✓ Branch 1 taken 3350 times.
30050 for (i = 0; i < chroma_h; i++) {
63 26700 memcpy(slice_byte_buffer + ctx->width * ctx->height + chroma_w * shift_height + chroma_w * shift_slice_position + i * chroma_w,
64 26700 fr->data[2] + offset[2] + i * fr->linesize[2], chroma_w);
65 }
66 3350 }
67
68 1 static int video_decode(const char *input_filename)
69 {
70 1 const AVCodec *codec = NULL;
71 1 AVCodecContext *ctx= NULL;
72 1 AVCodecParameters *origin_par = NULL;
73 1 uint8_t *byte_buffer = NULL;
74 1 AVFrame *fr = NULL;
75 AVPacket *pkt;
76 1 AVFormatContext *fmt_ctx = NULL;
77 int number_of_written_bytes;
78 int video_stream;
79 int byte_buffer_size;
80 int result;
81
82 1 draw_horiz_band_called = 0;
83
84 1 result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (result < 0) {
86 av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
87 return result;
88 }
89
90 1 result = avformat_find_stream_info(fmt_ctx, NULL);
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (result < 0) {
92 av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
93 return result;
94 }
95
96 1 video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (video_stream < 0) {
98 av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
99 return -1;
100 }
101
102 1 origin_par = fmt_ctx->streams[video_stream]->codecpar;
103
104 1 codec = avcodec_find_decoder(origin_par->codec_id);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!codec) {
106 av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
107 return -1;
108 }
109
110 1 ctx = avcodec_alloc_context3(codec);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ctx) {
112 av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
113 return AVERROR(ENOMEM);
114 }
115
116 1 result = avcodec_parameters_to_context(ctx, origin_par);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (result) {
118 av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
119 return result;
120 }
121
122 1 ctx->draw_horiz_band = draw_horiz_band;
123 1 ctx->thread_count = 1;
124
125 1 result = avcodec_open2(ctx, codec, NULL);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (result < 0) {
127 av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
128 return result;
129 }
130
131 1 fr = av_frame_alloc();
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fr) {
133 av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
134 return AVERROR(ENOMEM);
135 }
136
137 1 pkt = av_packet_alloc();
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pkt) {
139 av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
140 return AVERROR(ENOMEM);
141 }
142
143
2/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (strcmp(codec->name, "flv") && strcmp(codec->name, "mpeg4") && strcmp(codec->name, "huffyuv")) {
144 av_log(NULL, AV_LOG_ERROR, "Wrong codec\n");
145 return -1;
146 }
147
148 1 byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 32);
149 1 byte_buffer = av_malloc(byte_buffer_size);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!byte_buffer) {
151 av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
152 return AVERROR(ENOMEM);
153 }
154
155 1 slice_byte_buffer = av_malloc(byte_buffer_size);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!slice_byte_buffer) {
157 av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
158 return AVERROR(ENOMEM);
159 }
160 1 memset(slice_byte_buffer, 0, byte_buffer_size);
161 1 slice_byte_buffer_size = byte_buffer_size;
162
163 1 result = 0;
164
1/2
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
151 while (result >= 0) {
165 151 result = av_read_frame(fmt_ctx, pkt);
166
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 150 times.
151 if (result >= 0 && pkt->stream_index != video_stream) {
167 av_packet_unref(pkt);
168 continue;
169 }
170
171 // pkt will be empty on read error/EOF
172 151 result = avcodec_send_packet(ctx, pkt);
173
174 151 av_packet_unref(pkt);
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 if (result < 0) {
177 av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
178 return result;
179 }
180
181
1/2
✓ Branch 0 taken 301 times.
✗ Branch 1 not taken.
301 while (result >= 0) {
182 301 result = avcodec_receive_frame(ctx, fr);
183
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 300 times.
301 if (result == AVERROR_EOF)
184 1 goto finish;
185
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
300 else if (result == AVERROR(EAGAIN)) {
186 150 result = 0;
187 150 break;
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 } else if (result < 0) {
189 av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
190 return result;
191 }
192
193 150 number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
194 150 (const uint8_t* const *)fr->data, (const int*) fr->linesize,
195 150 ctx->pix_fmt, ctx->width, ctx->height, 1);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (number_of_written_bytes < 0) {
197 av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
198 return number_of_written_bytes;
199 }
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (draw_horiz_band_called == 0) {
201 av_log(NULL, AV_LOG_ERROR, "draw_horiz_band haven't been called!\n");
202 return -1;
203 }
204 150 if (av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes) !=
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 av_adler32_update(0, (const uint8_t*)slice_byte_buffer, number_of_written_bytes)) {
206 av_log(NULL, AV_LOG_ERROR, "Decoded frames with and without draw_horiz_band are not the same!\n");
207 return -1;
208 }
209 150 av_frame_unref(fr);
210 }
211 }
212
213 finish:
214 1 av_packet_free(&pkt);
215 1 av_frame_free(&fr);
216 1 avformat_close_input(&fmt_ctx);
217 1 avcodec_free_context(&ctx);
218 1 av_freep(&byte_buffer);
219 1 av_freep(&slice_byte_buffer);
220 1 return 0;
221 }
222
223 1 int main(int argc, char **argv)
224 {
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (argc < 2)
226 {
227 av_log(NULL, AV_LOG_ERROR, "Incorrect input: expected %s <name of a video file>\nNote that test works only for huffyuv, flv and mpeg4 decoders\n", argv[0]);
228 return 1;
229 }
230
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (video_decode(argv[1]) != 0)
232 return 1;
233
234 1 return 0;
235 }
236