From bfa589eb08a087ebfdff55a6b0676473f86db3fe Mon Sep 17 00:00:00 2001 From: Jacek Jendrzej Date: Tue, 7 Mar 2017 15:00:17 +0100 Subject: [PATCH] make neutrino compatible with new ffmpeg --- lib/libdvbsub/dvbsubtitle.cpp | 15 +++++++- src/driver/audiodec/ffmpegdec.cpp | 62 +++++++++++++++++++++++++++---- src/driver/record.cpp | 61 +++++++++++++++++++++++++++--- src/driver/streamts.cpp | 54 ++++++++++++++++++++++++--- src/driver/streamts.h | 4 ++ src/gui/movieplayer.cpp | 12 +++++- 6 files changed, 184 insertions(+), 24 deletions(-) diff --git a/lib/libdvbsub/dvbsubtitle.cpp b/lib/libdvbsub/dvbsubtitle.cpp index 0be467e10..e2c41188b 100644 --- a/lib/libdvbsub/dvbsubtitle.cpp +++ b/lib/libdvbsub/dvbsubtitle.cpp @@ -68,8 +68,13 @@ cDvbSubtitleBitmaps::~cDvbSubtitleBitmaps() if(sub.rects) { for (i = 0; i < Count(); i++) { +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 5, 0) av_freep(&sub.rects[i]->pict.data[0]); av_freep(&sub.rects[i]->pict.data[1]); +#else + av_freep(&sub.rects[i]->data[0]); + av_freep(&sub.rects[i]->data[1]); +#endif av_freep(&sub.rects[i]); } @@ -122,7 +127,11 @@ void cDvbSubtitleBitmaps::Draw(int &min_x, int &min_y, int &max_x, int &max_y) int xf = int(xc * (double) 720); for (i = 0; i < Count(); i++) { +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 5, 0) uint32_t * colors = (uint32_t *) sub.rects[i]->pict.data[1]; +#else + uint32_t * colors = (uint32_t *) sub.rects[i]->data[1]; +#endif int width = sub.rects[i]->w; int height = sub.rects[i]->h; int xoff, yoff; @@ -142,9 +151,11 @@ void cDvbSubtitleBitmaps::Draw(int &min_x, int &min_y, int &max_x, int &max_y) dbgconverter("cDvbSubtitleBitmaps::Draw: #%d at %d,%d size %dx%d colors %d (x=%d y=%d w=%d h=%d) \n", i+1, sub.rects[i]->x, sub.rects[i]->y, sub.rects[i]->w, sub.rects[i]->h, sub.rects[i]->nb_colors, xoff, yoff, nw, nh); - +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 5, 0) fb_pixel_t * newdata = simple_resize32 (sub.rects[i]->pict.data[0], colors, sub.rects[i]->nb_colors, width, height, nw, nh); - +#else + fb_pixel_t * newdata = simple_resize32 (sub.rects[i]->data[0], colors, sub.rects[i]->nb_colors, width, height, nw, nh); +#endif fb_pixel_t * ptr = newdata; for (int y2 = 0; y2 < nh; y2++) { int y = (yoff + y2) * stride; diff --git a/src/driver/audiodec/ffmpegdec.cpp b/src/driver/audiodec/ffmpegdec.cpp index c97497175..695f67d89 100644 --- a/src/driver/audiodec/ffmpegdec.cpp +++ b/src/driver/audiodec/ffmpegdec.cpp @@ -47,6 +47,11 @@ extern "C" { #define av_frame_unref avcodec_get_frame_defaults #define av_frame_free avcodec_free_frame #endif + +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57, 8, 0 )) +#define av_packet_unref av_free_packet +#endif + #include #include @@ -218,9 +223,16 @@ CBaseDec::RetCode CFfmpegDec::Decoder(FILE *_in, int /*OutputFd*/, State* state, Status=DATA_ERR; return Status; } - +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 )) AVCodecContext *c = avc->streams[best_stream]->codec; - +#else + AVCodecContext *c = avcodec_alloc_context3(codec); + if(avcodec_parameters_to_context(c,avc->streams[best_stream]->codecpar) < 0){ + DeInit(); + Status=DATA_ERR; + return Status; + } +#endif mutex.lock(); int r = avcodec_open2(c, codec, NULL); mutex.unlock(); @@ -326,9 +338,10 @@ CBaseDec::RetCode CFfmpegDec::Decoder(FILE *_in, int /*OutputFd*/, State* state, Status=DATA_ERR; break; } - } else + } else{ av_frame_unref(frame); - + } +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,37,100) int len = avcodec_decode_audio4(c, frame, &got_frame, &packet); if (len < 0) { // skip frame @@ -340,6 +353,27 @@ CBaseDec::RetCode CFfmpegDec::Decoder(FILE *_in, int /*OutputFd*/, State* state, mutex.unlock(); continue; } + packet.size -= len; + packet.data += len; +#else + int ret = avcodec_send_packet(c, &packet); + if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF){ + break; + } + if (ret >= 0){ + packet.size = 0; + } + ret = avcodec_receive_frame(c, frame); + if (ret < 0){ + if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF){ + break; + } + else{ + continue; + } + } + got_frame = 1; +#endif if (got_frame && *state!=PAUSE) { int out_samples; outsamples = av_rescale_rnd(swr_get_delay(swr, c->sample_rate) + frame->nb_samples, @@ -368,8 +402,6 @@ CBaseDec::RetCode CFfmpegDec::Decoder(FILE *_in, int /*OutputFd*/, State* state, if (!start_pts) start_pts = pts; } - packet.size -= len; - packet.data += len; } if (time_played && avc->streams[best_stream]->time_base.den) *time_played = (pts - start_pts) * avc->streams[best_stream]->time_base.num / avc->streams[best_stream]->time_base.den; @@ -428,7 +460,11 @@ bool CFfmpegDec::SetMetaData(FILE *_in, CAudioMetaData* m, bool save_cover) if (!is_stream) { GetMeta(avc->metadata); for(unsigned int i = 0; i < avc->nb_streams; i++) { +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 )) if (avc->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) +#else + if (avc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) +#endif GetMeta(avc->streams[i]->metadata); } } @@ -445,12 +481,17 @@ bool CFfmpegDec::SetMetaData(FILE *_in, CAudioMetaData* m, bool save_cover) DeInit(); return false; } - +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 )) if (!codec) codec = avcodec_find_decoder(avc->streams[best_stream]->codec->codec_id); samplerate = avc->streams[best_stream]->codec->sample_rate; mChannels = av_get_channel_layout_nb_channels(avc->streams[best_stream]->codec->channel_layout); - +#else + if (!codec) + codec = avcodec_find_decoder(avc->streams[best_stream]->codecpar->codec_id); + samplerate = avc->streams[best_stream]->codecpar->sample_rate; + mChannels = av_get_channel_layout_nb_channels(avc->streams[best_stream]->codecpar->channel_layout); +#endif std::stringstream ss; if (codec && codec->long_name != NULL) @@ -470,8 +511,13 @@ bool CFfmpegDec::SetMetaData(FILE *_in, CAudioMetaData* m, bool save_cover) printf("CFfmpegDec: format %s (%s) duration %ld\n", avc->iformat->name, type_info.c_str(), total_time); for(unsigned int i = 0; i < avc->nb_streams; i++) { +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,5,0 )) if (avc->streams[i]->codec->bit_rate > 0) bitrate += avc->streams[i]->codec->bit_rate; +#else + if (avc->streams[i]->codecpar->bit_rate > 0) + bitrate += avc->streams[i]->codecpar->bit_rate; +#endif if (save_cover && (avc->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC)) { mkdir(COVERDIR, 0755); std::string cover(COVERDIR); diff --git a/src/driver/record.cpp b/src/driver/record.cpp index 517ecff64..0c234cbe6 100644 --- a/src/driver/record.cpp +++ b/src/driver/record.cpp @@ -68,12 +68,20 @@ extern "C" { #include } +#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); diff --git a/src/driver/streamts.cpp b/src/driver/streamts.cpp index f0144806c..d2f4a2d3c 100644 --- a/src/driver/streamts.cpp +++ b/src/driver/streamts.cpp @@ -59,6 +59,10 @@ #include #include +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57, 8, 0 )) +#define av_packet_unref av_free_packet +#endif + /* experimental mode: * stream not possible, if record running * pids in url ignored, and added from channel, with fake PAT/PMT @@ -753,8 +757,13 @@ void CStreamStream::Close() if (avio_ctx) av_free(avio_ctx); - 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; @@ -837,21 +846,35 @@ bool CStreamStream::Open() av_dict_copy(&ofcx->metadata, ifcx->metadata, 0); 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++; } 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; } @@ -896,15 +919,34 @@ void CStreamStream::run() 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); diff --git a/src/driver/streamts.h b/src/driver/streamts.h index da2708e51..7ec1bd321 100644 --- a/src/driver/streamts.h +++ b/src/driver/streamts.h @@ -73,7 +73,11 @@ class CStreamStream : public CStreamInstance private: AVFormatContext *ifcx; AVFormatContext *ofcx; +#if (LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 57,52,100 )) AVBitStreamFilterContext *bsfc; +#else + AVBSFContext *bsfc; +#endif AVIOContext *avio_ctx; bool stopped; diff --git a/src/gui/movieplayer.cpp b/src/gui/movieplayer.cpp index eabe0e255..46e222231 100644 --- a/src/gui/movieplayer.cpp +++ b/src/gui/movieplayer.cpp @@ -2468,8 +2468,11 @@ void CMoviePlayerGui::showSubtitle(neutrino_msg_data_t data) clearSubtitle(); for (unsigned i = 0; i < sub->num_rects; i++) { +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 5, 0) uint32_t * colors = (uint32_t *) sub->rects[i]->pict.data[1]; - +#else + uint32_t * colors = (uint32_t *) sub->rects[i]->data[1]; +#endif int xoff = (double) sub->rects[i]->x * xc; int yoff = (double) sub->rects[i]->y * yc; int nw = frameBuffer->getWidth4FB_HW_ACC(xoff, (double) sub->rects[i]->w * xc); @@ -2478,9 +2481,14 @@ void CMoviePlayerGui::showSubtitle(neutrino_msg_data_t data) printf("Draw: #%d at %d,%d size %dx%d colors %d (x=%d y=%d w=%d h=%d) \n", i+1, sub->rects[i]->x, sub->rects[i]->y, sub->rects[i]->w, sub->rects[i]->h, sub->rects[i]->nb_colors, xoff, yoff, nw, nh); - +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 5, 0) fb_pixel_t * newdata = simple_resize32 (sub->rects[i]->pict.data[0], colors, sub->rects[i]->nb_colors, sub->rects[i]->w, sub->rects[i]->h, nw, nh); +#else + fb_pixel_t * newdata = simple_resize32 (sub->rects[i]->data[0], colors, + sub->rects[i]->nb_colors, sub->rects[i]->w, sub->rects[i]->h, nw, nh); +#endif + frameBuffer->blit2FB(newdata, nw, nh, xoff, yoff); free(newdata);