X-Git-Url: https://git.cinelerra-gg.org/git/?a=blobdiff_plain;f=cinelerra-5.1%2Fcinelerra%2Fffmpeg.C;h=e95cc87e6da6f8c835a199db8ea445d9696a8190;hb=def7a2e3e4f053af598be006210a4cd29d984305;hp=90e4a8510421a5c4bdf60d44422dc5245d0e29f9;hpb=071118a29f7a9b123cd313fca29bdc65368613ca;p=goodguy%2Fcinelerra.git diff --git a/cinelerra-5.1/cinelerra/ffmpeg.C b/cinelerra-5.1/cinelerra/ffmpeg.C index 90e4a851..e95cc87e 100644 --- a/cinelerra-5.1/cinelerra/ffmpeg.C +++ b/cinelerra-5.1/cinelerra/ffmpeg.C @@ -1,3 +1,22 @@ +/* + * CINELERRA + * Copyright (C) 2012-2014 Paolo Rampino + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ #include #include @@ -29,7 +48,9 @@ #include "ffmpeg.h" #include "indexfile.h" #include "interlacemodes.h" +#ifdef HAVE_DV #include "libdv.h" +#endif #include "libmjpeg.h" #include "mainerror.h" #include "mwindow.h" @@ -378,8 +399,11 @@ AVHWDeviceType FFStream::decode_hw_activate() { return AV_HWDEVICE_TYPE_NONE; } - +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type) +#else int FFStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type) +#endif { return 0; } @@ -404,7 +428,11 @@ int FFStream::decode_activate() } while( ret >= 0 && st != 0 && !reading ) { AVCodecID codec_id = st->codecpar->codec_id; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVCodec *decoder = 0; +#else AVCodec *decoder = 0; +#endif if( is_video() ) { if( ffmpeg->opt_video_decoder ) decoder = avcodec_find_decoder_by_name(ffmpeg->opt_video_decoder); @@ -576,6 +604,10 @@ int FFStream::write_packet(FFPacket &pkt) ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt); } else { + bsfc->time_base_in = st->time_base; + avcodec_parameters_copy(bsfc->par_in, st->codecpar); + av_bsf_init(bsfc); + ret = av_bsf_send_packet(bsfc, pkt); while( ret >= 0 ) { FFPacket bs; @@ -584,6 +616,9 @@ int FFStream::write_packet(FFPacket &pkt) if( ret == AVERROR_EOF ) return -1; break; } + //printf(" filter name %s \n", bsfc->filter[0].name); + //avcodec_parameters_copy(ffmpeg->fmt_ctx->streams[0]->codecpar, bsfc->par_out); + //avcodec_parameters_copy(st->codecpar, bsfc->par_out); av_packet_rescale_ts(bs, avctx->time_base, st->time_base); bs->stream_index = st->index; ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, bs); @@ -603,6 +638,7 @@ int FFStream::encode_frame(AVFrame *frame) ret = avcodec_send_frame(avctx, frame); if( !ret && frame ) return pkts; if( ret < 0 && ret != AVERROR(EAGAIN) ) break; + if ( ret == AVERROR(EAGAIN) && !frame ) continue; FFPacket opkt; ret = avcodec_receive_packet(avctx, opkt); if( !frame && ret == AVERROR_EOF ) return pkts; @@ -719,10 +755,19 @@ int FFStream::seek(int64_t no, double rate) double secs = pos < 0 ? 0. : pos / rate; AVRational time_base = st->time_base; int64_t tstmp = time_base.num > 0 ? secs * time_base.den/time_base.num : 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + int nb_index_entries = avformat_index_get_entries_count(st); +#endif if( !tstmp ) { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + if( nb_index_entries > 0 ) tstmp = (avformat_index_get_entry(st, 0))->timestamp; +#else if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp; +#endif else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time; +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts; +#endif else tstmp = INT64_MIN+1; } else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge; @@ -1090,8 +1135,11 @@ AVHWDeviceType FFVideoStream::decode_hw_activate() } return type; } - +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFVideoStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type) +#else int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type) +#endif { int ret = 0; hw_pix_fmt = AV_PIX_FMT_NONE; @@ -1112,7 +1160,14 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type) if( hw_pix_fmt >= 0 ) { hw_pixfmt = hw_pix_fmt; avctx->get_format = get_hw_format; + const char *drm_node = getenv("CIN_DRM_DEC"); + if(drm_node && type==AV_HWDEVICE_TYPE_VAAPI) { + ret = av_hwdevice_ctx_create(&hw_device_ctx, type, drm_node, 0, 0); + } + else { ret = av_hwdevice_ctx_create(&hw_device_ctx, type, 0, 0, 0); + } + if( ret >= 0 ) { avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx); ret = 1; @@ -1128,6 +1183,7 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type) AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev) { + const char *drm_node_enc = getenv("CIN_DRM_ENC"); AVBufferRef *hw_device_ctx = 0; AVBufferRef *hw_frames_ref = 0; AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE; @@ -1139,7 +1195,12 @@ AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev) } } if( type != AV_HWDEVICE_TYPE_NONE ) { - int ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, 0, 0, 0); + int ret = 0; + if (drm_node_enc) { + ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, drm_node_enc, 0, 0); + } else { + ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, 0, 0, 0); + } if( ret < 0 ) { ff_err(ret, "Failed to create a HW device.\n"); type = AV_HWDEVICE_TYPE_NONE; @@ -1243,6 +1304,7 @@ int FFVideoStream::probe(int64_t pos) ret = -1; ret = ret > 0 ? 1 : ret < 0 ? -1 : 0; + av_frame_free(&frame); return ret; } @@ -1589,9 +1651,11 @@ int FFVideoConvert::convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame * } int color_space = SWS_CS_ITU601; switch( preferences->yuv_color_space ) { - case BC_COLORS_BT601: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_PAL: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break; case BC_COLORS_BT709: color_space = SWS_CS_ITU709; break; - case BC_COLORS_BT2020: color_space = SWS_CS_BT2020; break; + case BC_COLORS_BT2020_NCL: + case BC_COLORS_BT2020_CL: color_space = SWS_CS_BT2020; break; } const int *color_table = sws_getCoefficients(color_space); @@ -1718,9 +1782,11 @@ int FFVideoConvert::convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame * } int color_space = SWS_CS_ITU601; switch( preferences->yuv_color_space ) { - case BC_COLORS_BT601: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_PAL: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break; case BC_COLORS_BT709: color_space = SWS_CS_ITU709; break; - case BC_COLORS_BT2020: color_space = SWS_CS_BT2020; break; + case BC_COLORS_BT2020_NCL: + case BC_COLORS_BT2020_CL: color_space = SWS_CS_BT2020; break; } const int *color_table = sws_getCoefficients(color_space); @@ -1842,7 +1908,11 @@ FFMPEG::~FFMPEG() delete [] opt_hw_dev; } +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFMPEG::check_sample_rate(const AVCodec *codec, int sample_rate) +#else int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate) +#endif { const int *p = codec->supported_samplerates; if( !p ) return sample_rate; @@ -2092,7 +2162,11 @@ void FFMPEG::scan_audio_options(Asset *asset, EDL *edl) cin_fmt = (int)av_get_sample_fmt(cin_sample_fmt); if( cin_fmt < 0 ) { char audio_codec[BCSTRLEN]; audio_codec[0] = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100) + const AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ? +#else AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ? +#endif avcodec_find_encoder_by_name(audio_codec) : 0; if( av_codec && av_codec->sample_fmts ) cin_fmt = find_best_sample_fmt_of_list(av_codec->sample_fmts, AV_SAMPLE_FMT_FLT); @@ -2128,7 +2202,11 @@ void FFMPEG::scan_video_options(Asset *asset, EDL *edl) cin_fmt = (int)av_get_pix_fmt(cin_pix_fmt); if( cin_fmt < 0 ) { char video_codec[BCSTRLEN]; video_codec[0] = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100) + const AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ? +#else AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ? +#endif avcodec_find_encoder_by_name(video_codec) : 0; if( av_codec && av_codec->pix_fmts ) { if( 0 && edl ) { // frequently picks a bad answer @@ -2322,10 +2400,15 @@ int FFCodecRemaps::add(const char *val) return 0; } - +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFCodecRemaps::update(AVCodecID &codec_id, const AVCodec *&decoder) +{ + const AVCodec *codec = avcodec_find_decoder(codec_id); +#else int FFCodecRemaps::update(AVCodecID &codec_id, AVCodec *&decoder) { AVCodec *codec = avcodec_find_decoder(codec_id); +#endif if( !codec ) return -1; const char *name = codec->name; FFCodecRemaps &map = *this; @@ -2545,7 +2628,11 @@ int FFMPEG::init_decoder(const char *filename) char *sp = strrchr(bp, '.'); if( !sp ) sp = bp + strlen(bp); FILE *fp = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100) + const AVInputFormat *ifmt = 0; +#else AVInputFormat *ifmt = 0; +#endif if( sp ) { strcpy(sp, ".opts"); fp = fopen(file_opts, "r"); @@ -2638,18 +2725,22 @@ int FFMPEG::open_decoder() } switch( avpar->color_space ) { case AVCOL_SPC_BT470BG: + vid->color_space = BC_COLORS_BT601_PAL; + break; case AVCOL_SPC_SMPTE170M: - vid->color_space = BC_COLORS_BT601; + vid->color_space = BC_COLORS_BT601_NTSC; break; case AVCOL_SPC_BT709: vid->color_space = BC_COLORS_BT709; break; case AVCOL_SPC_BT2020_NCL: + vid->color_space = BC_COLORS_BT2020_NCL; + break; case AVCOL_SPC_BT2020_CL: - vid->color_space = BC_COLORS_BT2020; + vid->color_space = BC_COLORS_BT2020_CL; break; default: - vid->color_space = !file_base ? BC_COLORS_BT601 : + vid->color_space = !file_base ? BC_COLORS_BT601_NTSC : file_base->file->preferences->yuv_color_space; break; } @@ -2762,7 +2853,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec) AVCodecContext *ctx = 0; const AVCodecDescriptor *codec_desc = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVCodec *codec = avcodec_find_encoder_by_name(codec_name); +#else AVCodec *codec = avcodec_find_encoder_by_name(codec_name); +#endif if( !codec ) { eprintf(_("cant find codec %s:%s\n"), codec_name, filename); ret = 1; @@ -2883,6 +2978,14 @@ int FFMPEG::open_encoder(const char *type, const char *spec) vid->width = asset->width; vid->height = asset->height; vid->frame_rate = asset->frame_rate; +#if 0 + char tc_str[20] = "00:00:00:00"; + double tc_offset; + if(asset->timecode > 0) + Units::totext(tc_str, asset->timecode, TIME_HMSF, 0, asset->frame_rate, 0); + //printf("tc: %s \n", tc_str); + av_dict_set(&st->metadata, "timecode", tc_str, 0); +#endif if( (vid->color_range = asset->ff_color_range) < 0 ) vid->color_range = file_base->file->preferences->yuv_color_range; switch( vid->color_range ) { @@ -2892,9 +2995,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec) if( (vid->color_space = asset->ff_color_space) < 0 ) vid->color_space = file_base->file->preferences->yuv_color_space; switch( vid->color_space ) { - case BC_COLORS_BT601: ctx->colorspace = AVCOL_SPC_SMPTE170M; break; + case BC_COLORS_BT601_NTSC: ctx->colorspace = AVCOL_SPC_SMPTE170M; break; + case BC_COLORS_BT601_PAL: ctx->colorspace = AVCOL_SPC_BT470BG; break; case BC_COLORS_BT709: ctx->colorspace = AVCOL_SPC_BT709; break; - case BC_COLORS_BT2020: ctx->colorspace = AVCOL_SPC_BT2020_NCL; break; + case BC_COLORS_BT2020_NCL: ctx->colorspace = AVCOL_SPC_BT2020_NCL; break; + case BC_COLORS_BT2020_CL: ctx->colorspace = AVCOL_SPC_BT2020_CL; break; } AVPixelFormat pix_fmt = av_get_pix_fmt(asset->ff_pixel_format); if( opt_hw_dev != 0 ) { @@ -3013,7 +3118,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec) } if( ret >= 0 ) { _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) ret = avcodec_copy_context(st->codec, ctx); +#else + ret = avcodec_parameters_to_context(ctx, st->codecpar); +#endif _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"") if( ret < 0 ) fprintf(stderr, "Could not copy the stream context\n"); @@ -3569,7 +3678,7 @@ float FFMPEG::ff_aspect_ratio(int stream) AVCodecParameters *par = ffvideo[stream]->st->codecpar; AVRational dar; AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, strm, NULL); - if (sar.num) { + if (sar.num && ffvideo[stream]->get_rotation_angle() == 0) { av_reduce(&dar.num, &dar.den, par->width * sar.num, par->height * sar.den, @@ -3658,7 +3767,11 @@ Preferences *FFMPEG::ff_prefs() double FFVideoStream::get_rotation_angle() { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + size_t size = 0; +#else int size = 0; +#endif int *matrix = (int*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, &size); int len = size/sizeof(*matrix); if( !matrix || len < 5 ) return 0; @@ -3883,7 +3996,11 @@ AVCodecContext *FFMPEG::activate_decoder(AVStream *st) AVDictionary *copts = 0; av_dict_copy(&copts, opts, 0); AVCodecID codec_id = st->codecpar->codec_id; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVCodec *decoder = 0; +#else AVCodec *decoder = 0; +#endif switch( st->codecpar->codec_type ) { case AVMEDIA_TYPE_VIDEO: if( opt_video_decoder ) @@ -4075,10 +4192,20 @@ void FFStream::load_markers(IndexMarks &marks, double rate) int in = 0; int64_t sz = marks.size(); int max_entries = fmt_ctx->max_index_size / sizeof(AVIndexEntry) - 1; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + int nb_ent = avformat_index_get_entries_count(st); +#endif +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) int nb_ent = st->nb_index_entries; +#endif // some formats already have an index if( nb_ent > 0 ) { +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) AVIndexEntry *ep = &st->index_entries[nb_ent-1]; +#endif +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVIndexEntry *ep = avformat_index_get_entry(st, nb_ent-1); +#endif int64_t tstmp = ep->timestamp; if( nudge != AV_NOPTS_VALUE ) tstmp -= nudge; double secs = ffmpeg->to_secs(tstmp, st->time_base);