X-Git-Url: https://git.cinelerra-gg.org/git/?a=blobdiff_plain;f=cinelerra-5.1%2Fcinelerra%2Fffmpeg.C;h=ee3edba59510a2f3cce711218a45fa9f1a865519;hb=HEAD;hp=a9f421785bdb8b9013fe227b49eabd44fe0e801a;hpb=2ed226ae2be30458e16669f2bf6f05b72470a4a5;p=goodguy%2Fcinelerra.git diff --git a/cinelerra-5.1/cinelerra/ffmpeg.C b/cinelerra-5.1/cinelerra/ffmpeg.C index a9f42178..9b8832dd 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 @@ -622,8 +641,8 @@ int FFStream::encode_frame(AVFrame *frame) if ( ret == AVERROR(EAGAIN) && !frame ) continue; FFPacket opkt; ret = avcodec_receive_packet(avctx, opkt); - if( !frame && ret == AVERROR_EOF ) return pkts; - if( ret < 0 ) break; + if( !frame && (ret == AVERROR_EOF || ret == AVERROR(EAGAIN) )) return pkts; + //if( ret < 0 ) break; ret = write_packet(opkt); if( ret < 0 ) break; ++pkts; @@ -861,13 +880,17 @@ void FFAudioStream::init_swr(int ichs, int ifmt, int irate) swr_ichs = ichs; swr_ifmt = ifmt; swr_irate = irate; if( ichs == channels && ifmt == AV_SAMPLE_FMT_FLT && irate == sample_rate ) return; - uint64_t ilayout = av_get_default_channel_layout(ichs); - if( !ilayout ) ilayout = ((uint64_t)1<= AV_VERSION_INT(61,3,100) + reserve(len+1, st->codecpar->ch_layout.nb_channels); +#else reserve(len+1, st->codecpar->channels); +#endif for( int ch=0; chnb_samples = frame_sz; frame->format = avctx->sample_fmt; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61,3,100) + frame->ch_layout.u.mask = avctx->ch_layout.u.mask; + av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout); +#else frame->channel_layout = avctx->channel_layout; +#endif frame->sample_rate = avctx->sample_rate; int ret = av_frame_get_buffer(frame, 0); if (ret < 0) @@ -985,7 +1017,7 @@ int FFAudioStream::load(int64_t pos, int len) while( ret>=0 && !flushed && curr_pos=0 ) { ret = read_frame(frame); if( ret > 0 && frame->nb_samples > 0 ) { - init_swr(frame->channels, frame->format, frame->sample_rate); + init_swr(frame->ch_layout.nb_channels, frame->format, frame->sample_rate); load_history(&frame->extended_data[0], frame->nb_samples); curr_pos += frame->nb_samples; } @@ -1141,7 +1173,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; @@ -1157,6 +1196,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; @@ -1168,7 +1208,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; @@ -2512,6 +2557,34 @@ int FFMPEG::info(char *text, int len) enum AVColorRange range = st->codecpar->color_range; const char *rg = av_color_range_name(range); report("/ range:%s\n", rg ? rg : unkn); + + AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, st, NULL); + AVRational display_aspect_ratio; + if(sar.num) { + + av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, + st->codecpar->width * (int64_t)sar.num, + st->codecpar->height * (int64_t)sar.den, + 1024 * 1024); +/* report(" Guessed SAR: %d:%d, ", sar.num, sar.den ); + report("DAR: %d:%d \n", display_aspect_ratio.num, display_aspect_ratio.den); */ + } + if (st->sample_aspect_ratio.num) + { + av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, + st->codecpar->width * (int64_t)st->sample_aspect_ratio.num, + st->codecpar->height * (int64_t)st->sample_aspect_ratio.den, + 1024 * 1024); + report(" container Detected SAR: %d:%d , DAR %d:%d \n", st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den); + } + if (st->codecpar->sample_aspect_ratio.num) + { + av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, + st->codecpar->width * (int64_t)st->codecpar->sample_aspect_ratio.num, + st->codecpar->height * (int64_t)st->codecpar->sample_aspect_ratio.den, + 1024 * 1024); + report(" codec Detected SAR: %d:%d , DAR %d:%d \n", st->codecpar->sample_aspect_ratio.num, st->codecpar->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den); + } double secs = to_secs(st->duration, st->time_base); int64_t length = secs * vid->frame_rate + 0.5; double ofs = to_secs((vid->nudge - st->start_time), st->time_base); @@ -2720,14 +2793,14 @@ int FFMPEG::open_decoder() ret = vid->create_filter(opt_video_filter); break; } case AVMEDIA_TYPE_AUDIO: { - if( avpar->channels < 1 ) continue; + if( avpar->ch_layout.nb_channels < 1 ) continue; if( avpar->sample_rate < 1 ) continue; has_audio = 1; int aidx = ffaudio.size(); FFAudioStream *aud = new FFAudioStream(this, st, aidx, i); ffaudio.append(aud); aud->channel0 = astrm_index.size(); - aud->channels = avpar->channels; + aud->channels = avpar->ch_layout.nb_channels; for( int ch=0; chchannels; ++ch ) astrm_index.append(ffidx(aidx, ch)); aud->sample_rate = avpar->sample_rate; @@ -2883,10 +2956,13 @@ int FFMPEG::open_encoder(const char *type, const char *spec) FFAudioStream *aud = new FFAudioStream(this, st, aidx, fidx); aud->avctx = ctx; ffaudio.append(aud); fst = aud; aud->sample_rate = asset->sample_rate; - ctx->channels = aud->channels = asset->channels; + ctx->ch_layout.nb_channels = aud->channels = asset->channels; for( int ch=0; chchannels; ++ch ) astrm_index.append(ffidx(aidx, ch)); - ctx->channel_layout = av_get_default_channel_layout(ctx->channels); + AVChannelLayout ch_layout; + av_channel_layout_default(&ch_layout, ctx->ch_layout.nb_channels); + ctx->ch_layout.u.mask = ch_layout.u.mask; + av_channel_layout_copy(&ctx->ch_layout, &ch_layout); ctx->sample_rate = check_sample_rate(codec, asset->sample_rate); if( !ctx->sample_rate ) { eprintf(_("check_sample_rate failed %s\n"), filename); @@ -2898,10 +2974,12 @@ int FFMPEG::open_encoder(const char *type, const char *spec) if( sample_fmt == AV_SAMPLE_FMT_NONE ) sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16; ctx->sample_fmt = sample_fmt; - uint64_t layout = av_get_default_channel_layout(ctx->channels); - aud->resample_context = swr_alloc_set_opts(NULL, - layout, ctx->sample_fmt, aud->sample_rate, - layout, AV_SAMPLE_FMT_FLT, ctx->sample_rate, + //uint64_t layout = av_get_default_channel_layout(ctx->ch_layout.nb_channels); + AVChannelLayout layout; + av_channel_layout_default(&layout, ctx->ch_layout.nb_channels); + swr_alloc_set_opts2(&aud->resample_context, + &layout, ctx->sample_fmt, aud->sample_rate, + &layout, AV_SAMPLE_FMT_FLT, ctx->sample_rate, 0, NULL); swr_init(aud->resample_context); aud->writing = -1; @@ -2946,6 +3024,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 ) { @@ -2997,6 +3083,13 @@ int FFMPEG::open_encoder(const char *type, const char *spec) frame_rate.num, frame_rate.den, INT_MAX); ctx->framerate = (AVRational) { frame_rate.num, frame_rate.den }; ctx->time_base = (AVRational) { frame_rate.den, frame_rate.num }; + if(!strcmp(format_name, "webm") || !strcmp(format_name, "matroska") || !strcmp(format_name, "mov") || + !strcmp(format_name, "qt") || !strcmp(format_name, "mp4") || !strcmp(format_name, "avi") || + !strcmp(format_name, "dv") || !strcmp(format_name, "yuv4mpegpipe")) + { + if (to_sample_aspect_ratio(asset).den > 0) + st->sample_aspect_ratio = to_sample_aspect_ratio(asset); + } st->avg_frame_rate = frame_rate; st->time_base = ctx->time_base; vid->writing = -1; @@ -3854,7 +3947,7 @@ int FFAudioStream::create_filter(const char *filter_spec) snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx", st->time_base.num, st->time_base.den, avpar->sample_rate, - av_get_sample_fmt_name(sample_fmt), avpar->channel_layout); + av_get_sample_fmt_name(sample_fmt), avpar->ch_layout.u.mask); if( ret >= 0 ) { filt_ctx = 0; ret = insert_filter("abuffer", args, "in"); @@ -3872,8 +3965,8 @@ int FFAudioStream::create_filter(const char *filter_spec) AV_OPT_SEARCH_CHILDREN); if( ret >= 0 ) ret = av_opt_set_bin(buffersink_ctx, "channel_layouts", - (uint8_t*)&avpar->channel_layout, - sizeof(avpar->channel_layout), AV_OPT_SEARCH_CHILDREN); + (uint8_t*)&avpar->ch_layout.u.mask, + sizeof(avpar->ch_layout.u.mask), AV_OPT_SEARCH_CHILDREN); if( ret >= 0 ) ret = av_opt_set_bin(buffersink_ctx, "sample_rates", (uint8_t*)&sample_rate, sizeof(sample_rate), @@ -4125,7 +4218,7 @@ printf("audio%d pad %jd %jd (%jd)\n", aud->idx, pos, aud->curr_pos, pos-aud->cur } while( (ret=aud->decode_frame(frame)) > 0 ) { //if( frame->channels != nch ) break; - aud->init_swr(frame->channels, frame->format, frame->sample_rate); + aud->init_swr(frame->ch_layout.nb_channels, frame->format, frame->sample_rate); float *samples; int len = aud->get_samples(samples, &frame->extended_data[0], frame->nb_samples);