plugins/ffmpeg_decoder: migrate to FFmpeg 4.x API

Signed-off-by: Zixing Liu <liushuyu@aosc.io>

plugins/ffmpeg_decoder: Add detection whether AVFrame structure has
pkt_size and channels members.

Signed-off-by: Slávek Banko <slavek.banko@axis.cz>
pull/7/head
liushuyu 3 years ago committed by liushuyu
parent 0b60970d5a
commit 6dacea6e81
No known key found for this signature in database
GPG Key ID: 23D1CE4534419437

@ -27,6 +27,7 @@ include( CheckFunctionExists )
include( CheckSymbolExists ) include( CheckSymbolExists )
include( CheckIncludeFile ) include( CheckIncludeFile )
include( CheckLibraryExists ) include( CheckLibraryExists )
include( CheckStructHasMember )
##### include our cmake modules ##### include our cmake modules

@ -169,6 +169,23 @@ if( WITH_FFMPEG_DECODER )
tde_message_fatal( "libavcodec >= 50 are required, but not found on your system" ) tde_message_fatal( "libavcodec >= 50 are required, but not found on your system" )
endif( NOT AVCODEC_FOUND ) endif( NOT AVCODEC_FOUND )
pkg_search_module( AVUTIL libavutil>=50 )
if( NOT AVUTIL_FOUND )
tde_message_fatal( "libavutil >= 50 are required, but not found on your system" )
endif( NOT AVUTIL_FOUND )
message( STATUS "Looking for ffmpeg frame.h" )
find_file( AVFRAME_H NAMES frame.h avcodec.h
PATHS ${AVUTIL_INCLUDE_DIRS} ${AVCODEC_INCLUDE_DIRS}
PATH_SUFFIXES libavutil libavcodec
)
if( NOT AVFRAME_H )
tde_message_fatal( "FFMPEG header frame.h cannot be found!" )
endif()
message( STATUS "Looking for ffmpeg frame.h - found ${AVFRAME_H}" )
check_struct_has_member( AVFrame pkt_size ${AVFRAME_H} FFMPEG_AVFRAME_HAVE_PKT_SIZE )
check_struct_has_member( AVFrame channels ${AVFRAME_H} FFMPEG_AVFRAME_HAVE_CHANNELS )
set( HAVE_FFMPEG 1 ) set( HAVE_FFMPEG 1 )
endif( WITH_FFMPEG_DECODER ) endif( WITH_FFMPEG_DECODER )

@ -30,4 +30,3 @@ tde_add_library(
LINK akode-shared ${AVFORMAT_LIBRARIES} ${AVCODEC_LIBRARIES} LINK akode-shared ${AVFORMAT_LIBRARIES} ${AVCODEC_LIBRARIES}
DESTINATION ${LIB_INSTALL_DIR} DESTINATION ${LIB_INSTALL_DIR}
) )

