Lines 30-36
Link Here
|
30 |
#include <strings.h> |
30 |
#include <strings.h> |
31 |
#include <assert.h> |
31 |
#include <assert.h> |
32 |
#include <stdint.h> |
32 |
#include <stdint.h> |
|
|
33 |
#include <errno.h> |
33 |
|
34 |
|
|
|
35 |
#include <libavcodec/avcodec.h> |
34 |
#include <libavformat/avformat.h> |
36 |
#include <libavformat/avformat.h> |
35 |
#include <libavutil/mathematics.h> |
37 |
#include <libavutil/mathematics.h> |
36 |
#if HAVE_LIBAVUTIL_CHANNEL_LAYOUT_H |
38 |
#if HAVE_LIBAVUTIL_CHANNEL_LAYOUT_H |
Lines 55-60
Link Here
|
55 |
#include "files.h" |
57 |
#include "files.h" |
56 |
#include "lists.h" |
58 |
#include "lists.h" |
57 |
|
59 |
|
|
|
60 |
#ifndef AV_CODEC_CAP_DELAY |
61 |
# define AV_CODEC_CAP_DELAY CODEC_CAP_DELAY |
62 |
# define AV_CODEC_CAP_EXPERIMENTAL CODEC_CAP_EXPERIMENTAL |
63 |
# define AV_CODEC_CAP_TRUNCATED CODEC_CAP_TRUNCATED |
64 |
#endif |
65 |
|
66 |
#ifndef AV_CODEC_FLAG_TRUNCATED |
67 |
# define AV_CODEC_FLAG_TRUNCATED CODEC_FLAG_TRUNCATED |
68 |
#endif |
69 |
|
58 |
/* Set SEEK_IN_DECODER to 1 if you'd prefer seeking to be delay until |
70 |
/* Set SEEK_IN_DECODER to 1 if you'd prefer seeking to be delay until |
59 |
* the next time ffmpeg_decode() is called. This will provide seeking |
71 |
* the next time ffmpeg_decode() is called. This will provide seeking |
60 |
* in formats for which FFmpeg falsely reports seek errors, but could |
72 |
* in formats for which FFmpeg falsely reports seek errors, but could |
Lines 195-201
Link Here
|
195 |
assert (ic); |
207 |
assert (ic); |
196 |
|
208 |
|
197 |
for (result = 0; result < ic->nb_streams; result += 1) { |
209 |
for (result = 0; result < ic->nb_streams; result += 1) { |
198 |
if (ic->streams[result]->codec->codec_type == AVMEDIA_TYPE_AUDIO) |
210 |
enum AVMediaType codec_type; |
|
|
211 |
|
212 |
#ifdef HAVE_STRUCT_AVSTREAM_CODECPAR |
213 |
codec_type = ic->streams[result]->codecpar->codec_type; |
214 |
#else |
215 |
codec_type = ic->streams[result]->codec->codec_type; |
216 |
#endif |
217 |
|
218 |
if (codec_type == AVMEDIA_TYPE_AUDIO) |
199 |
break; |
219 |
break; |
200 |
} |
220 |
} |
201 |
|
221 |
|
Lines 292-297
Link Here
|
292 |
} |
312 |
} |
293 |
|
313 |
|
294 |
/* Handle FFmpeg's locking requirements. */ |
314 |
/* Handle FFmpeg's locking requirements. */ |
|
|
315 |
#if HAVE_LIBAV || LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,9,100) |
295 |
static int locking_cb (void **mutex, enum AVLockOp op) |
316 |
static int locking_cb (void **mutex, enum AVLockOp op) |
296 |
{ |
317 |
{ |
297 |
int result; |
318 |
int result; |
Lines 322-327
Link Here
|
322 |
|
343 |
|
323 |
return result; |
344 |
return result; |
324 |
} |
345 |
} |
|
|
346 |
#endif |
325 |
|
347 |
|
326 |
/* Here we attempt to determine if FFmpeg/LibAV has trashed the 'duration' |
348 |
/* Here we attempt to determine if FFmpeg/LibAV has trashed the 'duration' |
327 |
* and 'bit_rate' fields in AVFormatContext for large files. Determining |
349 |
* and 'bit_rate' fields in AVFormatContext for large files. Determining |
Lines 366-373
Link Here
|
366 |
|
388 |
|
367 |
static void ffmpeg_init () |
389 |
static void ffmpeg_init () |
368 |
{ |
390 |
{ |
369 |
int rc; |
|
|
370 |
|
371 |
#ifndef NDEBUG |
391 |
#ifndef NDEBUG |
372 |
# ifdef DEBUG |
392 |
# ifdef DEBUG |
373 |
av_log_set_level (AV_LOG_INFO); |
393 |
av_log_set_level (AV_LOG_INFO); |
Lines 376-400
Link Here
|
376 |
# endif |
396 |
# endif |
377 |
av_log_set_callback (ffmpeg_log_cb); |
397 |
av_log_set_callback (ffmpeg_log_cb); |
378 |
#endif |
398 |
#endif |
|
|
399 |
|
400 |
#if HAVE_LIBAV || LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,10,100) |
379 |
avcodec_register_all (); |
401 |
avcodec_register_all (); |
380 |
av_register_all (); |
402 |
av_register_all (); |
|
|
403 |
#endif |
381 |
|
404 |
|
382 |
supported_extns = lists_strs_new (16); |
405 |
supported_extns = lists_strs_new (16); |
383 |
load_audio_extns (supported_extns); |
406 |
load_audio_extns (supported_extns); |
384 |
load_video_extns (supported_extns); |
407 |
load_video_extns (supported_extns); |
385 |
|
408 |
|
386 |
rc = av_lockmgr_register (locking_cb); |
409 |
#if HAVE_LIBAV || LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,9,100) |
|
|
410 |
int rc = av_lockmgr_register (locking_cb); |
387 |
if (rc < 0) { |
411 |
if (rc < 0) { |
388 |
char buf[128]; |
412 |
char buf[128]; |
389 |
|
413 |
|
390 |
av_strerror (rc, buf, sizeof (buf)); |
414 |
av_strerror (rc, buf, sizeof (buf)); |
391 |
fatal ("Lock manager initialisation failed: %s", buf); |
415 |
fatal ("Lock manager initialisation failed: %s", buf); |
392 |
} |
416 |
} |
|
|
417 |
#endif |
393 |
} |
418 |
} |
394 |
|
419 |
|
395 |
static void ffmpeg_destroy () |
420 |
static void ffmpeg_destroy () |
396 |
{ |
421 |
{ |
|
|
422 |
#if HAVE_LIBAV || LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,9,100) |
397 |
av_lockmgr_register (NULL); |
423 |
av_lockmgr_register (NULL); |
|
|
424 |
#endif |
398 |
|
425 |
|
399 |
av_log_set_level (AV_LOG_QUIET); |
426 |
av_log_set_level (AV_LOG_QUIET); |
400 |
ffmpeg_log_repeats (NULL); |
427 |
ffmpeg_log_repeats (NULL); |
Lines 508-514
Link Here
|
508 |
#endif |
535 |
#endif |
509 |
|
536 |
|
510 |
/* How much do we trust this? */ |
537 |
/* How much do we trust this? */ |
511 |
if (!data->ic->pb->seekable) { |
538 |
if (!(data->ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) { |
512 |
debug ("Seek broken by AVIOContext.seekable"); |
539 |
debug ("Seek broken by AVIOContext.seekable"); |
513 |
return true; |
540 |
return true; |
514 |
} |
541 |
} |
Lines 543-552
Link Here
|
543 |
|
570 |
|
544 |
static int ffmpeg_io_read_cb (void *s, uint8_t *buf, int count) |
571 |
static int ffmpeg_io_read_cb (void *s, uint8_t *buf, int count) |
545 |
{ |
572 |
{ |
546 |
if (!buf || count == 0) |
573 |
int len; |
547 |
return 0; |
|
|
548 |
|
574 |
|
549 |
return io_read ((struct io_stream *)s, buf, (size_t)count); |
575 |
if (!buf || count <= 0) |
|
|
576 |
return AVERROR(EINVAL); |
577 |
|
578 |
len = io_read ((struct io_stream *)s, buf, (size_t)count); |
579 |
if (len == 0) |
580 |
len = AVERROR_EOF; |
581 |
else if (len < 0) |
582 |
len = AVERROR(EIO); |
583 |
|
584 |
return len; |
550 |
} |
585 |
} |
551 |
|
586 |
|
552 |
static int64_t ffmpeg_io_seek_cb (void *s, int64_t offset, int whence) |
587 |
static int64_t ffmpeg_io_seek_cb (void *s, int64_t offset, int whence) |
Lines 673-679
Link Here
|
673 |
} |
708 |
} |
674 |
|
709 |
|
675 |
data->stream = data->ic->streams[audio_ix]; |
710 |
data->stream = data->ic->streams[audio_ix]; |
676 |
data->enc = data->stream->codec; |
711 |
|
|
|
712 |
data->enc = avcodec_alloc_context3 (NULL); |
713 |
if (!data->enc) { |
714 |
decoder_error (&data->error, ERROR_FATAL, 0, |
715 |
"Failed to allocate codec context"); |
716 |
goto end; |
717 |
} |
718 |
|
719 |
#ifdef HAVE_STRUCT_AVSTREAM_CODECPAR |
720 |
err = avcodec_parameters_to_context (data->enc, data->stream->codecpar); |
721 |
if (err < 0) { |
722 |
decoder_error (&data->error, ERROR_FATAL, 0, |
723 |
"Failed to copy codec parameters"); |
724 |
goto end; |
725 |
} |
726 |
#else |
727 |
err = avcodec_copy_context (data->enc, data->stream->codec); |
728 |
if (err < 0) { |
729 |
decoder_error (&data->error, ERROR_FATAL, 0, |
730 |
"Failed to copy codec context"); |
731 |
goto end; |
732 |
} |
733 |
#endif |
677 |
|
734 |
|
678 |
data->codec = avcodec_find_decoder (data->enc->codec_id); |
735 |
data->codec = avcodec_find_decoder (data->enc->codec_id); |
679 |
if (!data->codec) { |
736 |
if (!data->codec) { |
Lines 705-712
Link Here
|
705 |
} |
762 |
} |
706 |
|
763 |
|
707 |
set_downmixing (data); |
764 |
set_downmixing (data); |
|
|
765 |
#if LIBAVCODEC_VERSION_MAJOR < 60 |
708 |
if (data->codec->capabilities & AV_CODEC_CAP_TRUNCATED) |
766 |
if (data->codec->capabilities & AV_CODEC_CAP_TRUNCATED) |
709 |
data->enc->flags |= AV_CODEC_FLAG_TRUNCATED; |
767 |
data->enc->flags |= AV_CODEC_FLAG_TRUNCATED; |
|
|
768 |
#endif |
710 |
|
769 |
|
711 |
if (avcodec_open2 (data->enc, data->codec, NULL) < 0) |
770 |
if (avcodec_open2 (data->enc, data->codec, NULL) < 0) |
712 |
{ |
771 |
{ |
Lines 719-725
Link Here
|
719 |
decoder_error (&data->error, ERROR_FATAL, 0, |
778 |
decoder_error (&data->error, ERROR_FATAL, 0, |
720 |
"Cannot get sample size from unknown sample format: %s", |
779 |
"Cannot get sample size from unknown sample format: %s", |
721 |
av_get_sample_fmt_name (data->enc->sample_fmt)); |
780 |
av_get_sample_fmt_name (data->enc->sample_fmt)); |
722 |
avcodec_close (data->enc); |
|
|
723 |
goto end; |
781 |
goto end; |
724 |
} |
782 |
} |
725 |
|
783 |
|
Lines 734-740
Link Here
|
734 |
ffmpeg_log_repeats (NULL); |
792 |
ffmpeg_log_repeats (NULL); |
735 |
decoder_error (&data->error, ERROR_FATAL, 0, |
793 |
decoder_error (&data->error, ERROR_FATAL, 0, |
736 |
"Broken WAV file; use W64!"); |
794 |
"Broken WAV file; use W64!"); |
737 |
avcodec_close (data->enc); |
|
|
738 |
goto end; |
795 |
goto end; |
739 |
} |
796 |
} |
740 |
|
797 |
|
Lines 750-755
Link Here
|
750 |
return data; |
807 |
return data; |
751 |
|
808 |
|
752 |
end: |
809 |
end: |
|
|
810 |
#ifdef HAVE_AVCODEC_FREE_CONTEXT |
811 |
avcodec_free_context (&data->enc); |
812 |
#else |
813 |
avcodec_close (data->enc); |
814 |
av_freep (&data->enc); |
815 |
#endif |
753 |
avformat_close_input (&data->ic); |
816 |
avformat_close_input (&data->ic); |
754 |
ffmpeg_log_repeats (NULL); |
817 |
ffmpeg_log_repeats (NULL); |
755 |
return data; |
818 |
return data; |
Lines 960-965
Link Here
|
960 |
return NULL; |
1023 |
return NULL; |
961 |
} |
1024 |
} |
962 |
|
1025 |
|
|
|
1026 |
#ifndef HAVE_AVCODEC_RECEIVE_FRAME |
1027 |
/* Decode samples from packet data using avcodec_decode_audio4(). */ |
1028 |
# define decode_audio avcodec_decode_audio4 |
1029 |
#endif |
1030 |
|
1031 |
#ifdef HAVE_AVCODEC_RECEIVE_FRAME |
1032 |
/* Decode audio using FFmpeg's send/receive encoding and decoding API. */ |
1033 |
static int decode_audio (AVCodecContext *ctx, AVFrame *frame, |
1034 |
int *got_frame_ptr, const AVPacket *pkt) |
1035 |
{ |
1036 |
int rc, result = 0; |
1037 |
|
1038 |
*got_frame_ptr = 0; |
1039 |
|
1040 |
rc = avcodec_send_packet (ctx, pkt); |
1041 |
switch (rc) { |
1042 |
case 0: |
1043 |
break; |
1044 |
case AVERROR(EAGAIN): |
1045 |
debug ("avcodec_send_packet(): AVERROR(EAGAIN)"); |
1046 |
break; |
1047 |
case AVERROR_EOF: |
1048 |
if (pkt->data) |
1049 |
debug ("avcodec_send_packet(): AVERROR_EOF"); |
1050 |
break; |
1051 |
case AVERROR(EINVAL): |
1052 |
logit ("avcodec_send_packet(): AVERROR(EINVAL)"); |
1053 |
result = rc; |
1054 |
break; |
1055 |
case AVERROR(ENOMEM): |
1056 |
logit ("avcodec_send_packet(): AVERROR(ENOMEM)"); |
1057 |
result = rc; |
1058 |
break; |
1059 |
default: |
1060 |
log_errno ("avcodec_send_packet()", rc); |
1061 |
result = rc; |
1062 |
} |
1063 |
|
1064 |
if (result == 0) { |
1065 |
result = pkt->size; |
1066 |
|
1067 |
rc = avcodec_receive_frame (ctx, frame); |
1068 |
switch (rc) { |
1069 |
case 0: |
1070 |
*got_frame_ptr = 1; |
1071 |
break; |
1072 |
case AVERROR(EAGAIN): |
1073 |
debug ("avcodec_receive_frame(): AVERROR(EAGAIN)"); |
1074 |
break; |
1075 |
case AVERROR_EOF: |
1076 |
debug ("avcodec_receive_frame(): AVERROR_EOF"); |
1077 |
avcodec_flush_buffers (ctx); |
1078 |
break; |
1079 |
case AVERROR(EINVAL): |
1080 |
logit ("avcodec_receive_frame(): AVERROR(EINVAL)"); |
1081 |
result = rc; |
1082 |
break; |
1083 |
default: |
1084 |
log_errno ("avcodec_receive_frame()", rc); |
1085 |
result = rc; |
1086 |
} |
1087 |
} |
1088 |
|
1089 |
return result; |
1090 |
} |
1091 |
#endif |
1092 |
|
963 |
/* Decode samples from packet data. */ |
1093 |
/* Decode samples from packet data. */ |
964 |
static int decode_packet (struct ffmpeg_data *data, AVPacket *pkt, |
1094 |
static int decode_packet (struct ffmpeg_data *data, AVPacket *pkt, |
965 |
char *buf, int buf_len) |
1095 |
char *buf, int buf_len) |
Lines 977-983
Link Here
|
977 |
do { |
1107 |
do { |
978 |
int len, got_frame, is_planar, packed_size, copied; |
1108 |
int len, got_frame, is_planar, packed_size, copied; |
979 |
|
1109 |
|
980 |
len = avcodec_decode_audio4 (data->enc, frame, &got_frame, pkt); |
1110 |
len = decode_audio (data->enc, frame, &got_frame, pkt); |
981 |
|
1111 |
|
982 |
if (len < 0) { |
1112 |
if (len < 0) { |
983 |
/* skip frame */ |
1113 |
/* skip frame */ |
Lines 1063-1077
Link Here
|
1063 |
data->stream->time_base.num); |
1193 |
data->stream->time_base.num); |
1064 |
|
1194 |
|
1065 |
if (data->stream->start_time != (int64_t)AV_NOPTS_VALUE) { |
1195 |
if (data->stream->start_time != (int64_t)AV_NOPTS_VALUE) { |
1066 |
if (seek_ts > INT64_MAX - data->stream->start_time) { |
1196 |
if (seek_ts > INT64_MAX - MAX(0, data->stream->start_time)) { |
1067 |
logit ("Seek value too large"); |
1197 |
logit ("Seek value too large"); |
1068 |
return false; |
1198 |
return false; |
1069 |
} |
1199 |
} |
1070 |
seek_ts += data->stream->start_time; |
1200 |
seek_ts += data->stream->start_time; |
1071 |
} |
1201 |
} |
1072 |
|
1202 |
|
|
|
1203 |
#if LIBAVCODEC_VERSION_MAJOR < 60 |
1073 |
if (data->stream->cur_dts > seek_ts) |
1204 |
if (data->stream->cur_dts > seek_ts) |
1074 |
flags |= AVSEEK_FLAG_BACKWARD; |
1205 |
flags |= AVSEEK_FLAG_BACKWARD; |
|
|
1206 |
#endif |
1075 |
|
1207 |
|
1076 |
rc = av_seek_frame (data->ic, data->stream->index, seek_ts, flags); |
1208 |
rc = av_seek_frame (data->ic, data->stream->index, seek_ts, flags); |
1077 |
if (rc < 0) { |
1209 |
if (rc < 0) { |
Lines 1079-1085
Link Here
|
1079 |
return false; |
1211 |
return false; |
1080 |
} |
1212 |
} |
1081 |
|
1213 |
|
1082 |
avcodec_flush_buffers (data->stream->codec); |
1214 |
avcodec_flush_buffers (data->enc); |
1083 |
|
1215 |
|
1084 |
return true; |
1216 |
return true; |
1085 |
} |
1217 |
} |
Lines 1210-1216
Link Here
|
1210 |
} |
1342 |
} |
1211 |
|
1343 |
|
1212 |
if (data->okay) { |
1344 |
if (data->okay) { |
|
|
1345 |
#ifdef HAVE_AVCODEC_FREE_CONTEXT |
1346 |
avcodec_free_context (&data->enc); |
1347 |
#else |
1213 |
avcodec_close (data->enc); |
1348 |
avcodec_close (data->enc); |
|
|
1349 |
av_freep (&data->enc); |
1350 |
#endif |
1214 |
avformat_close_input (&data->ic); |
1351 |
avformat_close_input (&data->ic); |
1215 |
free_remain_buf (data); |
1352 |
free_remain_buf (data); |
1216 |
} |
1353 |
} |