53 | 53 |
unsigned (* avcodec_version)(void);
|
54 | 54 |
AVCodec * (* avcodec_find_decoder)(int);
|
55 | 55 |
int attribute_align_arg (* avcodec_open2)(AVCodecContext *, const AVCodec *, AVDictionary **);
|
56 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)
|
57 | 56 |
AVFrame * (* av_frame_alloc)(void);
|
58 | 57 |
void (* av_frame_free)(AVFrame **);
|
59 | |
#else
|
60 | |
AVFrame * (* avcodec_alloc_frame)(void);
|
61 | |
void (* avcodec_free_frame)(AVFrame **);
|
62 | |
#endif
|
63 | |
int attribute_align_arg (* avcodec_decode_audio4)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
|
|
58 |
int attribute_align_arg (* avcodec_send_packet)(AVCodecContext *, const AVPacket *);
|
|
59 |
int attribute_align_arg (* avcodec_receive_frame)(AVCodecContext *, AVFrame *);
|
64 | 60 |
AVCodecContext * (* avcodec_alloc_context3)(const AVCodec *);
|
65 | 61 |
void (* avcodec_free_context)(AVCodecContext **);
|
66 | 62 |
int (* avcodec_parameters_to_context)(AVCodecContext *, const AVCodecParameters *);
|
|
72 | 68 |
int (* avformat_find_stream_info)(AVFormatContext *, AVDictionary **);
|
73 | 69 |
AVIOContext * (* avio_alloc_context)(unsigned char *, int, int, void *,
|
74 | 70 |
int (*read_packet)(void *, uint8_t *, int), int (*write_packet)(void *, uint8_t *, int), int64_t (*seek)(void *, int64_t, int));
|
|
71 |
AVPacket * (* av_packet_alloc)(void);
|
|
72 |
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,133,100)
|
75 | 73 |
void (* av_init_packet)(AVPacket *);
|
76 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57,24,102)
|
|
74 |
#endif
|
77 | 75 |
void (* av_packet_unref)(AVPacket *);
|
78 | |
#else
|
79 | |
void (* av_free_packet)(AVPacket *);
|
80 | |
#endif
|
81 | 76 |
int (* av_read_frame)(AVFormatContext *, AVPacket *);
|
82 | 77 |
AVInputFormat * (* av_find_input_format)(const char *);
|
|
78 |
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,9,100)
|
83 | 79 |
void (* av_register_all)(void);
|
|
80 |
#endif
|
84 | 81 |
// ffmpeg symbols to be dynamically loaded from libavutil
|
85 | 82 |
unsigned (* avutil_version)(void);
|
86 | 83 |
void (* av_log_set_callback)(void (*)(void*, int, const char*, va_list));
|
|
262 | 259 |
}
|
263 | 260 |
|
264 | 261 |
static decode_state ff_decode(void) {
|
265 | |
int r, len, got_frame;
|
266 | |
AVPacket pkt_c;
|
|
262 |
int r;
|
267 | 263 |
s32_t *optr = NULL;
|
268 | 264 |
|
269 | 265 |
if (decode.new_stream) {
|
|
350 | 346 |
|
351 | 347 |
AVCODEC(ff, open2, ff->codecC, codec, NULL);
|
352 | 348 |
|
353 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)
|
354 | 349 |
ff->frame = AV(ff, frame_alloc);
|
355 | |
#else
|
356 | |
ff->frame = AVCODEC(ff, alloc_frame);
|
357 | |
#endif
|
358 | |
|
359 | |
ff->avpkt = AV(ff, malloc, sizeof(AVPacket));
|
|
350 |
ff->avpkt = AV(ff, packet_alloc);
|
360 | 351 |
if (ff->avpkt == NULL) {
|
361 | 352 |
LOG_ERROR("can't allocate avpkt");
|
362 | 353 |
return DECODE_ERROR;
|
363 | 354 |
}
|
364 | 355 |
|
365 | |
AV(ff, init_packet, ff->avpkt);
|
366 | |
ff->avpkt->data = NULL;
|
367 | |
ff->avpkt->size = 0;
|
|
356 |
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,133,100)
|
|
357 |
AV(ff, init_packet, ff->avpkt); // on older ffmpeg, this still is needed to e.g. set ->pos to -1
|
|
358 |
#endif
|
368 | 359 |
|
369 | 360 |
LOCK_O;
|
370 | 361 |
LOG_INFO("setting track_start");
|
|
375 | 366 |
decode.new_stream = false;
|
376 | 367 |
UNLOCK_O;
|
377 | 368 |
}
|
378 | |
|
379 | |
got_frame = 0;
|
380 | 369 |
|
381 | 370 |
if ((r = AV(ff, read_frame, ff->formatC, ff->avpkt)) < 0) {
|
382 | 371 |
if (r == AVERROR_EOF) {
|
|
392 | 381 |
return DECODE_RUNNING;
|
393 | 382 |
}
|
394 | 383 |
|
395 | |
// clone packet as we are adjusting it
|
396 | |
pkt_c = *ff->avpkt;
|
397 | |
|
398 | 384 |
IF_PROCESS(
|
399 | 385 |
optr = (s32_t *)process.inbuf;
|
400 | 386 |
process.in_frames = 0;
|
401 | 387 |
);
|
402 | 388 |
|
403 | |
while (pkt_c.size > 0 || got_frame) {
|
404 | |
|
405 | |
len = AVCODEC(ff, decode_audio4, ff->codecC, ff->frame, &got_frame, &pkt_c);
|
406 | |
if (len < 0) {
|
407 | |
LOG_ERROR("avcodec_decode_audio4 error: %i %s", len, av__err2str(len));
|
408 | |
break; // exit loop, free the packet, and continue decoding
|
409 | |
}
|
410 | |
|
411 | |
pkt_c.data += len;
|
412 | |
pkt_c.size -= len;
|
413 | |
|
414 | |
if (got_frame) {
|
415 | |
|
|
389 |
if ((r = AVCODEC(ff, send_packet, ff->codecC, ff->avpkt)) < 0) {
|
|
390 |
AV(ff, packet_unref, ff->avpkt);
|
|
391 |
|
|
392 |
if (r == AVERROR_EOF) {
|
|
393 |
LOG_DEBUG("av_send_packet reports eof");
|
|
394 |
return DECODE_COMPLETE;
|
|
395 |
} else {
|
|
396 |
LOG_ERROR("av_send_packet error: %i %s", r, av__err2str(r));
|
|
397 |
|
|
398 |
// EAGAIN is treated as an unrecoverable error here since all pending output frames are processed below
|
|
399 |
if (r == AVERROR(EAGAIN) || r == AVERROR(EINVAL) || r == AVERROR(ENOMEM))
|
|
400 |
return DECODE_ERROR;
|
|
401 |
|
|
402 |
// retain existing behavior (continue decoding) for legitimate decoding errors
|
|
403 |
return DECODE_RUNNING;
|
|
404 |
}
|
|
405 |
} else {
|
|
406 |
// a single input packet can lead to multiple output frames - process all of them
|
|
407 |
while ((r = AVCODEC(ff, receive_frame, ff->codecC, ff->frame)) >= 0) {
|
416 | 408 |
s16_t *iptr16 = (s16_t *)ff->frame->data[0];
|
417 | 409 |
s32_t *iptr32 = (s32_t *)ff->frame->data[0];
|
418 | 410 |
s16_t *iptr16l = (s16_t *)ff->frame->data[0];
|
|
535 | 527 |
}
|
536 | 528 |
}
|
537 | 529 |
|
538 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57,24,102)
|
539 | 530 |
AV(ff, packet_unref, ff->avpkt);
|
540 | |
#else
|
541 | |
AV(ff, free_packet, ff->avpkt);
|
542 | |
#endif
|
|
531 |
|
|
532 |
if (r == AVERROR(EAGAIN)) {
|
|
533 |
// EAGAIN is expected if there are no more pending frames
|
|
534 |
} else if (r == AVERROR_EOF) {
|
|
535 |
LOG_DEBUG("av_receive_frame reports eof");
|
|
536 |
return DECODE_COMPLETE;
|
|
537 |
} else {
|
|
538 |
LOG_ERROR("av_receive_frame error: %i %s", r, av__err2str(r));
|
|
539 |
|
|
540 |
if (r == AVERROR(EINVAL))
|
|
541 |
return DECODE_ERROR;
|
|
542 |
|
|
543 |
// retain existing behavior (continue decoding) for legitimate decoding errors
|
|
544 |
}
|
543 | 545 |
|
544 | 546 |
return DECODE_RUNNING;
|
545 | 547 |
}
|
|
564 | 566 |
}
|
565 | 567 |
|
566 | 568 |
if (ff->frame) {
|
567 | |
// ffmpeg version dependant free function
|
568 | |
#if !LINKALL
|
569 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)
|
570 | |
ff->av_frame_free ? AV(ff, frame_free, &ff->frame) : AV(ff, freep, &ff->frame);
|
571 | |
#else
|
572 | |
ff->avcodec_free_frame ? AVCODEC(ff, free_frame, &ff->frame) : AV(ff, freep, &ff->frame);
|
573 | |
#endif
|
574 | |
#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54,28,0)
|
575 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)
|
576 | 569 |
AV(ff, frame_free, &ff->frame);
|
577 | |
#else
|
578 | |
AVCODEC(ff, free_frame, &ff->frame);
|
579 | |
#endif
|
580 | |
#else
|
581 | |
AV(ff, freep, &ff->frame);
|
582 | |
#endif
|
583 | 570 |
ff->frame = NULL;
|
584 | 571 |
}
|
585 | 572 |
|
586 | 573 |
if (ff->avpkt) {
|
587 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57,24,102)
|
588 | 574 |
AV(ff, packet_unref, ff->avpkt);
|
589 | |
#else
|
590 | |
AV(ff, free_packet, ff->avpkt);
|
591 | |
#endif
|
592 | 575 |
AV(ff, freep, &ff->avpkt);
|
593 | 576 |
ff->avpkt = NULL;
|
594 | 577 |
}
|
|
660 | 643 |
ff->avcodec_version = dlsym(handle_codec, "avcodec_version");
|
661 | 644 |
ff->avcodec_find_decoder = dlsym(handle_codec, "avcodec_find_decoder");
|
662 | 645 |
ff->avcodec_open2 = dlsym(handle_codec, "avcodec_open2");
|
663 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55,28,1)
|
664 | 646 |
ff->av_frame_alloc = dlsym(handle_codec, "av_frame_alloc");
|
665 | 647 |
ff->av_frame_free = dlsym(handle_codec, "av_frame_free");
|
666 | |
#else
|
667 | |
ff->avcodec_alloc_frame = dlsym(handle_codec, "avcodec_alloc_frame");
|
668 | |
ff->avcodec_free_frame = dlsym(handle_codec, "avcodec_free_frame");
|
669 | |
#endif
|
670 | |
ff->avcodec_decode_audio4 = dlsym(handle_codec, "avcodec_decode_audio4");
|
|
648 |
ff->avcodec_send_packet = dlsym(handle_codec, "avcodec_send_packet");
|
|
649 |
ff->avcodec_receive_frame = dlsym(handle_codec, "avcodec_receive_frame");
|
671 | 650 |
ff->avcodec_alloc_context3 = dlsym(handle_format, "avcodec_alloc_context3");
|
672 | 651 |
ff->avcodec_free_context = dlsym(handle_format, "avcodec_free_context");
|
673 | 652 |
ff->avcodec_parameters_to_context = dlsym(handle_format, "avcodec_parameters_to_context");
|
|
653 |
ff->av_packet_alloc = dlsym(handle_codec, "av_packet_alloc");
|
|
654 |
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,133,100)
|
674 | 655 |
ff->av_init_packet = dlsym(handle_codec, "av_init_packet");
|
675 | |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57,24,102)
|
|
656 |
#endif
|
676 | 657 |
ff->av_packet_unref = dlsym(handle_codec, "av_packet_unref");
|
677 | |
#else
|
678 | |
ff->av_free_packet = dlsym(handle_codec, "av_free_packet");
|
679 | |
#endif
|
680 | 658 |
|
681 | 659 |
if ((err = dlerror()) != NULL) {
|
682 | 660 |
LOG_INFO("dlerror: %s", err);
|
|
693 | 671 |
ff->avio_alloc_context = dlsym(handle_format, "avio_alloc_context");
|
694 | 672 |
ff->av_read_frame = dlsym(handle_format, "av_read_frame");
|
695 | 673 |
ff->av_find_input_format= dlsym(handle_format, "av_find_input_format");
|
|
674 |
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,9,100)
|
696 | 675 |
ff->av_register_all = dlsym(handle_format, "av_register_all");
|
|
676 |
#endif
|
697 | 677 |
|
698 | 678 |
if ((err = dlerror()) != NULL) {
|
699 | 679 |
LOG_INFO("dlerror: %s", err);
|
|
760 | 740 |
|
761 | 741 |
AV(ff, log_set_callback, av_err_callback);
|
762 | 742 |
|
|
743 |
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,9,100)
|
763 | 744 |
AV(ff, register_all);
|
764 | |
|
|
745 |
#endif
|
|
746 |
|
765 | 747 |
registered = true;
|
766 | 748 |
}
|
767 | 749 |
|