@ -26,13 +26,21 @@
#include "decoder.h" #include "decoder.h"
#include <assert.h> #include <assert.h>
#include <ffmpeg/avcodec.h> extern "C" {
#include <ffmpeg/avformat.h> #include <libavcodec/avcodec.h>
#include <ffmpeg/avio.h> #include <libavformat/avformat.h>
#include <libavformat/avio.h>
}
#include "ffmpeg_decoder.h" #include "ffmpeg_decoder.h"
#include <iostream> #include <iostream>
#if LIBAVCODEC_VERSION_MAJOR < 58
#define CODECPAR codec
#else
#define CODECPAR codecpar
#endif
// FFMPEG callbacks // FFMPEG callbacks
extern "C" { extern "C" {
static int akode_read(void* opaque, unsigned char *buf, int size) static int akode_read(void* opaque, unsigned char *buf, int size)
@ -45,7 +53,7 @@ extern "C" {
aKode::File *file = (aKode::File*)opaque; aKode::File *file = (aKode::File*)opaque;
return file->write((char*)buf, size); return file->write((char*)buf, size);
} }
static offset_t akode_seek(void* opaque, offset_t pos, int whence) static off_t akode_seek(void* opaque, off_t pos, int whence)
{ {
aKode::File *file = (aKode::File*)opaque; aKode::File *file = (aKode::File*)opaque;
return file->seek(pos, whence); return file->seek(pos, whence);
@ -76,7 +84,7 @@ struct FFMPEGDecoder::private_data
AVFormatContext* ic; AVFormatContext* ic;
AVCodec* codec; AVCodec* codec;
AVInputFormat *fmt; AVInputFormat *fmt;
ByteIOContext stream; AVIOContext* stream;
int audioStream; int audioStream;
int videoStream; int videoStream;
@ -94,14 +102,13 @@ struct FFMPEGDecoder::private_data
bool initialized; bool initialized;
int retries; int retries;
unsigned char file_buffer[FILE_BUFFER_SIZE]; unsigned char *file_buffer;
char buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE]; unsigned char **buffer;
int buffer_size; int buffer_size;
}; };
FFMPEGDecoder::FFMPEGDecoder(File *src) { FFMPEGDecoder::FFMPEGDecoder(File *src) {
d = new private_data; d = new private_data;
av_register_all();
d->src = src; d->src = src;
} }
@ -121,10 +128,12 @@ static bool setAudioConfiguration(AudioConfiguration *config, AVCodecContext *co
config->channel_config = MonoStereo; config->channel_config = MonoStereo;
// avcodec.h says sample_fmt is not used. I guess it means it is always S16 // avcodec.h says sample_fmt is not used. I guess it means it is always S16
switch(codec_context->sample_fmt) { switch(codec_context->sample_fmt) {
case SAMPLE_FMT_U8: case AV_SAMPLE_FMT_U8:
case AV_SAMPLE_FMT_U8P:
config->sample_width = 8; // beware unsigned! config->sample_width = 8; // beware unsigned!
break; break;
case SAMPLE_FMT_S16: case AV_SAMPLE_FMT_S16:
case AV_SAMPLE_FMT_S16P:
config->sample_width = 16; config->sample_width = 16;
break; break;
/* disabled because I don't know byte ordering /* disabled because I don't know byte ordering
@ -132,10 +141,12 @@ static bool setAudioConfiguration(AudioConfiguration *config, AVCodecContext *co
config->sample_width = 24; config->sample_width = 24;
break; break;
*/ */
case SAMPLE_FMT_S32: case AV_SAMPLE_FMT_S32:
case AV_SAMPLE_FMT_S32P:
config->sample_width = 32; config->sample_width = 32;
break; break;
case SAMPLE_FMT_FLT: case AV_SAMPLE_FMT_FLT:
case AV_SAMPLE_FMT_FLTP:
config->sample_width = -32; config->sample_width = -32;
break; break;
default: default:
@ -152,58 +163,35 @@ bool FFMPEGDecoder::openFile() {
// The following duplicates what av_open_input_file would normally do // The following duplicates what av_open_input_file would normally do
// url_fdopen // url_fdopen
init_put_byte(&d->stream, d->file_buffer, FILE_BUFFER_SIZE, 0, d->src, akode_read, akode_write, akode_seek); d->file_buffer = (unsigned char*)av_malloc(FILE_BUFFER_SIZE);
d->stream.is_streamed = !d->src->seekable(); d->stream = avio_alloc_context(d->file_buffer, FILE_BUFFER_SIZE, 0, d->src, akode_read, akode_write, akode_seek);
d->stream.max_packet_size = FILE_BUFFER_SIZE; if (!d->stream) {
return false;
{
// 2048 is PROBE_BUF_SIZE from libavformat/utils.c
AVProbeData pd;
uint8_t buf[2048];
pd.filename = d->src->filename;
pd.buf = buf;
pd.buf_size = 0;
pd.buf_size = get_buffer(&d->stream, buf, 2048);
d->fmt = av_probe_input_format(&pd, 1);
// Seek back to 0
// copied from url_fseek
long offset1 = 0 - (d->stream.pos - (d->stream.buf_end - d->stream.buffer));
if (offset1 >= 0 && offset1 <= (d->stream.buf_end - d->stream.buffer)) {
/* can do the seek inside the buffer */
d->stream.buf_ptr = d->stream.buffer + offset1;
} else {
if (!d->src->seek(0)) {
d->src->close();
return false;
} else {
d->stream.pos = 0;
d->stream.buf_ptr = d->file_buffer;
d->stream.buf_end = d->file_buffer;
}
}
} }
if (!d->fmt) { d->stream->seekable = d->src->seekable();
std::cerr << "akode: FFMPEG: Format not found\n"; d->stream->max_packet_size = FILE_BUFFER_SIZE;
closeFile(); d->ic = avformat_alloc_context();
if (!d->ic) {
return false; return false;
} }
d->ic->pb = d->stream;
if (av_open_input_stream(&d->ic, &d->stream, d->src->filename, d->fmt, 0) != 0) if (avformat_open_input(&d->ic, d->src->filename, NULL, NULL) != 0)
{ {
closeFile(); closeFile();
return false; return false;
} }
av_find_stream_info( d->ic ); avformat_find_stream_info( d->ic, NULL );
// Find the first a/v streams // Find the first a/v streams
d->audioStream = -1; d->audioStream = -1;
d->videoStream = -1; d->videoStream = -1;
for (int i = 0; i < d->ic->nb_streams; i++) { for (int i = 0; i < d->ic->nb_streams; i++) {
if (d->ic->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) if (d->ic->streams[i]->CODECPAR->codec_type == AVMEDIA_TYPE_AUDIO)
d->audioStream = i; d->audioStream = i;
else else
if (d->ic->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) if (d->ic->streams[i]->CODECPAR->codec_type == AVMEDIA_TYPE_VIDEO)
d->videoStream = i; d->videoStream = i;
} }
if (d->audioStream == -1) if (d->audioStream == -1)
@ -214,6 +202,8 @@ bool FFMPEGDecoder::openFile() {
return false; return false;
} }
av_dump_format(d->ic, d->audioStream, d->src->filename, 0);
// Set config // Set config
if (!setAudioConfiguration(&d->config, d->ic->streams[d->audioStream]->codec)) if (!setAudioConfiguration(&d->config, d->ic->streams[d->audioStream]->codec))
{ {
@ -221,13 +211,13 @@ bool FFMPEGDecoder::openFile() {
return false; return false;
} }
d->codec = avcodec_find_decoder(d->ic->streams[d->audioStream]->codec->codec_id); d->codec = avcodec_find_decoder(d->ic->streams[d->audioStream]->CODECPAR->codec_id);
if (!d->codec) { if (!d->codec) {
std::cerr << "akode: FFMPEG: Codec not found\n"; std::cerr << "akode: FFMPEG: Codec not found\n";
closeFile(); closeFile();
return false; return false;
} }
avcodec_open( d->ic->streams[d->audioStream]->codec, d->codec ); avcodec_open2( d->ic->streams[d->audioStream]->codec, d->codec, NULL );
double ffpos = (double)d->ic->streams[d->audioStream]->start_time / (double)AV_TIME_BASE; double ffpos = (double)d->ic->streams[d->audioStream]->start_time / (double)AV_TIME_BASE;
d->position = (long)(ffpos * d->config.sample_rate); d->position = (long)(ffpos * d->config.sample_rate);
@ -236,8 +226,17 @@ bool FFMPEGDecoder::openFile() {
} }
void FFMPEGDecoder::closeFile() { void FFMPEGDecoder::closeFile() {
if ( d->stream ) {
if (d->stream->buffer)
av_free(d->stream->buffer);
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 80, 100)
av_free(d->stream);
#else
avio_context_free(&d->stream);
#endif
}
if( d->packetSize > 0 ) { if( d->packetSize > 0 ) {
av_free_packet( &d->packet ); av_packet_unref( &d->packet );
d->packetSize = 0; d->packetSize = 0;
} }
@ -248,7 +247,7 @@ void FFMPEGDecoder::closeFile() {
if( d->ic ) { if( d->ic ) {
// make sure av_close_input_file doesn't actually close the file // make sure av_close_input_file doesn't actually close the file
d->ic->iformat->flags = d->ic->iformat->flags | AVFMT_NOFILE; d->ic->iformat->flags = d->ic->iformat->flags | AVFMT_NOFILE;
av_close_input_file( d->ic ); avformat_close_input( &d->ic );
d->ic = 0; d->ic = 0;
} }
if (d->src) { if (d->src) {
@ -260,7 +259,7 @@ bool FFMPEGDecoder::readPacket() {
do { do {
av_init_packet(&d->packet); av_init_packet(&d->packet);
if ( av_read_frame(d->ic, &d->packet) < 0 ) { if ( av_read_frame(d->ic, &d->packet) < 0 ) {
av_free_packet( &d->packet ); av_packet_unref( &d->packet );
d->packetSize = 0; d->packetSize = 0;
d->packetData = 0; d->packetData = 0;
return false; return false;
@ -270,7 +269,7 @@ bool FFMPEGDecoder::readPacket() {
d->packetData = d->packet.data; d->packetData = d->packet.data;
return true; return true;
} }
av_free_packet(&d->packet); av_packet_unref(&d->packet);
} while (true); } while (true);
return false; return false;
@ -286,11 +285,11 @@ static long demux(FFMPEGDecoder::private_data* d, AudioFrame* frame) {
if (frame->sample_width == 8) offset = -128; // convert unsigned to signed if (frame->sample_width == 8) offset = -128; // convert unsigned to signed
// Demux into frame // Demux into frame
T* buffer = (T*)d->buffer; T** buffer = (T**)d->buffer;
T** data = (T**)frame->data; T** data = (T**)frame->data;
for(int i=0; i<length; i++) for(int i=0; i<length; i++)
for(int j=0; j<channels; j++) for(int j=0; j<channels; j++)
data[j][i] = buffer[i*channels+j] + offset; data[j][i] = buffer[j][i] + offset;
return length; return length;
} }
@ -314,9 +313,25 @@ bool FFMPEGDecoder::readFrame(AudioFrame* frame)
assert(d->packet.stream_index == d->audioStream); assert(d->packet.stream_index == d->audioStream);
retry: retry:
int len = avcodec_decode_audio( d->ic->streams[d->audioStream]->codec, AVFrame *decodeFrame = av_frame_alloc();
(short*)d->buffer, &d->buffer_size, if (!decodeFrame) {
d->packetData, d->packetSize ); return false;
}
int decoded;
int len = avcodec_decode_audio4( d->ic->streams[d->audioStream]->codec,
decodeFrame, &decoded,
&d->packet );
#if !defined(FFMPEG_AVFRAME_HAVE_PKT_SIZE)
d->packetSize = d->packet.size;
#else
d->packetSize = decodeFrame->pkt_size;
#endif
d->buffer = decodeFrame->data;
#if !defined(FFMPEG_AVFRAME_HAVE_CHANNELS)
d->buffer_size = decodeFrame->nb_samples * d->ic->streams[d->audioStream]->codec->channels * av_get_bytes_per_sample(d->ic->streams[d->audioStream]->codec->sample_fmt);
#else
d->buffer_size = decodeFrame->nb_samples * decodeFrame->channels * av_get_bytes_per_sample(d->ic->streams[d->audioStream]->codec->sample_fmt);
#endif
if (len <= 0) { if (len <= 0) {
d->retries++; d->retries++;
@ -348,11 +363,12 @@ retry:
default: default:
assert(false); assert(false);
} }
av_frame_free(&decodeFrame);
if (length == 0) return readFrame(frame); if (length == 0) return readFrame(frame);
std::cout << "akode: FFMPEG: Frame length: " << length << "\n"; // std::cout << "akode: FFMPEG: Frame length: " << length << "\n";
if( d->packetSize <= 0 ) if( d->packetSize <= 0 )
av_free_packet( &d->packet ); av_packet_unref( &d->packet );
frame->pos = (d->position*1000)/d->config.sample_rate; frame->pos = (d->position*1000)/d->config.sample_rate;
d->position += length; d->position += length;

@ -39,6 +39,10 @@
and RealAudio decoding) */ and RealAudio decoding) */
#cmakedefine HAVE_FFMPEG #cmakedefine HAVE_FFMPEG
/* Define if struct AVFrame have appropriate members */
#cmakedefine FFMPEG_AVFRAME_HAVE_PKT_SIZE 1
#cmakedefine FFMPEG_AVFRAME_HAVE_CHANNELS 1
/* Define if you have libFLAC 1.1.3 or newer */ /* Define if you have libFLAC 1.1.3 or newer */
#cmakedefine HAVE_LIBFLAC113 #cmakedefine HAVE_LIBFLAC113

Loading…
Cancel
Save