make neutrino compatible with new ffmpeg

This commit is contained in:
Jacek Jendrzej
2017-03-07 15:00:17 +01:00
parent d3c3d3760c
commit bfa589eb08
6 changed files with 184 additions and 24 deletions

View File

@@ -68,12 +68,20 @@ extern "C" {
#include <libavformat/avformat.h>
}
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57, 8, 0 ))
#define av_packet_unref av_free_packet
#endif
class CStreamRec : public CRecordInstance, OpenThreads::Thread
{
private:
AVFormatContext *ifcx;
AVFormatContext *ofcx;
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,52,100 ))
AVBitStreamFilterContext *bsfc;
#else
AVBSFContext *bsfc;
#endif
bool stopped;
bool interrupt;
time_t time_started;
@@ -1912,8 +1920,13 @@ void CStreamRec::Close()
}
avformat_free_context(ofcx);
}
if (bsfc)
if (bsfc){
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,52,100 ))
av_bitstream_filter_close(bsfc);
#else
av_bsf_free(&bsfc);
#endif
}
ifcx = NULL;
ofcx = NULL;
bsfc = NULL;
@@ -1930,7 +1943,11 @@ void CStreamRec::FillMovieInfo(CZapitChannel * /*channel*/, APIDList & /*apid_li
for (unsigned i = 0; i < ofcx->nb_streams; i++) {
AVStream *st = ofcx->streams[i];
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 ))
AVCodecContext * codec = st->codec;
#else
AVCodecParameters * codec = st->codecpar;
#endif
if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
AUDIO_PIDS audio_pids;
AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
@@ -2159,12 +2176,17 @@ bool CStreamRec::Open(CZapitChannel * channel)
stream_index = -1;
int stid = 0x200;
for (unsigned i = 0; i < ifcx->nb_streams; i++) {
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 ))
AVCodecContext * iccx = ifcx->streams[i]->codec;
AVStream *ost = avformat_new_stream(ofcx, iccx->codec);
avcodec_copy_context(ost->codec, iccx);
#else
AVCodecParameters * iccx = ifcx->streams[i]->codecpar;
AVStream *ost = avformat_new_stream(ofcx, NULL);
avcodec_parameters_copy(ost->codecpar, iccx);
#endif
av_dict_copy(&ost->metadata, ifcx->streams[i]->metadata, 0);
ost->time_base = iccx->time_base;
ost->time_base = ifcx->streams[i]->time_base;
ost->id = stid++;
if (iccx->codec_type == AVMEDIA_TYPE_VIDEO) {
stream_index = i;
@@ -2174,10 +2196,19 @@ bool CStreamRec::Open(CZapitChannel * channel)
av_log_set_level(AV_LOG_VERBOSE);
av_dump_format(ofcx, 0, ofcx->filename, 1);
av_log_set_level(AV_LOG_WARNING);
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,52,100 ))
bsfc = av_bitstream_filter_init("h264_mp4toannexb");
if (!bsfc)
printf("%s: av_bitstream_filter_init h264_mp4toannexb failed!\n", __FUNCTION__);
#else
const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
if(!bsf) {
return false;
}
if ((av_bsf_alloc(bsf, &bsfc))) {
return false;
}
#endif
return true;
}
@@ -2201,16 +2232,34 @@ void CStreamRec::run()
break;
if (pkt.stream_index < 0)
continue;
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 ))
AVCodecContext *codec = ifcx->streams[pkt.stream_index]->codec;
#else
AVCodecParameters *codec = ifcx->streams[pkt.stream_index]->codecpar;
#endif
if (bsfc && codec->codec_id == AV_CODEC_ID_H264) {
AVPacket newpkt = pkt;
#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,52,100 ))
if (av_bitstream_filter_filter(bsfc, codec, NULL, &newpkt.data, &newpkt.size, pkt.data, pkt.size, pkt.flags & AV_PKT_FLAG_KEY) >= 0) {
av_packet_unref(&pkt);
newpkt.buf = av_buffer_create(newpkt.data, newpkt.size, av_buffer_default_free, NULL, 0);
pkt = newpkt;
}
#else
int ret = av_bsf_send_packet(bsfc, &pkt);
if (ret < 0){
break;
}
ret = av_bsf_receive_packet(bsfc, &newpkt);
if (ret == AVERROR(EAGAIN)){
break;
}
if(ret != AVERROR_EOF){
av_packet_unref(&pkt);
newpkt.buf = av_buffer_create(newpkt.data, newpkt.size, av_buffer_default_free, NULL, 0);
pkt = newpkt;
}
#endif
}
pkt.pts = av_rescale_q(pkt.pts, ifcx->streams[pkt.stream_index]->time_base, ofcx->streams[pkt.stream_index]->time_base);
pkt.dts = av_rescale_q(pkt.dts, ifcx->streams[pkt.stream_index]->time_base, ofcx->streams[pkt.stream_index]->time_base);