Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * generic decoding-related code | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #include <stdint.h> | ||
22 | #include <string.h> | ||
23 | |||
24 | #include "config.h" | ||
25 | |||
26 | #if CONFIG_ICONV | ||
27 | # include <iconv.h> | ||
28 | #endif | ||
29 | |||
30 | #include "libavutil/avassert.h" | ||
31 | #include "libavutil/avstring.h" | ||
32 | #include "libavutil/bprint.h" | ||
33 | #include "libavutil/channel_layout.h" | ||
34 | #include "libavutil/common.h" | ||
35 | #include "libavutil/fifo.h" | ||
36 | #include "libavutil/frame.h" | ||
37 | #include "libavutil/hwcontext.h" | ||
38 | #include "libavutil/imgutils.h" | ||
39 | #include "libavutil/internal.h" | ||
40 | #include "libavutil/intmath.h" | ||
41 | #include "libavutil/opt.h" | ||
42 | |||
43 | #include "avcodec.h" | ||
44 | #include "bytestream.h" | ||
45 | #include "bsf.h" | ||
46 | #include "codec_internal.h" | ||
47 | #include "decode.h" | ||
48 | #include "hwconfig.h" | ||
49 | #include "internal.h" | ||
50 | #include "thread.h" | ||
51 | |||
52 | 393262 | static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt) | |
53 | { | ||
54 | int ret; | ||
55 | size_t size; | ||
56 | const uint8_t *data; | ||
57 | uint32_t flags; | ||
58 | int64_t val; | ||
59 | |||
60 | 393262 | data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size); | |
61 |
2/2✓ Branch 0 taken 393260 times.
✓ Branch 1 taken 2 times.
|
393262 | if (!data) |
62 | 393260 | return 0; | |
63 | |||
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) { |
65 | ✗ | av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter " | |
66 | "changes, but PARAM_CHANGE side data was sent to it.\n"); | ||
67 | ✗ | ret = AVERROR(EINVAL); | |
68 | ✗ | goto fail2; | |
69 | } | ||
70 | |||
71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (size < 4) |
72 | ✗ | goto fail; | |
73 | |||
74 | 2 | flags = bytestream_get_le32(&data); | |
75 | 2 | size -= 4; | |
76 | |||
77 | #if FF_API_OLD_CHANNEL_LAYOUT | ||
78 | FF_DISABLE_DEPRECATION_WARNINGS | ||
79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) { |
80 | ✗ | if (size < 4) | |
81 | ✗ | goto fail; | |
82 | ✗ | val = bytestream_get_le32(&data); | |
83 | ✗ | if (val <= 0 || val > INT_MAX) { | |
84 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid channel count"); | |
85 | ✗ | ret = AVERROR_INVALIDDATA; | |
86 | ✗ | goto fail2; | |
87 | } | ||
88 | ✗ | avctx->channels = val; | |
89 | ✗ | size -= 4; | |
90 | } | ||
91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) { |
92 | ✗ | if (size < 8) | |
93 | ✗ | goto fail; | |
94 | ✗ | avctx->channel_layout = bytestream_get_le64(&data); | |
95 | ✗ | size -= 8; | |
96 | } | ||
97 | FF_ENABLE_DEPRECATION_WARNINGS | ||
98 | #endif | ||
99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) { |
100 | ✗ | if (size < 4) | |
101 | ✗ | goto fail; | |
102 | ✗ | val = bytestream_get_le32(&data); | |
103 | ✗ | if (val <= 0 || val > INT_MAX) { | |
104 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid sample rate"); | |
105 | ✗ | ret = AVERROR_INVALIDDATA; | |
106 | ✗ | goto fail2; | |
107 | } | ||
108 | ✗ | avctx->sample_rate = val; | |
109 | ✗ | size -= 4; | |
110 | } | ||
111 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) { |
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (size < 8) |
113 | ✗ | goto fail; | |
114 | 2 | avctx->width = bytestream_get_le32(&data); | |
115 | 2 | avctx->height = bytestream_get_le32(&data); | |
116 | 2 | size -= 8; | |
117 | 2 | ret = ff_set_dimensions(avctx, avctx->width, avctx->height); | |
118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
119 | ✗ | goto fail2; | |
120 | } | ||
121 | |||
122 | 2 | return 0; | |
123 | ✗ | fail: | |
124 | ✗ | av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n"); | |
125 | ✗ | ret = AVERROR_INVALIDDATA; | |
126 | ✗ | fail2: | |
127 | ✗ | if (ret < 0) { | |
128 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n"); | |
129 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
130 | ✗ | return ret; | |
131 | } | ||
132 | ✗ | return 0; | |
133 | } | ||
134 | |||
135 | 393262 | static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt) | |
136 | { | ||
137 | 393262 | int ret = 0; | |
138 | |||
139 | 393262 | av_packet_unref(avci->last_pkt_props); | |
140 |
1/2✓ Branch 0 taken 393262 times.
✗ Branch 1 not taken.
|
393262 | if (pkt) { |
141 | 393262 | ret = av_packet_copy_props(avci->last_pkt_props, pkt); | |
142 | #if FF_API_FRAME_PKT | ||
143 |
1/2✓ Branch 0 taken 393262 times.
✗ Branch 1 not taken.
|
393262 | if (!ret) |
144 | 393262 | avci->last_pkt_props->stream_index = pkt->size; // Needed for ff_decode_frame_props(). | |
145 | #endif | ||
146 | } | ||
147 | 393262 | return ret; | |
148 | } | ||
149 | |||
150 | 13854 | static int decode_bsfs_init(AVCodecContext *avctx) | |
151 | { | ||
152 | 13854 | AVCodecInternal *avci = avctx->internal; | |
153 | 13854 | const FFCodec *const codec = ffcodec(avctx->codec); | |
154 | int ret; | ||
155 | |||
156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13854 times.
|
13854 | if (avci->bsf) |
157 | ✗ | return 0; | |
158 | |||
159 | 13854 | ret = av_bsf_list_parse_str(codec->bsfs, &avci->bsf); | |
160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13854 times.
|
13854 | if (ret < 0) { |
161 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", codec->bsfs, av_err2str(ret)); | |
162 | ✗ | if (ret != AVERROR(ENOMEM)) | |
163 | ✗ | ret = AVERROR_BUG; | |
164 | ✗ | goto fail; | |
165 | } | ||
166 | |||
167 | /* We do not currently have an API for passing the input timebase into decoders, | ||
168 | * but no filters used here should actually need it. | ||
169 | * So we make up some plausible-looking number (the MPEG 90kHz timebase) */ | ||
170 | 13854 | avci->bsf->time_base_in = (AVRational){ 1, 90000 }; | |
171 | 13854 | ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx); | |
172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13854 times.
|
13854 | if (ret < 0) |
173 | ✗ | goto fail; | |
174 | |||
175 | 13854 | ret = av_bsf_init(avci->bsf); | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13854 times.
|
13854 | if (ret < 0) |
177 | ✗ | goto fail; | |
178 | |||
179 | 13854 | return 0; | |
180 | ✗ | fail: | |
181 | ✗ | av_bsf_free(&avci->bsf); | |
182 | ✗ | return ret; | |
183 | } | ||
184 | |||
185 | 794751 | int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt) | |
186 | { | ||
187 | 794751 | AVCodecInternal *avci = avctx->internal; | |
188 | int ret; | ||
189 | |||
190 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 794748 times.
|
794751 | if (avci->draining) |
191 | 3 | return AVERROR_EOF; | |
192 | |||
193 | 794748 | ret = av_bsf_receive_packet(avci->bsf, pkt); | |
194 |
2/2✓ Branch 0 taken 6280 times.
✓ Branch 1 taken 788468 times.
|
794748 | if (ret == AVERROR_EOF) |
195 | 6280 | avci->draining = 1; | |
196 |
2/2✓ Branch 0 taken 401486 times.
✓ Branch 1 taken 393262 times.
|
794748 | if (ret < 0) |
197 | 401486 | return ret; | |
198 | |||
199 |
1/2✓ Branch 1 taken 393262 times.
✗ Branch 2 not taken.
|
393262 | if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) { |
200 | 393262 | ret = extract_packet_props(avctx->internal, pkt); | |
201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 393262 times.
|
393262 | if (ret < 0) |
202 | ✗ | goto finish; | |
203 | } | ||
204 | |||
205 | 393262 | ret = apply_param_change(avctx, pkt); | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 393262 times.
|
393262 | if (ret < 0) |
207 | ✗ | goto finish; | |
208 | |||
209 | 393262 | return 0; | |
210 | ✗ | finish: | |
211 | ✗ | av_packet_unref(pkt); | |
212 | ✗ | return ret; | |
213 | } | ||
214 | |||
215 | /** | ||
216 | * Attempt to guess proper monotonic timestamps for decoded video frames | ||
217 | * which might have incorrect times. Input timestamps may wrap around, in | ||
218 | * which case the output will as well. | ||
219 | * | ||
220 | * @param pts the pts field of the decoded AVPacket, as passed through | ||
221 | * AVFrame.pts | ||
222 | * @param dts the dts field of the decoded AVPacket | ||
223 | * @return one of the input values, may be AV_NOPTS_VALUE | ||
224 | */ | ||
225 | 411341 | static int64_t guess_correct_pts(AVCodecContext *ctx, | |
226 | int64_t reordered_pts, int64_t dts) | ||
227 | { | ||
228 | 411341 | int64_t pts = AV_NOPTS_VALUE; | |
229 | |||
230 |
2/2✓ Branch 0 taken 376187 times.
✓ Branch 1 taken 35154 times.
|
411341 | if (dts != AV_NOPTS_VALUE) { |
231 | 376187 | ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts; | |
232 | 376187 | ctx->pts_correction_last_dts = dts; | |
233 |
2/2✓ Branch 0 taken 337 times.
✓ Branch 1 taken 34817 times.
|
35154 | } else if (reordered_pts != AV_NOPTS_VALUE) |
234 | 337 | ctx->pts_correction_last_dts = reordered_pts; | |
235 | |||
236 |
2/2✓ Branch 0 taken 347138 times.
✓ Branch 1 taken 64203 times.
|
411341 | if (reordered_pts != AV_NOPTS_VALUE) { |
237 | 347138 | ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts; | |
238 | 347138 | ctx->pts_correction_last_pts = reordered_pts; | |
239 |
2/2✓ Branch 0 taken 29386 times.
✓ Branch 1 taken 34817 times.
|
64203 | } else if(dts != AV_NOPTS_VALUE) |
240 | 29386 | ctx->pts_correction_last_pts = dts; | |
241 | |||
242 |
4/4✓ Branch 0 taken 566 times.
✓ Branch 1 taken 410775 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 556 times.
|
411341 | if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE) |
243 |
2/2✓ Branch 0 taken 346582 times.
✓ Branch 1 taken 64203 times.
|
410785 | && reordered_pts != AV_NOPTS_VALUE) |
244 | 346582 | pts = reordered_pts; | |
245 | else | ||
246 | 64759 | pts = dts; | |
247 | |||
248 | 411341 | return pts; | |
249 | } | ||
250 | |||
251 | /* | ||
252 | * The core of the receive_frame_wrapper for the decoders implementing | ||
253 | * the simple API. Certain decoders might consume partial packets without | ||
254 | * returning any output, so this function needs to be called in a loop until it | ||
255 | * returns EAGAIN. | ||
256 | **/ | ||
257 | 830761 | static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples) | |
258 | { | ||
259 | 830761 | AVCodecInternal *avci = avctx->internal; | |
260 | 830761 | AVPacket *const pkt = avci->in_pkt; | |
261 | 830761 | const FFCodec *const codec = ffcodec(avctx->codec); | |
262 | int got_frame, actual_got_frame; | ||
263 | int ret; | ||
264 | |||
265 |
4/4✓ Branch 0 taken 801898 times.
✓ Branch 1 taken 28863 times.
✓ Branch 2 taken 794556 times.
✓ Branch 3 taken 7342 times.
|
830761 | if (!pkt->data && !avci->draining) { |
266 | 794556 | av_packet_unref(pkt); | |
267 | 794556 | ret = ff_decode_get_packet(avctx, pkt); | |
268 |
4/4✓ Branch 0 taken 401389 times.
✓ Branch 1 taken 393167 times.
✓ Branch 2 taken 395112 times.
✓ Branch 3 taken 6277 times.
|
794556 | if (ret < 0 && ret != AVERROR_EOF) |
269 | 395112 | return ret; | |
270 | } | ||
271 | |||
272 | // Some codecs (at least wma lossless) will crash when feeding drain packets | ||
273 | // after EOF was signaled. | ||
274 |
2/2✓ Branch 0 taken 6585 times.
✓ Branch 1 taken 429064 times.
|
435649 | if (avci->draining_done) |
275 | 6585 | return AVERROR_EOF; | |
276 | |||
277 |
2/2✓ Branch 0 taken 7034 times.
✓ Branch 1 taken 422030 times.
|
429064 | if (!pkt->data && |
278 |
2/2✓ Branch 0 taken 5623 times.
✓ Branch 1 taken 1411 times.
|
7034 | !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY || |
279 |
2/2✓ Branch 0 taken 5582 times.
✓ Branch 1 taken 41 times.
|
5623 | avctx->active_thread_type & FF_THREAD_FRAME)) |
280 | 5582 | return AVERROR_EOF; | |
281 | |||
282 | 423482 | got_frame = 0; | |
283 | |||
284 |
2/2✓ Branch 0 taken 610 times.
✓ Branch 1 taken 422872 times.
|
423482 | if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) { |
285 | 610 | ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt); | |
286 | } else { | ||
287 | 422872 | ret = codec->cb.decode(avctx, frame, &got_frame, pkt); | |
288 | |||
289 |
2/2✓ Branch 0 taken 422542 times.
✓ Branch 1 taken 330 times.
|
422872 | if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS)) |
290 | 422542 | frame->pkt_dts = pkt->dts; | |
291 |
2/2✓ Branch 0 taken 120554 times.
✓ Branch 1 taken 302318 times.
|
422872 | if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { |
292 | #if FF_API_FRAME_PKT | ||
293 | FF_DISABLE_DEPRECATION_WARNINGS | ||
294 |
2/2✓ Branch 0 taken 87930 times.
✓ Branch 1 taken 32624 times.
|
120554 | if(!avctx->has_b_frames) |
295 | 87930 | frame->pkt_pos = pkt->pos; | |
296 | FF_ENABLE_DEPRECATION_WARNINGS | ||
297 | #endif | ||
298 | //FIXME these should be under if(!avctx->has_b_frames) | ||
299 | /* get_buffer is supposed to set frame parameters */ | ||
300 |
2/2✓ Branch 0 taken 22509 times.
✓ Branch 1 taken 98045 times.
|
120554 | if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) { |
301 |
2/2✓ Branch 0 taken 21800 times.
✓ Branch 1 taken 709 times.
|
22509 | if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio; |
302 |
2/2✓ Branch 0 taken 21794 times.
✓ Branch 1 taken 715 times.
|
22509 | if (!frame->width) frame->width = avctx->width; |
303 |
2/2✓ Branch 0 taken 21794 times.
✓ Branch 1 taken 715 times.
|
22509 | if (!frame->height) frame->height = avctx->height; |
304 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22507 times.
|
22509 | if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt; |
305 | } | ||
306 | } | ||
307 | } | ||
308 | 423482 | emms_c(); | |
309 | 423482 | actual_got_frame = got_frame; | |
310 | |||
311 |
2/2✓ Branch 0 taken 120692 times.
✓ Branch 1 taken 302790 times.
|
423482 | if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) { |
312 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 120512 times.
|
120692 | if (frame->flags & AV_FRAME_FLAG_DISCARD) |
313 | 180 | got_frame = 0; | |
314 |
1/2✓ Branch 0 taken 302790 times.
✗ Branch 1 not taken.
|
302790 | } else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) { |
315 | uint8_t *side; | ||
316 | size_t side_size; | ||
317 | 302790 | uint32_t discard_padding = 0; | |
318 | 302790 | uint8_t skip_reason = 0; | |
319 | 302790 | uint8_t discard_reason = 0; | |
320 | |||
321 |
4/4✓ Branch 0 taken 302769 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 301393 times.
✓ Branch 3 taken 1376 times.
|
302790 | if (ret >= 0 && got_frame) { |
322 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 301393 times.
|
301393 | if (frame->format == AV_SAMPLE_FMT_NONE) |
323 | ✗ | frame->format = avctx->sample_fmt; | |
324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 301393 times.
|
301393 | if (!frame->ch_layout.nb_channels) { |
325 | ✗ | int ret2 = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); | |
326 | ✗ | if (ret2 < 0) { | |
327 | ✗ | ret = ret2; | |
328 | ✗ | got_frame = 0; | |
329 | } | ||
330 | } | ||
331 | #if FF_API_OLD_CHANNEL_LAYOUT | ||
332 | FF_DISABLE_DEPRECATION_WARNINGS | ||
333 |
2/2✓ Branch 0 taken 1139 times.
✓ Branch 1 taken 300254 times.
|
301393 | if (!frame->channel_layout) |
334 | 1139 | frame->channel_layout = avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? | |
335 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1115 times.
|
1139 | avctx->ch_layout.u.mask : 0; |
336 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 301393 times.
|
301393 | if (!frame->channels) |
337 | ✗ | frame->channels = avctx->ch_layout.nb_channels; | |
338 | FF_ENABLE_DEPRECATION_WARNINGS | ||
339 | #endif | ||
340 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 301388 times.
|
301393 | if (!frame->sample_rate) |
341 | 5 | frame->sample_rate = avctx->sample_rate; | |
342 | } | ||
343 | |||
344 | 302790 | side= av_packet_get_side_data(avci->last_pkt_props, AV_PKT_DATA_SKIP_SAMPLES, &side_size); | |
345 |
3/4✓ Branch 0 taken 112 times.
✓ Branch 1 taken 302678 times.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
|
302790 | if(side && side_size>=10) { |
346 | 112 | avci->skip_samples = AV_RL32(side); | |
347 | 112 | avci->skip_samples = FFMAX(0, avci->skip_samples); | |
348 | 112 | discard_padding = AV_RL32(side + 4); | |
349 | 112 | av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n", | |
350 | avci->skip_samples, (int)discard_padding); | ||
351 | 112 | skip_reason = AV_RL8(side + 8); | |
352 | 112 | discard_reason = AV_RL8(side + 9); | |
353 | } | ||
354 | |||
355 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 302770 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
|
302790 | if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame && |
356 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) { |
357 | 20 | avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples); | |
358 | 20 | got_frame = 0; | |
359 | 20 | *discarded_samples += frame->nb_samples; | |
360 | } | ||
361 | |||
362 |
4/4✓ Branch 0 taken 220 times.
✓ Branch 1 taken 302570 times.
✓ Branch 2 taken 213 times.
✓ Branch 3 taken 7 times.
|
302790 | if (avci->skip_samples > 0 && got_frame && |
363 |
1/2✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
|
213 | !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) { |
364 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 63 times.
|
213 | if(frame->nb_samples <= avci->skip_samples){ |
365 | 150 | got_frame = 0; | |
366 | 150 | *discarded_samples += frame->nb_samples; | |
367 | 150 | avci->skip_samples -= frame->nb_samples; | |
368 | 150 | av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n", | |
369 | avci->skip_samples); | ||
370 | } else { | ||
371 | 63 | av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples, | |
372 | 63 | frame->nb_samples - avci->skip_samples, avctx->ch_layout.nb_channels, frame->format); | |
373 |
2/4✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
|
126 | if(avctx->pkt_timebase.num && avctx->sample_rate) { |
374 | 63 | int64_t diff_ts = av_rescale_q(avci->skip_samples, | |
375 | 63 | (AVRational){1, avctx->sample_rate}, | |
376 | avctx->pkt_timebase); | ||
377 |
1/2✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63 | if(frame->pts!=AV_NOPTS_VALUE) |
378 | 63 | frame->pts += diff_ts; | |
379 |
1/2✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63 | if(frame->pkt_dts!=AV_NOPTS_VALUE) |
380 | 63 | frame->pkt_dts += diff_ts; | |
381 |
1/2✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63 | if (frame->duration >= diff_ts) |
382 | 63 | frame->duration -= diff_ts; | |
383 | } else { | ||
384 | ✗ | av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n"); | |
385 | } | ||
386 | 63 | av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n", | |
387 | avci->skip_samples, frame->nb_samples); | ||
388 | 63 | *discarded_samples += avci->skip_samples; | |
389 | 63 | frame->nb_samples -= avci->skip_samples; | |
390 | 63 | avci->skip_samples = 0; | |
391 | } | ||
392 | } | ||
393 | |||
394 |
4/6✓ Branch 0 taken 7 times.
✓ Branch 1 taken 302783 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
|
302790 | if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame && |
395 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) { |
396 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if (discard_padding == frame->nb_samples) { |
397 | 2 | *discarded_samples += frame->nb_samples; | |
398 | 2 | got_frame = 0; | |
399 | } else { | ||
400 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | if(avctx->pkt_timebase.num && avctx->sample_rate) { |
401 | 5 | int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding, | |
402 | 5 | (AVRational){1, avctx->sample_rate}, | |
403 | avctx->pkt_timebase); | ||
404 | 5 | frame->duration = diff_ts; | |
405 | } else { | ||
406 | ✗ | av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n"); | |
407 | } | ||
408 | 5 | av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n", | |
409 | (int)discard_padding, frame->nb_samples); | ||
410 | 5 | frame->nb_samples -= discard_padding; | |
411 | } | ||
412 | } | ||
413 | |||
414 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 302790 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
302790 | if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) { |
415 | ✗ | AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10); | |
416 | ✗ | if (fside) { | |
417 | ✗ | AV_WL32(fside->data, avci->skip_samples); | |
418 | ✗ | AV_WL32(fside->data + 4, discard_padding); | |
419 | ✗ | AV_WL8(fside->data + 8, skip_reason); | |
420 | ✗ | AV_WL8(fside->data + 9, discard_reason); | |
421 | ✗ | avci->skip_samples = 0; | |
422 | } | ||
423 | } | ||
424 | } | ||
425 | |||
426 |
2/2✓ Branch 0 taken 302790 times.
✓ Branch 1 taken 120692 times.
|
423482 | if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && |
427 |
4/4✓ Branch 0 taken 277263 times.
✓ Branch 1 taken 25527 times.
✓ Branch 2 taken 277245 times.
✓ Branch 3 taken 18 times.
|
302790 | !avci->showed_multi_packet_warning && |
428 |
4/4✓ Branch 0 taken 5280 times.
✓ Branch 1 taken 271965 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 5260 times.
|
277245 | ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) { |
429 | 20 | av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n"); | |
430 | 20 | avci->showed_multi_packet_warning = 1; | |
431 | } | ||
432 | |||
433 |
2/2✓ Branch 0 taken 12326 times.
✓ Branch 1 taken 411156 times.
|
423482 | if (!got_frame) |
434 | 12326 | av_frame_unref(frame); | |
435 | |||
436 |
4/4✓ Branch 0 taken 422469 times.
✓ Branch 1 taken 1013 times.
✓ Branch 2 taken 119700 times.
✓ Branch 3 taken 302769 times.
|
423482 | if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO) |
437 | 119700 | ret = pkt->size; | |
438 | |||
439 | /* do not stop draining when actual_got_frame != 0 or ret < 0 */ | ||
440 | /* got_frame == 0 but actual_got_frame != 0 when frame is discarded */ | ||
441 |
4/4✓ Branch 0 taken 1452 times.
✓ Branch 1 taken 422030 times.
✓ Branch 2 taken 681 times.
✓ Branch 3 taken 771 times.
|
423482 | if (avci->draining && !actual_got_frame) { |
442 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 680 times.
|
681 | if (ret < 0) { |
443 | /* prevent infinite loop if a decoder wrongly always return error on draining */ | ||
444 | /* reasonable nb_errors_max = maximum b frames + thread count */ | ||
445 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ? |
446 | ✗ | avctx->thread_count : 1); | |
447 | |||
448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (avci->nb_draining_errors++ >= nb_errors_max) { |
449 | ✗ | av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. " | |
450 | "Stop draining and force EOF.\n"); | ||
451 | ✗ | avci->draining_done = 1; | |
452 | ✗ | ret = AVERROR_BUG; | |
453 | } | ||
454 | } else { | ||
455 | 680 | avci->draining_done = 1; | |
456 | } | ||
457 | } | ||
458 | |||
459 |
4/4✓ Branch 0 taken 29887 times.
✓ Branch 1 taken 393595 times.
✓ Branch 2 taken 1013 times.
✓ Branch 3 taken 28874 times.
|
423482 | if (ret >= pkt->size || ret < 0) { |
460 | 394608 | av_packet_unref(pkt); | |
461 | } else { | ||
462 | 28874 | int consumed = ret; | |
463 | |||
464 | 28874 | pkt->data += consumed; | |
465 | 28874 | pkt->size -= consumed; | |
466 | 28874 | pkt->pts = AV_NOPTS_VALUE; | |
467 | 28874 | pkt->dts = AV_NOPTS_VALUE; | |
468 |
1/2✓ Branch 0 taken 28874 times.
✗ Branch 1 not taken.
|
28874 | if (!(codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) { |
469 | #if FF_API_FRAME_PKT | ||
470 | // See extract_packet_props() comment. | ||
471 | 28874 | avci->last_pkt_props->stream_index = avci->last_pkt_props->stream_index - consumed; | |
472 | #endif | ||
473 | 28874 | avci->last_pkt_props->pts = AV_NOPTS_VALUE; | |
474 | 28874 | avci->last_pkt_props->dts = AV_NOPTS_VALUE; | |
475 | } | ||
476 | } | ||
477 | |||
478 |
2/2✓ Branch 0 taken 411156 times.
✓ Branch 1 taken 12326 times.
|
423482 | if (got_frame) |
479 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 411156 times.
|
411156 | av_assert0(frame->buf[0]); |
480 | |||
481 | 423482 | return ret < 0 ? ret : 0; | |
482 | } | ||
483 | |||
484 | #if CONFIG_LCMS2 | ||
485 | static int detect_colorspace(AVCodecContext *avctx, AVFrame *frame) | ||
486 | { | ||
487 | AVCodecInternal *avci = avctx->internal; | ||
488 | enum AVColorTransferCharacteristic trc; | ||
489 | AVColorPrimariesDesc coeffs; | ||
490 | enum AVColorPrimaries prim; | ||
491 | cmsHPROFILE profile; | ||
492 | AVFrameSideData *sd; | ||
493 | int ret; | ||
494 | if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES)) | ||
495 | return 0; | ||
496 | |||
497 | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE); | ||
498 | if (!sd || !sd->size) | ||
499 | return 0; | ||
500 | |||
501 | if (!avci->icc.avctx) { | ||
502 | ret = ff_icc_context_init(&avci->icc, avctx); | ||
503 | if (ret < 0) | ||
504 | return ret; | ||
505 | } | ||
506 | |||
507 | profile = cmsOpenProfileFromMemTHR(avci->icc.ctx, sd->data, sd->size); | ||
508 | if (!profile) | ||
509 | return AVERROR_INVALIDDATA; | ||
510 | |||
511 | ret = ff_icc_profile_read_primaries(&avci->icc, profile, &coeffs); | ||
512 | if (!ret) | ||
513 | ret = ff_icc_profile_detect_transfer(&avci->icc, profile, &trc); | ||
514 | cmsCloseProfile(profile); | ||
515 | if (ret < 0) | ||
516 | return ret; | ||
517 | |||
518 | prim = av_csp_primaries_id_from_desc(&coeffs); | ||
519 | if (prim != AVCOL_PRI_UNSPECIFIED) | ||
520 | frame->color_primaries = prim; | ||
521 | if (trc != AVCOL_TRC_UNSPECIFIED) | ||
522 | frame->color_trc = trc; | ||
523 | return 0; | ||
524 | } | ||
525 | #else /* !CONFIG_LCMS2 */ | ||
526 | 819733 | static int detect_colorspace(av_unused AVCodecContext *c, av_unused AVFrame *f) | |
527 | { | ||
528 | 819733 | return 0; | |
529 | } | ||
530 | #endif | ||
531 | |||
532 | 819448 | static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame) | |
533 | { | ||
534 | int ret; | ||
535 | 819448 | int64_t discarded_samples = 0; | |
536 | |||
537 |
2/2✓ Branch 0 taken 830761 times.
✓ Branch 1 taken 411156 times.
|
1241917 | while (!frame->buf[0]) { |
538 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 830761 times.
|
830761 | if (discarded_samples > avctx->max_samples) |
539 | ✗ | return AVERROR(EAGAIN); | |
540 | 830761 | ret = decode_simple_internal(avctx, frame, &discarded_samples); | |
541 |
2/2✓ Branch 0 taken 408292 times.
✓ Branch 1 taken 422469 times.
|
830761 | if (ret < 0) |
542 | 408292 | return ret; | |
543 | } | ||
544 | |||
545 | 411156 | return 0; | |
546 | } | ||
547 | |||
548 | 819733 | static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) | |
549 | { | ||
550 | 819733 | AVCodecInternal *avci = avctx->internal; | |
551 | 819733 | const FFCodec *const codec = ffcodec(avctx->codec); | |
552 | int ret, ok; | ||
553 | |||
554 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 819733 times.
|
819733 | av_assert0(!frame->buf[0]); |
555 | |||
556 |
2/2✓ Branch 0 taken 285 times.
✓ Branch 1 taken 819448 times.
|
819733 | if (codec->cb_type == FF_CODEC_CB_TYPE_RECEIVE_FRAME) { |
557 | 285 | ret = codec->cb.receive_frame(avctx, frame); | |
558 | 285 | emms_c(); | |
559 | } else | ||
560 | 819448 | ret = decode_simple_receive_frame(avctx, frame); | |
561 | |||
562 |
2/2✓ Branch 0 taken 12173 times.
✓ Branch 1 taken 807560 times.
|
819733 | if (ret == AVERROR_EOF) |
563 | 12173 | avci->draining_done = 1; | |
564 | |||
565 | /* preserve ret */ | ||
566 | 819733 | ok = detect_colorspace(avctx, frame); | |
567 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 819733 times.
|
819733 | if (ok < 0) { |
568 | ✗ | av_frame_unref(frame); | |
569 | ✗ | return ok; | |
570 | } | ||
571 | |||
572 |
2/2✓ Branch 0 taken 411341 times.
✓ Branch 1 taken 408392 times.
|
819733 | if (!ret) { |
573 | 411341 | frame->best_effort_timestamp = guess_correct_pts(avctx, | |
574 | frame->pts, | ||
575 | frame->pkt_dts); | ||
576 | |||
577 | #if FF_API_PKT_DURATION | ||
578 | FF_DISABLE_DEPRECATION_WARNINGS | ||
579 | 411341 | frame->pkt_duration = frame->duration; | |
580 | FF_ENABLE_DEPRECATION_WARNINGS | ||
581 | #endif | ||
582 | |||
583 | /* the only case where decode data is not set should be decoders | ||
584 | * that do not call ff_get_buffer() */ | ||
585 |
4/6✓ Branch 0 taken 388840 times.
✓ Branch 1 taken 22501 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 388840 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22501 times.
|
411341 | av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) || |
586 | !(avctx->codec->capabilities & AV_CODEC_CAP_DR1)); | ||
587 | |||
588 |
2/2✓ Branch 0 taken 388840 times.
✓ Branch 1 taken 22501 times.
|
411341 | if (frame->private_ref) { |
589 | 388840 | FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data; | |
590 | |||
591 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 388840 times.
|
388840 | if (fdd->post_process) { |
592 | ✗ | ret = fdd->post_process(avctx, frame); | |
593 | ✗ | if (ret < 0) { | |
594 | ✗ | av_frame_unref(frame); | |
595 | ✗ | return ret; | |
596 | } | ||
597 | } | ||
598 | } | ||
599 | } | ||
600 | |||
601 | /* free the per-frame decode data */ | ||
602 | 819733 | av_buffer_unref(&frame->private_ref); | |
603 | |||
604 | 819733 | return ret; | |
605 | } | ||
606 | |||
607 | 400166 | int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) | |
608 | { | ||
609 | 400166 | AVCodecInternal *avci = avctx->internal; | |
610 | int ret; | ||
611 | |||
612 |
2/4✓ Branch 1 taken 400166 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 400166 times.
|
400166 | if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec)) |
613 | ✗ | return AVERROR(EINVAL); | |
614 | |||
615 |
2/2✓ Branch 0 taken 688 times.
✓ Branch 1 taken 399478 times.
|
400166 | if (avctx->internal->draining) |
616 | 688 | return AVERROR_EOF; | |
617 | |||
618 |
5/6✓ Branch 0 taken 399458 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6260 times.
✓ Branch 3 taken 393198 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6260 times.
|
399478 | if (avpkt && !avpkt->size && avpkt->data) |
619 | ✗ | return AVERROR(EINVAL); | |
620 | |||
621 | 399478 | av_packet_unref(avci->buffer_pkt); | |
622 |
5/6✓ Branch 0 taken 399458 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 6260 times.
✓ Branch 3 taken 393198 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6260 times.
|
399478 | if (avpkt && (avpkt->data || avpkt->side_data_elems)) { |
623 | 393198 | ret = av_packet_ref(avci->buffer_pkt, avpkt); | |
624 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 393198 times.
|
393198 | if (ret < 0) |
625 | ✗ | return ret; | |
626 | } | ||
627 | |||
628 | 399478 | ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt); | |
629 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 399411 times.
|
399478 | if (ret < 0) { |
630 | 67 | av_packet_unref(avci->buffer_pkt); | |
631 | 67 | return ret; | |
632 | } | ||
633 | |||
634 |
1/2✓ Branch 0 taken 399411 times.
✗ Branch 1 not taken.
|
399411 | if (!avci->buffer_frame->buf[0]) { |
635 | 399411 | ret = decode_receive_frame_internal(avctx, avci->buffer_frame); | |
636 |
6/6✓ Branch 0 taken 16175 times.
✓ Branch 1 taken 383236 times.
✓ Branch 2 taken 6846 times.
✓ Branch 3 taken 9329 times.
✓ Branch 4 taken 940 times.
✓ Branch 5 taken 5906 times.
|
399411 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
637 | 940 | return ret; | |
638 | } | ||
639 | |||
640 | 398471 | return 0; | |
641 | } | ||
642 | |||
643 | 109948 | static int apply_cropping(AVCodecContext *avctx, AVFrame *frame) | |
644 | { | ||
645 | /* make sure we are noisy about decoders returning invalid cropping data */ | ||
646 |
1/2✓ Branch 0 taken 109948 times.
✗ Branch 1 not taken.
|
109948 | if (frame->crop_left >= INT_MAX - frame->crop_right || |
647 |
1/2✓ Branch 0 taken 109948 times.
✗ Branch 1 not taken.
|
109948 | frame->crop_top >= INT_MAX - frame->crop_bottom || |
648 |
1/2✓ Branch 0 taken 109948 times.
✗ Branch 1 not taken.
|
109948 | (frame->crop_left + frame->crop_right) >= frame->width || |
649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 109948 times.
|
109948 | (frame->crop_top + frame->crop_bottom) >= frame->height) { |
650 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
651 | "Invalid cropping information set by a decoder: " | ||
652 | "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" " | ||
653 | "(frame size %dx%d). This is a bug, please report it\n", | ||
654 | frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom, | ||
655 | frame->width, frame->height); | ||
656 | ✗ | frame->crop_left = 0; | |
657 | ✗ | frame->crop_right = 0; | |
658 | ✗ | frame->crop_top = 0; | |
659 | ✗ | frame->crop_bottom = 0; | |
660 | ✗ | return 0; | |
661 | } | ||
662 | |||
663 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 109948 times.
|
109948 | if (!avctx->apply_cropping) |
664 | ✗ | return 0; | |
665 | |||
666 | 109948 | return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ? | |
667 | AV_FRAME_CROP_UNALIGNED : 0); | ||
668 | } | ||
669 | |||
670 | // make sure frames returned to the caller are valid | ||
671 | 411341 | static int frame_validate(AVCodecContext *avctx, AVFrame *frame) | |
672 | { | ||
673 |
2/4✓ Branch 0 taken 411341 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 411341 times.
|
411341 | if (!frame->buf[0] || frame->format < 0) |
674 | ✗ | goto fail; | |
675 | |||
676 |
2/3✓ Branch 0 taken 109948 times.
✓ Branch 1 taken 301393 times.
✗ Branch 2 not taken.
|
411341 | switch (avctx->codec_type) { |
677 | 109948 | case AVMEDIA_TYPE_VIDEO: | |
678 |
2/4✓ Branch 0 taken 109948 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 109948 times.
|
109948 | if (frame->width <= 0 || frame->height <= 0) |
679 | ✗ | goto fail; | |
680 | 109948 | break; | |
681 | 301393 | case AVMEDIA_TYPE_AUDIO: | |
682 |
1/2✓ Branch 1 taken 301393 times.
✗ Branch 2 not taken.
|
301393 | if (!av_channel_layout_check(&frame->ch_layout) || |
683 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 301393 times.
|
301393 | frame->sample_rate <= 0) |
684 | ✗ | goto fail; | |
685 | |||
686 | 301393 | break; | |
687 | ✗ | default: av_assert0(0); | |
688 | } | ||
689 | |||
690 | 411341 | return 0; | |
691 | ✗ | fail: | |
692 | ✗ | av_log(avctx, AV_LOG_ERROR, "An invalid frame was output by a decoder. " | |
693 | "This is a bug, please report it.\n"); | ||
694 | ✗ | return AVERROR_BUG; | |
695 | } | ||
696 | |||
697 | 803558 | int ff_decode_receive_frame(AVCodecContext *avctx, AVFrame *frame) | |
698 | { | ||
699 | 803558 | AVCodecInternal *avci = avctx->internal; | |
700 | int ret, changed; | ||
701 | |||
702 |
2/4✓ Branch 1 taken 803558 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 803558 times.
|
803558 | if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec)) |
703 | ✗ | return AVERROR(EINVAL); | |
704 | |||
705 |
2/2✓ Branch 0 taken 383236 times.
✓ Branch 1 taken 420322 times.
|
803558 | if (avci->buffer_frame->buf[0]) { |
706 | 383236 | av_frame_move_ref(frame, avci->buffer_frame); | |
707 | } else { | ||
708 | 420322 | ret = decode_receive_frame_internal(avctx, frame); | |
709 |
2/2✓ Branch 0 taken 392217 times.
✓ Branch 1 taken 28105 times.
|
420322 | if (ret < 0) |
710 | 392217 | return ret; | |
711 | } | ||
712 | |||
713 | 411341 | ret = frame_validate(avctx, frame); | |
714 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 411341 times.
|
411341 | if (ret < 0) |
715 | ✗ | goto fail; | |
716 | |||
717 |
2/2✓ Branch 0 taken 109948 times.
✓ Branch 1 taken 301393 times.
|
411341 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
718 | 109948 | ret = apply_cropping(avctx, frame); | |
719 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 109948 times.
|
109948 | if (ret < 0) |
720 | ✗ | goto fail; | |
721 | } | ||
722 | |||
723 | 411341 | avctx->frame_num++; | |
724 | #if FF_API_AVCTX_FRAME_NUMBER | ||
725 | FF_DISABLE_DEPRECATION_WARNINGS | ||
726 | 411341 | avctx->frame_number = avctx->frame_num; | |
727 | FF_ENABLE_DEPRECATION_WARNINGS | ||
728 | #endif | ||
729 | |||
730 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 411341 times.
|
411341 | if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) { |
731 | |||
732 | ✗ | if (avctx->frame_num == 1) { | |
733 | ✗ | avci->initial_format = frame->format; | |
734 | ✗ | switch(avctx->codec_type) { | |
735 | ✗ | case AVMEDIA_TYPE_VIDEO: | |
736 | ✗ | avci->initial_width = frame->width; | |
737 | ✗ | avci->initial_height = frame->height; | |
738 | ✗ | break; | |
739 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
740 | ✗ | avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate : | |
741 | avctx->sample_rate; | ||
742 | ✗ | ret = av_channel_layout_copy(&avci->initial_ch_layout, &frame->ch_layout); | |
743 | ✗ | if (ret < 0) | |
744 | ✗ | goto fail; | |
745 | ✗ | break; | |
746 | } | ||
747 | } | ||
748 | |||
749 | ✗ | if (avctx->frame_num > 1) { | |
750 | ✗ | changed = avci->initial_format != frame->format; | |
751 | |||
752 | ✗ | switch(avctx->codec_type) { | |
753 | ✗ | case AVMEDIA_TYPE_VIDEO: | |
754 | ✗ | changed |= avci->initial_width != frame->width || | |
755 | ✗ | avci->initial_height != frame->height; | |
756 | ✗ | break; | |
757 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
758 | ✗ | changed |= avci->initial_sample_rate != frame->sample_rate || | |
759 | ✗ | avci->initial_sample_rate != avctx->sample_rate || | |
760 | ✗ | av_channel_layout_compare(&avci->initial_ch_layout, &frame->ch_layout); | |
761 | ✗ | break; | |
762 | } | ||
763 | |||
764 | ✗ | if (changed) { | |
765 | ✗ | avci->changed_frames_dropped++; | |
766 | ✗ | av_log(avctx, AV_LOG_INFO, "dropped changed frame #%"PRId64" pts %"PRId64 | |
767 | " drop count: %d \n", | ||
768 | avctx->frame_num, frame->pts, | ||
769 | avci->changed_frames_dropped); | ||
770 | ✗ | ret = AVERROR_INPUT_CHANGED; | |
771 | ✗ | goto fail; | |
772 | } | ||
773 | } | ||
774 | } | ||
775 | 411341 | return 0; | |
776 | ✗ | fail: | |
777 | ✗ | av_frame_unref(frame); | |
778 | ✗ | return ret; | |
779 | } | ||
780 | |||
781 | 1709 | static void get_subtitle_defaults(AVSubtitle *sub) | |
782 | { | ||
783 | 1709 | memset(sub, 0, sizeof(*sub)); | |
784 | 1709 | sub->pts = AV_NOPTS_VALUE; | |
785 | 1709 | } | |
786 | |||
787 | #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */ | ||
788 | 1675 | static int recode_subtitle(AVCodecContext *avctx, const AVPacket **outpkt, | |
789 | const AVPacket *inpkt, AVPacket *buf_pkt) | ||
790 | { | ||
791 | #if CONFIG_ICONV | ||
792 | 1675 | iconv_t cd = (iconv_t)-1; | |
793 | 1675 | int ret = 0; | |
794 | char *inb, *outb; | ||
795 | size_t inl, outl; | ||
796 | #endif | ||
797 | |||
798 |
3/4✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
|
1675 | if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0) { |
799 | 1582 | *outpkt = inpkt; | |
800 | 1582 | return 0; | |
801 | } | ||
802 | |||
803 | #if CONFIG_ICONV | ||
804 | 93 | inb = inpkt->data; | |
805 | 93 | inl = inpkt->size; | |
806 | |||
807 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) { |
808 | ✗ | av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n"); | |
809 | ✗ | return AVERROR(ERANGE); | |
810 | } | ||
811 | |||
812 | 93 | cd = iconv_open("UTF-8", avctx->sub_charenc); | |
813 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | av_assert0(cd != (iconv_t)-1); |
814 | |||
815 | 93 | ret = av_new_packet(buf_pkt, inl * UTF8_MAX_BYTES); | |
816 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ret < 0) |
817 | ✗ | goto end; | |
818 | 93 | ret = av_packet_copy_props(buf_pkt, inpkt); | |
819 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ret < 0) |
820 | ✗ | goto end; | |
821 | 93 | outb = buf_pkt->data; | |
822 | 93 | outl = buf_pkt->size; | |
823 | |||
824 |
2/4✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
✗ Branch 4 not taken.
|
186 | if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 || |
825 | 93 | iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 || | |
826 |
2/4✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
|
93 | outl >= buf_pkt->size || inl != 0) { |
827 | ✗ | ret = FFMIN(AVERROR(errno), -1); | |
828 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" " | |
829 | ✗ | "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc); | |
830 | ✗ | goto end; | |
831 | } | ||
832 | 93 | buf_pkt->size -= outl; | |
833 | 93 | memset(buf_pkt->data + buf_pkt->size, 0, outl); | |
834 | 93 | *outpkt = buf_pkt; | |
835 | |||
836 | 93 | ret = 0; | |
837 | 93 | end: | |
838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ret < 0) |
839 | ✗ | av_packet_unref(buf_pkt); | |
840 |
1/2✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
|
93 | if (cd != (iconv_t)-1) |
841 | 93 | iconv_close(cd); | |
842 | 93 | return ret; | |
843 | #else | ||
844 | av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv"); | ||
845 | return AVERROR(EINVAL); | ||
846 | #endif | ||
847 | } | ||
848 | |||
849 | 700 | static int utf8_check(const uint8_t *str) | |
850 | { | ||
851 | const uint8_t *byte; | ||
852 | uint32_t codepoint, min; | ||
853 | |||
854 |
2/2✓ Branch 0 taken 61067 times.
✓ Branch 1 taken 700 times.
|
61767 | while (*str) { |
855 | 61067 | byte = str; | |
856 |
5/8✓ Branch 0 taken 61067 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61067 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2794 times.
✓ Branch 6 taken 2794 times.
✓ Branch 7 taken 61067 times.
|
63861 | GET_UTF8(codepoint, *(byte++), return 0;); |
857 |
4/4✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 58978 times.
✓ Branch 2 taken 705 times.
✓ Branch 3 taken 1384 times.
|
61772 | min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 : |
858 | 705 | 1 << (5 * (byte - str) - 4); | |
859 |
3/6✓ Branch 0 taken 61067 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61067 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 61067 times.
✗ Branch 5 not taken.
|
61067 | if (codepoint < min || codepoint >= 0x110000 || |
860 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61067 times.
|
61067 | codepoint == 0xFFFE /* BOM */ || |
861 | ✗ | codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */) | |
862 | ✗ | return 0; | |
863 | 61067 | str = byte; | |
864 | } | ||
865 | 700 | return 1; | |
866 | } | ||
867 | |||
868 | 1709 | int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, | |
869 | int *got_sub_ptr, const AVPacket *avpkt) | ||
870 | { | ||
871 | 1709 | int ret = 0; | |
872 | |||
873 |
3/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1669 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
|
1709 | if (!avpkt->data && avpkt->size) { |
874 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n"); | |
875 | ✗ | return AVERROR(EINVAL); | |
876 | } | ||
877 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1709 times.
|
1709 | if (!avctx->codec) |
878 | ✗ | return AVERROR(EINVAL); | |
879 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1709 times.
|
1709 | if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) { |
880 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n"); | |
881 | ✗ | return AVERROR(EINVAL); | |
882 | } | ||
883 | |||
884 | 1709 | *got_sub_ptr = 0; | |
885 | 1709 | get_subtitle_defaults(sub); | |
886 | |||
887 |
4/4✓ Branch 0 taken 857 times.
✓ Branch 1 taken 852 times.
✓ Branch 2 taken 823 times.
✓ Branch 3 taken 34 times.
|
1709 | if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) { |
888 | 1675 | AVCodecInternal *avci = avctx->internal; | |
889 | const AVPacket *pkt; | ||
890 | |||
891 | 1675 | ret = recode_subtitle(avctx, &pkt, avpkt, avci->buffer_pkt); | |
892 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1675 times.
|
1675 | if (ret < 0) |
893 | 79 | return ret; | |
894 | |||
895 |
3/4✓ Branch 0 taken 1675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
✓ Branch 3 taken 10 times.
|
1675 | if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE) |
896 | 1665 | sub->pts = av_rescale_q(avpkt->pts, | |
897 | 1665 | avctx->pkt_timebase, AV_TIME_BASE_Q); | |
898 | 1675 | ret = ffcodec(avctx->codec)->cb.decode_sub(avctx, sub, got_sub_ptr, pkt); | |
899 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1582 times.
|
1675 | if (pkt == avci->buffer_pkt) // did we recode? |
900 | 93 | av_packet_unref(avci->buffer_pkt); | |
901 |
2/2✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1596 times.
|
1675 | if (ret < 0) { |
902 | 79 | *got_sub_ptr = 0; | |
903 | 79 | avsubtitle_free(sub); | |
904 | 79 | return ret; | |
905 | } | ||
906 | av_assert1(!sub->num_rects || *got_sub_ptr); | ||
907 | |||
908 |
6/6✓ Branch 0 taken 826 times.
✓ Branch 1 taken 770 times.
✓ Branch 2 taken 580 times.
✓ Branch 3 taken 246 times.
✓ Branch 4 taken 528 times.
✓ Branch 5 taken 52 times.
|
1596 | if (sub->num_rects && !sub->end_display_time && avpkt->duration && |
909 |
1/2✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
|
528 | avctx->pkt_timebase.num) { |
910 | 528 | AVRational ms = { 1, 1000 }; | |
911 | 528 | sub->end_display_time = av_rescale_q(avpkt->duration, | |
912 | avctx->pkt_timebase, ms); | ||
913 | } | ||
914 | |||
915 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 1440 times.
|
1596 | if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) |
916 | 156 | sub->format = 0; | |
917 |
1/2✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
|
1440 | else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB) |
918 | 1440 | sub->format = 1; | |
919 | |||
920 |
2/2✓ Branch 0 taken 859 times.
✓ Branch 1 taken 1596 times.
|
2455 | for (unsigned i = 0; i < sub->num_rects; i++) { |
921 |
1/2✓ Branch 0 taken 859 times.
✗ Branch 1 not taken.
|
859 | if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE && |
922 |
3/4✓ Branch 0 taken 700 times.
✓ Branch 1 taken 159 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 700 times.
|
859 | sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) { |
923 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
924 | "Invalid UTF-8 in decoded subtitles text; " | ||
925 | "maybe missing -sub_charenc option\n"); | ||
926 | ✗ | avsubtitle_free(sub); | |
927 | ✗ | *got_sub_ptr = 0; | |
928 | ✗ | return AVERROR_INVALIDDATA; | |
929 | } | ||
930 | } | ||
931 | |||
932 |
2/2✓ Branch 0 taken 835 times.
✓ Branch 1 taken 761 times.
|
1596 | if (*got_sub_ptr) |
933 | 835 | avctx->frame_num++; | |
934 | #if FF_API_AVCTX_FRAME_NUMBER | ||
935 | FF_DISABLE_DEPRECATION_WARNINGS | ||
936 | 1596 | avctx->frame_number = avctx->frame_num; | |
937 | FF_ENABLE_DEPRECATION_WARNINGS | ||
938 | #endif | ||
939 | } | ||
940 | |||
941 | 1630 | return ret; | |
942 | } | ||
943 | |||
944 | 1589 | enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx, | |
945 | const enum AVPixelFormat *fmt) | ||
946 | { | ||
947 | const AVPixFmtDescriptor *desc; | ||
948 | const AVCodecHWConfig *config; | ||
949 | int i, n; | ||
950 | |||
951 | // If a device was supplied when the codec was opened, assume that the | ||
952 | // user wants to use it. | ||
953 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1589 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1589 | if (avctx->hw_device_ctx && ffcodec(avctx->codec)->hw_configs) { |
954 | ✗ | AVHWDeviceContext *device_ctx = | |
955 | ✗ | (AVHWDeviceContext*)avctx->hw_device_ctx->data; | |
956 | ✗ | for (i = 0;; i++) { | |
957 | ✗ | config = &ffcodec(avctx->codec)->hw_configs[i]->public; | |
958 | ✗ | if (!config) | |
959 | ✗ | break; | |
960 | ✗ | if (!(config->methods & | |
961 | AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) | ||
962 | ✗ | continue; | |
963 | ✗ | if (device_ctx->type != config->device_type) | |
964 | ✗ | continue; | |
965 | ✗ | for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) { | |
966 | ✗ | if (config->pix_fmt == fmt[n]) | |
967 | ✗ | return fmt[n]; | |
968 | } | ||
969 | } | ||
970 | } | ||
971 | // No device or other setup, so we have to choose from things which | ||
972 | // don't any other external information. | ||
973 | |||
974 | // If the last element of the list is a software format, choose it | ||
975 | // (this should be best software format if any exist). | ||
976 |
2/2✓ Branch 0 taken 4054 times.
✓ Branch 1 taken 1589 times.
|
5643 | for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++); |
977 | 1589 | desc = av_pix_fmt_desc_get(fmt[n - 1]); | |
978 |
1/2✓ Branch 0 taken 1589 times.
✗ Branch 1 not taken.
|
1589 | if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) |
979 | 1589 | return fmt[n - 1]; | |
980 | |||
981 | // Finally, traverse the list in order and choose the first entry | ||
982 | // with no external dependencies (if there is no hardware configuration | ||
983 | // information available then this just picks the first entry). | ||
984 | ✗ | for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) { | |
985 | ✗ | for (i = 0;; i++) { | |
986 | ✗ | config = avcodec_get_hw_config(avctx->codec, i); | |
987 | ✗ | if (!config) | |
988 | ✗ | break; | |
989 | ✗ | if (config->pix_fmt == fmt[n]) | |
990 | ✗ | break; | |
991 | } | ||
992 | ✗ | if (!config) { | |
993 | // No specific config available, so the decoder must be able | ||
994 | // to handle this format without any additional setup. | ||
995 | ✗ | return fmt[n]; | |
996 | } | ||
997 | ✗ | if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) { | |
998 | // Usable with only internal setup. | ||
999 | ✗ | return fmt[n]; | |
1000 | } | ||
1001 | } | ||
1002 | |||
1003 | // Nothing is usable, give up. | ||
1004 | ✗ | return AV_PIX_FMT_NONE; | |
1005 | } | ||
1006 | |||
1007 | ✗ | int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, | |
1008 | enum AVHWDeviceType dev_type) | ||
1009 | { | ||
1010 | AVHWDeviceContext *device_ctx; | ||
1011 | AVHWFramesContext *frames_ctx; | ||
1012 | int ret; | ||
1013 | |||
1014 | ✗ | if (!avctx->hwaccel) | |
1015 | ✗ | return AVERROR(ENOSYS); | |
1016 | |||
1017 | ✗ | if (avctx->hw_frames_ctx) | |
1018 | ✗ | return 0; | |
1019 | ✗ | if (!avctx->hw_device_ctx) { | |
1020 | ✗ | av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is " | |
1021 | "required for hardware accelerated decoding.\n"); | ||
1022 | ✗ | return AVERROR(EINVAL); | |
1023 | } | ||
1024 | |||
1025 | ✗ | device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data; | |
1026 | ✗ | if (device_ctx->type != dev_type) { | |
1027 | ✗ | av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware " | |
1028 | "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type), | ||
1029 | av_hwdevice_get_type_name(device_ctx->type)); | ||
1030 | ✗ | return AVERROR(EINVAL); | |
1031 | } | ||
1032 | |||
1033 | ✗ | ret = avcodec_get_hw_frames_parameters(avctx, | |
1034 | avctx->hw_device_ctx, | ||
1035 | ✗ | avctx->hwaccel->pix_fmt, | |
1036 | &avctx->hw_frames_ctx); | ||
1037 | ✗ | if (ret < 0) | |
1038 | ✗ | return ret; | |
1039 | |||
1040 | ✗ | frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; | |
1041 | |||
1042 | |||
1043 | ✗ | if (frames_ctx->initial_pool_size) { | |
1044 | // We guarantee 4 base work surfaces. The function above guarantees 1 | ||
1045 | // (the absolute minimum), so add the missing count. | ||
1046 | ✗ | frames_ctx->initial_pool_size += 3; | |
1047 | } | ||
1048 | |||
1049 | ✗ | ret = av_hwframe_ctx_init(avctx->hw_frames_ctx); | |
1050 | ✗ | if (ret < 0) { | |
1051 | ✗ | av_buffer_unref(&avctx->hw_frames_ctx); | |
1052 | ✗ | return ret; | |
1053 | } | ||
1054 | |||
1055 | ✗ | return 0; | |
1056 | } | ||
1057 | |||
1058 | ✗ | int avcodec_get_hw_frames_parameters(AVCodecContext *avctx, | |
1059 | AVBufferRef *device_ref, | ||
1060 | enum AVPixelFormat hw_pix_fmt, | ||
1061 | AVBufferRef **out_frames_ref) | ||
1062 | { | ||
1063 | ✗ | AVBufferRef *frames_ref = NULL; | |
1064 | const AVCodecHWConfigInternal *hw_config; | ||
1065 | const AVHWAccel *hwa; | ||
1066 | int i, ret; | ||
1067 | |||
1068 | ✗ | for (i = 0;; i++) { | |
1069 | ✗ | hw_config = ffcodec(avctx->codec)->hw_configs[i]; | |
1070 | ✗ | if (!hw_config) | |
1071 | ✗ | return AVERROR(ENOENT); | |
1072 | ✗ | if (hw_config->public.pix_fmt == hw_pix_fmt) | |
1073 | ✗ | break; | |
1074 | } | ||
1075 | |||
1076 | ✗ | hwa = hw_config->hwaccel; | |
1077 | ✗ | if (!hwa || !hwa->frame_params) | |
1078 | ✗ | return AVERROR(ENOENT); | |
1079 | |||
1080 | ✗ | frames_ref = av_hwframe_ctx_alloc(device_ref); | |
1081 | ✗ | if (!frames_ref) | |
1082 | ✗ | return AVERROR(ENOMEM); | |
1083 | |||
1084 | ✗ | ret = hwa->frame_params(avctx, frames_ref); | |
1085 | ✗ | if (ret >= 0) { | |
1086 | ✗ | AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data; | |
1087 | |||
1088 | ✗ | if (frames_ctx->initial_pool_size) { | |
1089 | // If the user has requested that extra output surfaces be | ||
1090 | // available then add them here. | ||
1091 | ✗ | if (avctx->extra_hw_frames > 0) | |
1092 | ✗ | frames_ctx->initial_pool_size += avctx->extra_hw_frames; | |
1093 | |||
1094 | // If frame threading is enabled then an extra surface per thread | ||
1095 | // is also required. | ||
1096 | ✗ | if (avctx->active_thread_type & FF_THREAD_FRAME) | |
1097 | ✗ | frames_ctx->initial_pool_size += avctx->thread_count; | |
1098 | } | ||
1099 | |||
1100 | ✗ | *out_frames_ref = frames_ref; | |
1101 | } else { | ||
1102 | ✗ | av_buffer_unref(&frames_ref); | |
1103 | } | ||
1104 | ✗ | return ret; | |
1105 | } | ||
1106 | |||
1107 | ✗ | static int hwaccel_init(AVCodecContext *avctx, | |
1108 | const AVCodecHWConfigInternal *hw_config) | ||
1109 | { | ||
1110 | const AVHWAccel *hwaccel; | ||
1111 | int err; | ||
1112 | |||
1113 | ✗ | hwaccel = hw_config->hwaccel; | |
1114 | ✗ | if (hwaccel->capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL && | |
1115 | ✗ | avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { | |
1116 | ✗ | av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n", | |
1117 | ✗ | hwaccel->name); | |
1118 | ✗ | return AVERROR_PATCHWELCOME; | |
1119 | } | ||
1120 | |||
1121 | ✗ | if (hwaccel->priv_data_size) { | |
1122 | ✗ | avctx->internal->hwaccel_priv_data = | |
1123 | ✗ | av_mallocz(hwaccel->priv_data_size); | |
1124 | ✗ | if (!avctx->internal->hwaccel_priv_data) | |
1125 | ✗ | return AVERROR(ENOMEM); | |
1126 | } | ||
1127 | |||
1128 | ✗ | avctx->hwaccel = hwaccel; | |
1129 | ✗ | if (hwaccel->init) { | |
1130 | ✗ | err = hwaccel->init(avctx); | |
1131 | ✗ | if (err < 0) { | |
1132 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: " | |
1133 | "hwaccel initialisation returned error.\n", | ||
1134 | ✗ | av_get_pix_fmt_name(hw_config->public.pix_fmt)); | |
1135 | ✗ | av_freep(&avctx->internal->hwaccel_priv_data); | |
1136 | ✗ | avctx->hwaccel = NULL; | |
1137 | ✗ | return err; | |
1138 | } | ||
1139 | } | ||
1140 | |||
1141 | ✗ | return 0; | |
1142 | } | ||
1143 | |||
1144 | 2551 | static void hwaccel_uninit(AVCodecContext *avctx) | |
1145 | { | ||
1146 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2551 | if (avctx->hwaccel && avctx->hwaccel->uninit) |
1147 | ✗ | avctx->hwaccel->uninit(avctx); | |
1148 | |||
1149 | 2551 | av_freep(&avctx->internal->hwaccel_priv_data); | |
1150 | |||
1151 | 2551 | avctx->hwaccel = NULL; | |
1152 | |||
1153 | 2551 | av_buffer_unref(&avctx->hw_frames_ctx); | |
1154 | 2551 | } | |
1155 | |||
1156 | 2551 | int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt) | |
1157 | { | ||
1158 | const AVPixFmtDescriptor *desc; | ||
1159 | enum AVPixelFormat *choices; | ||
1160 | enum AVPixelFormat ret, user_choice; | ||
1161 | const AVCodecHWConfigInternal *hw_config; | ||
1162 | const AVCodecHWConfig *config; | ||
1163 | int i, n, err; | ||
1164 | |||
1165 | // Find end of list. | ||
1166 |
2/2✓ Branch 0 taken 6645 times.
✓ Branch 1 taken 2551 times.
|
9196 | for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++); |
1167 | // Must contain at least one entry. | ||
1168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
|
2551 | av_assert0(n >= 1); |
1169 | // If a software format is available, it must be the last entry. | ||
1170 | 2551 | desc = av_pix_fmt_desc_get(fmt[n - 1]); | |
1171 |
1/2✓ Branch 0 taken 2551 times.
✗ Branch 1 not taken.
|
2551 | if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) { |
1172 | // No software format is available. | ||
1173 | } else { | ||
1174 | 2551 | avctx->sw_pix_fmt = fmt[n - 1]; | |
1175 | } | ||
1176 | |||
1177 | 2551 | choices = av_memdup(fmt, (n + 1) * sizeof(*choices)); | |
1178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
|
2551 | if (!choices) |
1179 | ✗ | return AV_PIX_FMT_NONE; | |
1180 | |||
1181 | for (;;) { | ||
1182 | // Remove the previous hwaccel, if there was one. | ||
1183 | 2551 | hwaccel_uninit(avctx); | |
1184 | |||
1185 | 2551 | user_choice = avctx->get_format(avctx, choices); | |
1186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
|
2551 | if (user_choice == AV_PIX_FMT_NONE) { |
1187 | // Explicitly chose nothing, give up. | ||
1188 | ✗ | ret = AV_PIX_FMT_NONE; | |
1189 | ✗ | break; | |
1190 | } | ||
1191 | |||
1192 | 2551 | desc = av_pix_fmt_desc_get(user_choice); | |
1193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
|
2551 | if (!desc) { |
1194 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid format returned by " | |
1195 | "get_format() callback.\n"); | ||
1196 | ✗ | ret = AV_PIX_FMT_NONE; | |
1197 | ✗ | break; | |
1198 | } | ||
1199 | 2551 | av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n", | |
1200 | 2551 | desc->name); | |
1201 | |||
1202 |
1/2✓ Branch 0 taken 6645 times.
✗ Branch 1 not taken.
|
6645 | for (i = 0; i < n; i++) { |
1203 |
2/2✓ Branch 0 taken 2551 times.
✓ Branch 1 taken 4094 times.
|
6645 | if (choices[i] == user_choice) |
1204 | 2551 | break; | |
1205 | } | ||
1206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
|
2551 | if (i == n) { |
1207 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): " | |
1208 | ✗ | "%s not in possible list.\n", desc->name); | |
1209 | ✗ | ret = AV_PIX_FMT_NONE; | |
1210 | ✗ | break; | |
1211 | } | ||
1212 | |||
1213 |
2/2✓ Branch 1 taken 2427 times.
✓ Branch 2 taken 124 times.
|
2551 | if (ffcodec(avctx->codec)->hw_configs) { |
1214 | 6728 | for (i = 0;; i++) { | |
1215 | 6728 | hw_config = ffcodec(avctx->codec)->hw_configs[i]; | |
1216 |
2/2✓ Branch 0 taken 2427 times.
✓ Branch 1 taken 4301 times.
|
6728 | if (!hw_config) |
1217 | 2427 | break; | |
1218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4301 times.
|
4301 | if (hw_config->public.pix_fmt == user_choice) |
1219 | ✗ | break; | |
1220 | } | ||
1221 | } else { | ||
1222 | 124 | hw_config = NULL; | |
1223 | } | ||
1224 | |||
1225 |
1/2✓ Branch 0 taken 2551 times.
✗ Branch 1 not taken.
|
2551 | if (!hw_config) { |
1226 | // No config available, so no extra setup required. | ||
1227 | 2551 | ret = user_choice; | |
1228 | 2551 | break; | |
1229 | } | ||
1230 | ✗ | config = &hw_config->public; | |
1231 | |||
1232 | ✗ | if (config->methods & | |
1233 | ✗ | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX && | |
1234 | ✗ | avctx->hw_frames_ctx) { | |
1235 | ✗ | const AVHWFramesContext *frames_ctx = | |
1236 | ✗ | (AVHWFramesContext*)avctx->hw_frames_ctx->data; | |
1237 | ✗ | if (frames_ctx->format != user_choice) { | |
1238 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: " | |
1239 | "does not match the format of the provided frames " | ||
1240 | ✗ | "context.\n", desc->name); | |
1241 | ✗ | goto try_again; | |
1242 | } | ||
1243 | ✗ | } else if (config->methods & | |
1244 | ✗ | AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && | |
1245 | ✗ | avctx->hw_device_ctx) { | |
1246 | ✗ | const AVHWDeviceContext *device_ctx = | |
1247 | ✗ | (AVHWDeviceContext*)avctx->hw_device_ctx->data; | |
1248 | ✗ | if (device_ctx->type != config->device_type) { | |
1249 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: " | |
1250 | "does not match the type of the provided device " | ||
1251 | ✗ | "context.\n", desc->name); | |
1252 | ✗ | goto try_again; | |
1253 | } | ||
1254 | ✗ | } else if (config->methods & | |
1255 | AV_CODEC_HW_CONFIG_METHOD_INTERNAL) { | ||
1256 | // Internal-only setup, no additional configuration. | ||
1257 | ✗ | } else if (config->methods & | |
1258 | AV_CODEC_HW_CONFIG_METHOD_AD_HOC) { | ||
1259 | // Some ad-hoc configuration we can't see and can't check. | ||
1260 | } else { | ||
1261 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: " | |
1262 | ✗ | "missing configuration.\n", desc->name); | |
1263 | ✗ | goto try_again; | |
1264 | } | ||
1265 | ✗ | if (hw_config->hwaccel) { | |
1266 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel " | |
1267 | ✗ | "initialisation.\n", desc->name); | |
1268 | ✗ | err = hwaccel_init(avctx, hw_config); | |
1269 | ✗ | if (err < 0) | |
1270 | ✗ | goto try_again; | |
1271 | } | ||
1272 | ✗ | ret = user_choice; | |
1273 | ✗ | break; | |
1274 | |||
1275 | ✗ | try_again: | |
1276 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying " | |
1277 | ✗ | "get_format() without it.\n", desc->name); | |
1278 | ✗ | for (i = 0; i < n; i++) { | |
1279 | ✗ | if (choices[i] == user_choice) | |
1280 | ✗ | break; | |
1281 | } | ||
1282 | ✗ | for (; i + 1 < n; i++) | |
1283 | ✗ | choices[i] = choices[i + 1]; | |
1284 | ✗ | --n; | |
1285 | } | ||
1286 | |||
1287 | 2551 | av_freep(&choices); | |
1288 | 2551 | return ret; | |
1289 | } | ||
1290 | |||
1291 | 413398 | static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame) | |
1292 | { | ||
1293 | size_t size; | ||
1294 | const uint8_t *side_metadata; | ||
1295 | |||
1296 | 413398 | AVDictionary **frame_md = &frame->metadata; | |
1297 | |||
1298 | 413398 | side_metadata = av_packet_get_side_data(avpkt, | |
1299 | AV_PKT_DATA_STRINGS_METADATA, &size); | ||
1300 | 413398 | return av_packet_unpack_dictionary(side_metadata, size, frame_md); | |
1301 | } | ||
1302 | |||
1303 | 413398 | int ff_decode_frame_props_from_pkt(const AVCodecContext *avctx, | |
1304 | AVFrame *frame, const AVPacket *pkt) | ||
1305 | { | ||
1306 | static const struct { | ||
1307 | enum AVPacketSideDataType packet; | ||
1308 | enum AVFrameSideDataType frame; | ||
1309 | } sd[] = { | ||
1310 | { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN }, | ||
1311 | { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX }, | ||
1312 | { AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL }, | ||
1313 | { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D }, | ||
1314 | { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE }, | ||
1315 | { AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA }, | ||
1316 | { AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL }, | ||
1317 | { AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC }, | ||
1318 | { AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE }, | ||
1319 | { AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE }, | ||
1320 | { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS }, | ||
1321 | }; | ||
1322 | |||
1323 | 413398 | frame->pts = pkt->pts; | |
1324 | 413398 | frame->duration = pkt->duration; | |
1325 | #if FF_API_FRAME_PKT | ||
1326 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1327 | 413398 | frame->pkt_pos = pkt->pos; | |
1328 | 413398 | frame->pkt_size = pkt->size; | |
1329 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1330 | #endif | ||
1331 | |||
1332 |
2/2✓ Branch 0 taken 4547378 times.
✓ Branch 1 taken 413398 times.
|
4960776 | for (int i = 0; i < FF_ARRAY_ELEMS(sd); i++) { |
1333 | size_t size; | ||
1334 | 4547378 | uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size); | |
1335 |
2/2✓ Branch 0 taken 211 times.
✓ Branch 1 taken 4547167 times.
|
4547378 | if (packet_sd) { |
1336 | 211 | AVFrameSideData *frame_sd = av_frame_new_side_data(frame, | |
1337 | 211 | sd[i].frame, | |
1338 | size); | ||
1339 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
|
211 | if (!frame_sd) |
1340 | ✗ | return AVERROR(ENOMEM); | |
1341 | |||
1342 | 211 | memcpy(frame_sd->data, packet_sd, size); | |
1343 | } | ||
1344 | } | ||
1345 | 413398 | add_metadata_from_side_data(pkt, frame); | |
1346 | |||
1347 |
2/2✓ Branch 0 taken 202 times.
✓ Branch 1 taken 413196 times.
|
413398 | if (pkt->flags & AV_PKT_FLAG_DISCARD) { |
1348 | 202 | frame->flags |= AV_FRAME_FLAG_DISCARD; | |
1349 | } else { | ||
1350 | 413196 | frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD); | |
1351 | } | ||
1352 | |||
1353 |
2/2✓ Branch 0 taken 3155 times.
✓ Branch 1 taken 410243 times.
|
413398 | if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) { |
1354 | 3155 | int ret = av_buffer_replace(&frame->opaque_ref, pkt->opaque_ref); | |
1355 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3155 times.
|
3155 | if (ret < 0) |
1356 | ✗ | return ret; | |
1357 | 3155 | frame->opaque = pkt->opaque; | |
1358 | } | ||
1359 | |||
1360 | 413398 | return 0; | |
1361 | } | ||
1362 | |||
1363 | 413398 | int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame) | |
1364 | { | ||
1365 | 413398 | const AVPacket *pkt = avctx->internal->last_pkt_props; | |
1366 | |||
1367 |
1/2✓ Branch 1 taken 413398 times.
✗ Branch 2 not taken.
|
413398 | if (!(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS)) { |
1368 | 413398 | int ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt); | |
1369 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 413398 times.
|
413398 | if (ret < 0) |
1370 | ✗ | return ret; | |
1371 | #if FF_API_FRAME_PKT | ||
1372 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1373 | 413398 | frame->pkt_size = pkt->stream_index; | |
1374 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1375 | #endif | ||
1376 | } | ||
1377 | #if FF_API_REORDERED_OPAQUE | ||
1378 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1379 | 413398 | frame->reordered_opaque = avctx->reordered_opaque; | |
1380 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1381 | #endif | ||
1382 | |||
1383 |
2/2✓ Branch 0 taken 413362 times.
✓ Branch 1 taken 36 times.
|
413398 | if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED) |
1384 | 413362 | frame->color_primaries = avctx->color_primaries; | |
1385 |
2/2✓ Branch 0 taken 413362 times.
✓ Branch 1 taken 36 times.
|
413398 | if (frame->color_trc == AVCOL_TRC_UNSPECIFIED) |
1386 | 413362 | frame->color_trc = avctx->color_trc; | |
1387 |
2/2✓ Branch 0 taken 412904 times.
✓ Branch 1 taken 494 times.
|
413398 | if (frame->colorspace == AVCOL_SPC_UNSPECIFIED) |
1388 | 412904 | frame->colorspace = avctx->colorspace; | |
1389 |
2/2✓ Branch 0 taken 411946 times.
✓ Branch 1 taken 1452 times.
|
413398 | if (frame->color_range == AVCOL_RANGE_UNSPECIFIED) |
1390 | 411946 | frame->color_range = avctx->color_range; | |
1391 |
2/2✓ Branch 0 taken 413213 times.
✓ Branch 1 taken 185 times.
|
413398 | if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED) |
1392 | 413213 | frame->chroma_location = avctx->chroma_sample_location; | |
1393 | |||
1394 |
2/3✓ Branch 0 taken 111790 times.
✓ Branch 1 taken 301608 times.
✗ Branch 2 not taken.
|
413398 | switch (avctx->codec->type) { |
1395 | 111790 | case AVMEDIA_TYPE_VIDEO: | |
1396 | 111790 | frame->format = avctx->pix_fmt; | |
1397 |
2/2✓ Branch 0 taken 111026 times.
✓ Branch 1 taken 764 times.
|
111790 | if (!frame->sample_aspect_ratio.num) |
1398 | 111026 | frame->sample_aspect_ratio = avctx->sample_aspect_ratio; | |
1399 | |||
1400 |
4/6✓ Branch 0 taken 89998 times.
✓ Branch 1 taken 21792 times.
✓ Branch 2 taken 89998 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 89998 times.
|
201788 | if (frame->width && frame->height && |
1401 | 89998 | av_image_check_sar(frame->width, frame->height, | |
1402 | frame->sample_aspect_ratio) < 0) { | ||
1403 | ✗ | av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n", | |
1404 | frame->sample_aspect_ratio.num, | ||
1405 | frame->sample_aspect_ratio.den); | ||
1406 | ✗ | frame->sample_aspect_ratio = (AVRational){ 0, 1 }; | |
1407 | } | ||
1408 | |||
1409 | 111790 | break; | |
1410 | 301608 | case AVMEDIA_TYPE_AUDIO: | |
1411 |
1/2✓ Branch 0 taken 301608 times.
✗ Branch 1 not taken.
|
301608 | if (!frame->sample_rate) |
1412 | 301608 | frame->sample_rate = avctx->sample_rate; | |
1413 |
2/2✓ Branch 0 taken 301584 times.
✓ Branch 1 taken 24 times.
|
301608 | if (frame->format < 0) |
1414 | 301584 | frame->format = avctx->sample_fmt; | |
1415 |
2/2✓ Branch 0 taken 301584 times.
✓ Branch 1 taken 24 times.
|
301608 | if (!frame->ch_layout.nb_channels) { |
1416 | 301584 | int ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); | |
1417 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 301584 times.
|
301584 | if (ret < 0) |
1418 | ✗ | return ret; | |
1419 | } | ||
1420 | #if FF_API_OLD_CHANNEL_LAYOUT | ||
1421 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1422 | 301608 | frame->channels = frame->ch_layout.nb_channels; | |
1423 | 603216 | frame->channel_layout = frame->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ? | |
1424 |
2/2✓ Branch 0 taken 300469 times.
✓ Branch 1 taken 1139 times.
|
301608 | frame->ch_layout.u.mask : 0; |
1425 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1426 | #endif | ||
1427 | 301608 | break; | |
1428 | } | ||
1429 | 413398 | return 0; | |
1430 | } | ||
1431 | |||
1432 | 385461 | static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame) | |
1433 | { | ||
1434 |
2/2✓ Branch 0 taken 83853 times.
✓ Branch 1 taken 301608 times.
|
385461 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
1435 | int i; | ||
1436 | 83853 | int num_planes = av_pix_fmt_count_planes(frame->format); | |
1437 | 83853 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); | |
1438 |
1/2✓ Branch 0 taken 83853 times.
✗ Branch 1 not taken.
|
83853 | int flags = desc ? desc->flags : 0; |
1439 |
4/4✓ Branch 0 taken 8037 times.
✓ Branch 1 taken 75816 times.
✓ Branch 2 taken 2013 times.
✓ Branch 3 taken 6024 times.
|
83853 | if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL)) |
1440 | 2013 | num_planes = 2; | |
1441 |
2/2✓ Branch 0 taken 238186 times.
✓ Branch 1 taken 83853 times.
|
322039 | for (i = 0; i < num_planes; i++) { |
1442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 238186 times.
|
238186 | av_assert0(frame->data[i]); |
1443 | } | ||
1444 | // For formats without data like hwaccel allow unused pointers to be non-NULL. | ||
1445 |
3/4✓ Branch 0 taken 516491 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 432638 times.
✓ Branch 3 taken 83853 times.
|
516491 | for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) { |
1446 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 432638 times.
|
432638 | if (frame->data[i]) |
1447 | ✗ | av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n"); | |
1448 | 432638 | frame->data[i] = NULL; | |
1449 | } | ||
1450 | } | ||
1451 | 385461 | } | |
1452 | |||
1453 | 385461 | static void decode_data_free(void *opaque, uint8_t *data) | |
1454 | { | ||
1455 | 385461 | FrameDecodeData *fdd = (FrameDecodeData*)data; | |
1456 | |||
1457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (fdd->post_process_opaque_free) |
1458 | ✗ | fdd->post_process_opaque_free(fdd->post_process_opaque); | |
1459 | |||
1460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (fdd->hwaccel_priv_free) |
1461 | ✗ | fdd->hwaccel_priv_free(fdd->hwaccel_priv); | |
1462 | |||
1463 | 385461 | av_freep(&fdd); | |
1464 | 385461 | } | |
1465 | |||
1466 | 385461 | int ff_attach_decode_data(AVFrame *frame) | |
1467 | { | ||
1468 | AVBufferRef *fdd_buf; | ||
1469 | FrameDecodeData *fdd; | ||
1470 | |||
1471 | av_assert1(!frame->private_ref); | ||
1472 | 385461 | av_buffer_unref(&frame->private_ref); | |
1473 | |||
1474 | 385461 | fdd = av_mallocz(sizeof(*fdd)); | |
1475 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (!fdd) |
1476 | ✗ | return AVERROR(ENOMEM); | |
1477 | |||
1478 | 385461 | fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free, | |
1479 | NULL, AV_BUFFER_FLAG_READONLY); | ||
1480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (!fdd_buf) { |
1481 | ✗ | av_freep(&fdd); | |
1482 | ✗ | return AVERROR(ENOMEM); | |
1483 | } | ||
1484 | |||
1485 | 385461 | frame->private_ref = fdd_buf; | |
1486 | |||
1487 | 385461 | return 0; | |
1488 | } | ||
1489 | |||
1490 | 385461 | int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) | |
1491 | { | ||
1492 | 385461 | const AVHWAccel *hwaccel = avctx->hwaccel; | |
1493 | 385461 | int override_dimensions = 1; | |
1494 | int ret; | ||
1495 | |||
1496 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 385461 times.
|
385461 | av_assert0(av_codec_is_decoder(avctx->codec)); |
1497 | |||
1498 |
2/2✓ Branch 0 taken 83853 times.
✓ Branch 1 taken 301608 times.
|
385461 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
1499 |
2/4✓ Branch 0 taken 83853 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83853 times.
✗ Branch 3 not taken.
|
167706 | if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN || |
1500 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 83853 times.
|
167706 | (ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) { |
1501 | ✗ | av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n"); | |
1502 | ✗ | ret = AVERROR(EINVAL); | |
1503 | ✗ | goto fail; | |
1504 | } | ||
1505 | |||
1506 |
3/4✓ Branch 0 taken 729 times.
✓ Branch 1 taken 83124 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729 times.
|
83853 | if (frame->width <= 0 || frame->height <= 0) { |
1507 | 83124 | frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres)); | |
1508 | 83124 | frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres)); | |
1509 | 83124 | override_dimensions = 0; | |
1510 | } | ||
1511 | |||
1512 |
4/8✓ Branch 0 taken 83853 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83853 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83853 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 83853 times.
|
83853 | if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) { |
1513 | ✗ | av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n"); | |
1514 | ✗ | ret = AVERROR(EINVAL); | |
1515 | ✗ | goto fail; | |
1516 | } | ||
1517 |
1/2✓ Branch 0 taken 301608 times.
✗ Branch 1 not taken.
|
301608 | } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
1518 | #if FF_API_OLD_CHANNEL_LAYOUT | ||
1519 | FF_DISABLE_DEPRECATION_WARNINGS | ||
1520 | /* compat layer for old-style get_buffer() implementations */ | ||
1521 | 301608 | avctx->channels = avctx->ch_layout.nb_channels; | |
1522 | 603216 | avctx->channel_layout = (avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) ? | |
1523 |
2/2✓ Branch 0 taken 300493 times.
✓ Branch 1 taken 1115 times.
|
301608 | avctx->ch_layout.u.mask : 0; |
1524 | FF_ENABLE_DEPRECATION_WARNINGS | ||
1525 | #endif | ||
1526 | |||
1527 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 301608 times.
|
301608 | if (frame->nb_samples * (int64_t)avctx->ch_layout.nb_channels > avctx->max_samples) { |
1528 | ✗ | av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples); | |
1529 | ✗ | ret = AVERROR(EINVAL); | |
1530 | ✗ | goto fail; | |
1531 | } | ||
1532 | } | ||
1533 | 385461 | ret = ff_decode_frame_props(avctx, frame); | |
1534 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (ret < 0) |
1535 | ✗ | goto fail; | |
1536 | |||
1537 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (hwaccel) { |
1538 | ✗ | if (hwaccel->alloc_frame) { | |
1539 | ✗ | ret = hwaccel->alloc_frame(avctx, frame); | |
1540 | ✗ | goto end; | |
1541 | } | ||
1542 | } else | ||
1543 | 385461 | avctx->sw_pix_fmt = avctx->pix_fmt; | |
1544 | |||
1545 | 385461 | ret = avctx->get_buffer2(avctx, frame, flags); | |
1546 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (ret < 0) |
1547 | ✗ | goto fail; | |
1548 | |||
1549 | 385461 | validate_avframe_allocation(avctx, frame); | |
1550 | |||
1551 | 385461 | ret = ff_attach_decode_data(frame); | |
1552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (ret < 0) |
1553 | ✗ | goto fail; | |
1554 | |||
1555 | 385461 | end: | |
1556 |
4/4✓ Branch 0 taken 301608 times.
✓ Branch 1 taken 83853 times.
✓ Branch 2 taken 729 times.
✓ Branch 3 taken 83124 times.
|
385461 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions && |
1557 |
2/2✓ Branch 1 taken 33799 times.
✓ Branch 2 taken 49325 times.
|
83124 | !(ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) { |
1558 | 49325 | frame->width = avctx->width; | |
1559 | 49325 | frame->height = avctx->height; | |
1560 | } | ||
1561 | |||
1562 | 336136 | fail: | |
1563 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 385461 times.
|
385461 | if (ret < 0) { |
1564 | ✗ | av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
1565 | ✗ | av_frame_unref(frame); | |
1566 | } | ||
1567 | |||
1568 | 385461 | return ret; | |
1569 | } | ||
1570 | |||
1571 | 7089 | static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags) | |
1572 | { | ||
1573 | AVFrame *tmp; | ||
1574 | int ret; | ||
1575 | |||
1576 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7089 times.
|
7089 | av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO); |
1577 | |||
1578 |
5/8✓ Branch 0 taken 6965 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 6965 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6965 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6965 times.
|
7089 | if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) { |
1579 | ✗ | av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n", | |
1580 | ✗ | frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt)); | |
1581 | ✗ | av_frame_unref(frame); | |
1582 | } | ||
1583 | |||
1584 |
2/2✓ Branch 0 taken 124 times.
✓ Branch 1 taken 6965 times.
|
7089 | if (!frame->data[0]) |
1585 | 124 | return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF); | |
1586 | |||
1587 |
4/4✓ Branch 0 taken 6964 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 5326 times.
✓ Branch 4 taken 1638 times.
|
6965 | if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame)) |
1588 | 5327 | return ff_decode_frame_props(avctx, frame); | |
1589 | |||
1590 | 1638 | tmp = av_frame_alloc(); | |
1591 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1638 times.
|
1638 | if (!tmp) |
1592 | ✗ | return AVERROR(ENOMEM); | |
1593 | |||
1594 | 1638 | av_frame_move_ref(tmp, frame); | |
1595 | |||
1596 | 1638 | ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF); | |
1597 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1638 times.
|
1638 | if (ret < 0) { |
1598 | ✗ | av_frame_free(&tmp); | |
1599 | ✗ | return ret; | |
1600 | } | ||
1601 | |||
1602 | 1638 | av_frame_copy(frame, tmp); | |
1603 | 1638 | av_frame_free(&tmp); | |
1604 | |||
1605 | 1638 | return 0; | |
1606 | } | ||
1607 | |||
1608 | 7089 | int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) | |
1609 | { | ||
1610 | 7089 | int ret = reget_buffer_internal(avctx, frame, flags); | |
1611 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7089 times.
|
7089 | if (ret < 0) |
1612 | ✗ | av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); | |
1613 | 7089 | return ret; | |
1614 | } | ||
1615 | |||
1616 | 13854 | int ff_decode_preinit(AVCodecContext *avctx) | |
1617 | { | ||
1618 | 13854 | AVCodecInternal *avci = avctx->internal; | |
1619 | 13854 | int ret = 0; | |
1620 | |||
1621 | /* if the decoder init function was already called previously, | ||
1622 | * free the already allocated subtitle_header before overwriting it */ | ||
1623 | 13854 | av_freep(&avctx->subtitle_header); | |
1624 | |||
1625 |
2/4✓ Branch 0 taken 13854 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13854 times.
|
13854 | if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) { |
1626 | ✗ | av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n", | |
1627 | ✗ | avctx->codec->max_lowres); | |
1628 | ✗ | avctx->lowres = avctx->codec->max_lowres; | |
1629 | } | ||
1630 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13848 times.
|
13854 | if (avctx->sub_charenc) { |
1631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) { |
1632 | ✗ | av_log(avctx, AV_LOG_ERROR, "Character encoding is only " | |
1633 | "supported with subtitles codecs\n"); | ||
1634 | ✗ | return AVERROR(EINVAL); | |
1635 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) { |
1636 | ✗ | av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, " | |
1637 | "subtitles character encoding will be ignored\n", | ||
1638 | ✗ | avctx->codec_descriptor->name); | |
1639 | ✗ | avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING; | |
1640 | } else { | ||
1641 | /* input character encoding is set for a text based subtitle | ||
1642 | * codec at this point */ | ||
1643 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC) |
1644 | 6 | avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER; | |
1645 | |||
1646 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) { |
1647 | #if CONFIG_ICONV | ||
1648 | 6 | iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc); | |
1649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (cd == (iconv_t)-1) { |
1650 | ✗ | ret = AVERROR(errno); | |
1651 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context " | |
1652 | "with input character encoding \"%s\"\n", avctx->sub_charenc); | ||
1653 | ✗ | return ret; | |
1654 | } | ||
1655 | 6 | iconv_close(cd); | |
1656 | #else | ||
1657 | av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles " | ||
1658 | "conversion needs a libavcodec built with iconv support " | ||
1659 | "for this codec\n"); | ||
1660 | return AVERROR(ENOSYS); | ||
1661 | #endif | ||
1662 | } | ||
1663 | } | ||
1664 | } | ||
1665 | |||
1666 | 13854 | avctx->pts_correction_num_faulty_pts = | |
1667 | 13854 | avctx->pts_correction_num_faulty_dts = 0; | |
1668 | 13854 | avctx->pts_correction_last_pts = | |
1669 | 13854 | avctx->pts_correction_last_dts = INT64_MIN; | |
1670 | |||
1671 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13854 times.
|
13854 | if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY |
1672 | ✗ | && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO) | |
1673 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
1674 | "gray decoding requested but not enabled at configuration time\n"); | ||
1675 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13849 times.
|
13854 | if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) { |
1676 | 5 | avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS; | |
1677 | } | ||
1678 | |||
1679 | 13854 | avci->in_pkt = av_packet_alloc(); | |
1680 | 13854 | avci->last_pkt_props = av_packet_alloc(); | |
1681 |
2/4✓ Branch 0 taken 13854 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13854 times.
|
13854 | if (!avci->in_pkt || !avci->last_pkt_props) |
1682 | ✗ | return AVERROR(ENOMEM); | |
1683 | |||
1684 | 13854 | ret = decode_bsfs_init(avctx); | |
1685 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13854 times.
|
13854 | if (ret < 0) |
1686 | ✗ | return ret; | |
1687 | |||
1688 | 13854 | return 0; | |
1689 | } | ||
1690 | |||
1691 | 1263 | int ff_copy_palette(void *dst, const AVPacket *src, void *logctx) | |
1692 | { | ||
1693 | size_t size; | ||
1694 | 1263 | const void *pal = av_packet_get_side_data(src, AV_PKT_DATA_PALETTE, &size); | |
1695 | |||
1696 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1239 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
1263 | if (pal && size == AVPALETTE_SIZE) { |
1697 | 24 | memcpy(dst, pal, AVPALETTE_SIZE); | |
1698 | 24 | return 1; | |
1699 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
|
1239 | } else if (pal) { |
1700 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
1701 | "Palette size %"SIZE_SPECIFIER" is wrong\n", size); | ||
1702 | } | ||
1703 | 1239 | return 0; | |
1704 | } | ||
1705 |