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 | 19 | static int decode_read(DecodeContext *dc, int flush) | |
36 | { | ||
37 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
|
19 | const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN); |
38 | 19 | int ret = 0; | |
39 | |||
40 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | while (ret >= 0 && |
41 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
34 | (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames)) { |
42 | 34 | ret = avcodec_receive_frame(dc->decoder, dc->frame); | |
43 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
|
34 | if (ret < 0) { |
44 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15 times.
|
17 | if (ret == AVERROR_EOF) { |
45 | 2 | int err = dc->process_frame(dc, NULL); | |
46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (err < 0) |
47 | ✗ | return err; | |
48 | } | ||
49 | |||
50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | return (ret == ret_done) ? 0 : ret; |
51 | } | ||
52 | |||
53 | 17 | ret = dc->process_frame(dc, dc->frame); | |
54 | 17 | av_frame_unref(dc->frame); | |
55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (ret < 0) |
56 | ✗ | return ret; | |
57 | |||
58 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
|
17 | if (dc->max_frames && dc->decoder->frame_num == dc->max_frames) |
59 | 2 | return 1; | |
60 | } | ||
61 | |||
62 | ✗ | return (dc->max_frames == 0 || dc->decoder->frame_num < dc->max_frames) ? 0 : 1; | |
63 | } | ||
64 | |||
65 | 4 | int ds_run(DecodeContext *dc) | |
66 | { | ||
67 | int ret; | ||
68 | |||
69 | 4 | ret = avcodec_open2(dc->decoder, NULL, &dc->decoder_opts); | |
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
71 | ✗ | return ret; | |
72 | |||
73 |
1/2✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 | while (ret >= 0) { |
74 | 19 | ret = av_read_frame(dc->demuxer, dc->pkt); | |
75 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
|
19 | if (ret < 0) |
76 | 2 | break; | |
77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (dc->pkt->stream_index != dc->stream->index) { |
78 | ✗ | av_packet_unref(dc->pkt); | |
79 | ✗ | continue; | |
80 | } | ||
81 | |||
82 | 17 | ret = avcodec_send_packet(dc->decoder, dc->pkt); | |
83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (ret < 0) { |
84 | ✗ | fprintf(stderr, "Error decoding: %d\n", ret); | |
85 | ✗ | return ret; | |
86 | } | ||
87 | 17 | av_packet_unref(dc->pkt); | |
88 | |||
89 | 17 | ret = decode_read(dc, 0); | |
90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (ret < 0) { |
91 | ✗ | fprintf(stderr, "Error decoding: %d\n", ret); | |
92 | ✗ | return ret; | |
93 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15 times.
|
17 | } else if (ret > 0) |
94 | 2 | goto finish; | |
95 | } | ||
96 | |||
97 | 2 | avcodec_send_packet(dc->decoder, NULL); | |
98 | 2 | ret = decode_read(dc, 1); | |
99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
100 | ✗ | fprintf(stderr, "Error flushing: %d\n", ret); | |
101 | ✗ | return ret; | |
102 | } | ||
103 | |||
104 | 2 | finish: | |
105 | 4 | return dc->process_frame(dc, NULL); | |
106 | } | ||
107 | |||
108 | 4 | void ds_free(DecodeContext *dc) | |
109 | { | ||
110 | 4 | av_dict_free(&dc->decoder_opts); | |
111 | |||
112 | 4 | av_frame_free(&dc->frame); | |
113 | 4 | av_packet_free(&dc->pkt); | |
114 | |||
115 | 4 | avcodec_free_context(&dc->decoder); | |
116 | 4 | avformat_close_input(&dc->demuxer); | |
117 | 4 | } | |
118 | |||
119 | 4 | int ds_open(DecodeContext *dc, const char *url, int stream_idx) | |
120 | { | ||
121 | const AVCodec *codec; | ||
122 | int ret; | ||
123 | |||
124 | 4 | memset(dc, 0, sizeof(*dc)); | |
125 | |||
126 | 4 | dc->pkt = av_packet_alloc(); | |
127 | 4 | dc->frame = av_frame_alloc(); | |
128 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (!dc->pkt || !dc->frame) { |
129 | ✗ | ret = AVERROR(ENOMEM); | |
130 | ✗ | goto fail; | |
131 | } | ||
132 | |||
133 | 4 | ret = avformat_open_input(&dc->demuxer, url, NULL, NULL); | |
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) { |
135 | ✗ | fprintf(stderr, "Error opening input file: %d\n", ret); | |
136 | ✗ | return ret; | |
137 | } | ||
138 | |||
139 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams) |
140 | ✗ | return AVERROR(EINVAL); | |
141 | |||
142 | 4 | dc->stream = dc->demuxer->streams[stream_idx]; | |
143 | |||
144 | 4 | codec = avcodec_find_decoder(dc->stream->codecpar->codec_id); | |
145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!codec) |
146 | ✗ | return AVERROR_DECODER_NOT_FOUND; | |
147 | |||
148 | 4 | dc->decoder = avcodec_alloc_context3(codec); | |
149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!dc->decoder) |
150 | ✗ | return AVERROR(ENOMEM); | |
151 | |||
152 | 4 | ret = avcodec_parameters_to_context(dc->decoder, dc->stream->codecpar); | |
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
154 | ✗ | goto fail; | |
155 | |||
156 | 4 | return 0; | |
157 | |||
158 | ✗ | fail: | |
159 | ✗ | ds_free(dc); | |
160 | ✗ | return ret; | |
161 | } | ||
162 |