../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
60 / 83
72.3%
Branches:
36 / 60
60.0%
Functions:
4 / 4
100.0%

tools/decode_simple.c
Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /* shared code for simple demux/decode tools */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "decode_simple.h"
25
26 #include "libavformat/avformat.h"
27
28 #include "libavcodec/avcodec.h"
29 #include "libavcodec/packet.h"
30
31 #include "libavutil/dict.h"
32 #include "libavutil/error.h"
33 #include "libavutil/frame.h"
34
35 55 static int decode_read(DecodeContext *dc, int flush)
36 {
37
2/2
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 4 taken 51 times.
55 const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN);
38 55 int ret = 0;
39
40
1/2
✓ Branch 24 → 25 taken 104 times.
✗ Branch 24 → 27 not taken.
104 while (ret >= 0 &&
41
3/4
✓ Branch 25 → 6 taken 94 times.
✓ Branch 25 → 26 taken 10 times.
✓ Branch 26 → 6 taken 10 times.
✗ Branch 26 → 27 not taken.
104 (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames)) {
42 104 ret = avcodec_receive_frame(dc->decoder, dc->frame);
43
2/2
✓ Branch 7 → 8 taken 53 times.
✓ Branch 7 → 16 taken 51 times.
104 if (ret < 0) {
44
2/2
✓ Branch 8 → 9 taken 4 times.
✓ Branch 8 → 12 taken 49 times.
53 if (ret == AVERROR_EOF) {
45 4 int err = dc->process_frame(dc, NULL);
46
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 4 times.
4 if (err < 0)
47 ✗ return err;
48 }
49
50
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 53 times.
53 return (ret == ret_done) ? 0 : ret;
51 }
52
53 51 ret = dc->process_frame(dc, dc->frame);
54 51 av_frame_unref(dc->frame);
55
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 51 times.
51 if (ret < 0)
56 ✗ return ret;
57
58
4/4
✓ Branch 20 → 21 taken 6 times.
✓ Branch 20 → 23 taken 45 times.
✓ Branch 21 → 22 taken 2 times.
✓ Branch 21 → 23 taken 4 times.
51 if (dc->max_frames && dc->decoder->frame_num == dc->max_frames)
59 2 return 1;
60 }
61
62
0/4
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 30 not taken.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ return (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames) ? 0 : 1;
63 }
64
65 6 int ds_run(DecodeContext *dc)
66 {
67 int ret;
68
69 6 ret = avcodec_open2(dc->decoder, NULL, &dc->decoder_opts);
70
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 6 times.
6 if (ret < 0)
71 ✗ return ret;
72
73
1/2
✓ Branch 24 → 6 taken 55 times.
✗ Branch 24 → 25 not taken.
55 while (ret >= 0) {
74 55 ret = av_read_frame(dc->demuxer, dc->pkt);
75
2/2
✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 9 taken 51 times.
55 if (ret < 0)
76 4 break;
77
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 51 times.
51 if (dc->pkt->stream_index != dc->stream->index) {
78 ✗ av_packet_unref(dc->pkt);
79 ✗ continue;
80 }
81
82 51 ret = avcodec_send_packet(dc->decoder, dc->pkt);
83
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 51 times.
51 if (ret < 0) {
84 ✗ fprintf(stderr, "Error decoding: %d\n", ret);
85 ✗ return ret;
86 }
87 51 av_packet_unref(dc->pkt);
88
89 51 ret = decode_read(dc, 0);
90
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 51 times.
51 if (ret < 0) {
91 ✗ fprintf(stderr, "Error decoding: %d\n", ret);
92 ✗ return ret;
93
2/2
✓ Branch 21 → 22 taken 2 times.
✓ Branch 21 → 23 taken 49 times.
51 } else if (ret > 0)
94 2 goto finish;
95 }
96
97 4 ret = avcodec_send_packet(dc->decoder, NULL);
98
1/2
✓ Branch 26 → 27 taken 4 times.
✗ Branch 26 → 28 not taken.
4 if (ret >= 0)
99 4 ret = decode_read(dc, 1);
100
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 4 times.
4 if (ret < 0) {
101 ✗ fprintf(stderr, "Error flushing: %d\n", ret);
102 ✗ return ret;
103 }
104
105 4 finish:
106 6 return dc->process_frame(dc, NULL);
107 }
108
109 6 void ds_free(DecodeContext *dc)
110 {
111 6 av_dict_free(&dc->decoder_opts);
112
113 6 av_frame_free(&dc->frame);
114 6 av_packet_free(&dc->pkt);
115
116 6 avcodec_free_context(&dc->decoder);
117 6 avformat_close_input(&dc->demuxer);
118 6 }
119
120 6 int ds_open(DecodeContext *dc, const char *url, int stream_idx)
121 {
122 const AVCodec *codec;
123 int ret;
124
125 6 memset(dc, 0, sizeof(*dc));
126
127 6 dc->pkt = av_packet_alloc();
128 6 dc->frame = av_frame_alloc();
129
2/4
✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 6 times.
6 if (!dc->pkt || !dc->frame) {
130 ✗ ret = AVERROR(ENOMEM);
131 ✗ goto fail;
132 }
133
134 6 ret = avformat_open_input(&dc->demuxer, url, NULL, NULL);
135
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 6 times.
6 if (ret < 0) {
136 ✗ fprintf(stderr, "Error opening input file: %d\n", ret);
137 ✗ return ret;
138 }
139
140
2/4
✓ Branch 11 → 12 taken 6 times.
✗ Branch 11 → 13 not taken.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 6 times.
6 if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams)
141 ✗ return AVERROR(EINVAL);
142
143 6 dc->stream = dc->demuxer->streams[stream_idx];
144
145 6 codec = avcodec_find_decoder(dc->stream->codecpar->codec_id);
146
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 6 times.
6 if (!codec)
147 ✗ return AVERROR_DECODER_NOT_FOUND;
148
149 6 dc->decoder = avcodec_alloc_context3(codec);
150
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 6 times.
6 if (!dc->decoder)
151 ✗ return AVERROR(ENOMEM);
152
153 6 ret = avcodec_parameters_to_context(dc->decoder, dc->stream->codecpar);
154
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 6 times.
6 if (ret < 0)
155 ✗ goto fail;
156
157 6 return 0;
158
159 ✗ fail:
160 ✗ ds_free(dc);
161 ✗ return ret;
162 }
163