static int configure_lavrr(struct af_instance *af, struct mp_audio *in, struct mp_audio *out) { struct af_resample *s = af->priv; enum AVSampleFormat in_samplefmt = af_to_avformat(in->format); enum AVSampleFormat out_samplefmt = af_to_avformat(out->format); if (in_samplefmt == AV_SAMPLE_FMT_NONE || out_samplefmt == AV_SAMPLE_FMT_NONE) return AF_ERROR; avresample_close(s->avrctx); avresample_close(s->avrctx_out); s->ctx.out_rate = out->rate; s->ctx.in_rate = in->rate; s->ctx.out_format = out->format; s->ctx.in_format = in->format; s->ctx.out_channels= out->channels; s->ctx.in_channels = in->channels; s->ctx.filter_size = s->opts.filter_size; s->ctx.phase_shift = s->opts.phase_shift; s->ctx.linear = s->opts.linear; s->ctx.cutoff = s->opts.cutoff; av_opt_set_int(s->avrctx, "filter_size", s->ctx.filter_size, 0); av_opt_set_int(s->avrctx, "phase_shift", s->ctx.phase_shift, 0); av_opt_set_int(s->avrctx, "linear_interp", s->ctx.linear, 0); av_opt_set_double(s->avrctx, "cutoff", s->ctx.cutoff, 0); if (parse_avopts(s->avrctx, s->avopts) < 0) { mp_msg(MSGT_VFILTER, MSGL_FATAL, "af_lavrresample: could not set opts: '%s'\n", s->avopts); return AF_ERROR; } struct mp_chmap map_in = in->channels; struct mp_chmap map_out = out->channels; // Try not to do any remixing if at least one is "unknown". if (mp_chmap_is_unknown(&map_in) || mp_chmap_is_unknown(&map_out)) { mp_chmap_set_unknown(&map_in, map_in.num); mp_chmap_set_unknown(&map_out, map_out.num); } // unchecked: don't take any channel reordering into account uint64_t in_ch_layout = mp_chmap_to_lavc_unchecked(&map_in); uint64_t out_ch_layout = mp_chmap_to_lavc_unchecked(&map_out); av_opt_set_int(s->avrctx, "in_channel_layout", in_ch_layout, 0); av_opt_set_int(s->avrctx, "out_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx, "in_sample_rate", s->ctx.in_rate, 0); av_opt_set_int(s->avrctx, "out_sample_rate", s->ctx.out_rate, 0); av_opt_set_int(s->avrctx, "in_sample_fmt", in_samplefmt, 0); av_opt_set_int(s->avrctx, "out_sample_fmt", out_samplefmt, 0); struct mp_chmap in_lavc; mp_chmap_from_lavc(&in_lavc, in_ch_layout); mp_chmap_get_reorder(s->reorder_in, &map_in, &in_lavc); struct mp_chmap out_lavc; mp_chmap_from_lavc(&out_lavc, out_ch_layout); mp_chmap_get_reorder(s->reorder_out, &out_lavc, &map_out); // Same configuration; we just reorder. av_opt_set_int(s->avrctx_out, "in_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx_out, "out_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx_out, "in_sample_fmt", out_samplefmt, 0); av_opt_set_int(s->avrctx_out, "out_sample_fmt", out_samplefmt, 0); av_opt_set_int(s->avrctx_out, "in_sample_rate", s->ctx.out_rate, 0); av_opt_set_int(s->avrctx_out, "out_sample_rate", s->ctx.out_rate, 0); #if USE_SET_CHANNEL_MAPPING // API has weird requirements, quoting avresample.h: // * This function can only be called when the allocated context is not open. // * Also, the input channel layout must have already been set. avresample_set_channel_mapping(s->avrctx, s->reorder_in); avresample_set_channel_mapping(s->avrctx_out, s->reorder_out); #endif if (avresample_open(s->avrctx) < 0 || avresample_open(s->avrctx_out) < 0) { mp_msg(MSGT_AFILTER, MSGL_ERR, "[lavrresample] Cannot open " "Libavresample Context. \n"); return AF_ERROR; } return AF_OK; }
static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ AVFormatContext *avfc; AVDictionaryEntry *t = NULL; lavf_priv_t *priv= demuxer->priv; int i; char mp_filename[256]="mp:"; stream_seek(demuxer->stream, 0); avfc = avformat_alloc_context(); if (opt_cryptokey) parse_cryptokey(avfc, opt_cryptokey); if (user_correct_pts != 0) avfc->flags |= AVFMT_FLAG_GENPTS; if (index_mode == 0) avfc->flags |= AVFMT_FLAG_IGNIDX; if(opt_probesize) { if (av_opt_set_int(avfc, "probesize", opt_probesize, 0) < 0) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize); } if(opt_analyzeduration) { if (av_opt_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE, 0) < 0) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration); } if(opt_avopt){ if(parse_avopts(avfc, opt_avopt) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt); return NULL; } } if(demuxer->stream->url) { if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14)) av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename)); else av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename)); } else av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename)); if (!(priv->avif->flags & AVFMT_NOFILE)) { priv->pb = avio_alloc_context(priv->buffer, BIO_BUFFER_SIZE, 0, demuxer, mp_read, NULL, mp_seek); priv->pb->read_seek = mp_read_seek; if (!demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK) priv->pb->seekable = 0; avfc->pb = priv->pb; } if(avformat_open_input(&avfc, mp_filename, priv->avif, NULL)<0){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n"); return NULL; } priv->avfc= avfc; if(avformat_find_stream_info(avfc, NULL) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n"); return NULL; } /* Add metadata. */ while((t = av_dict_get(avfc->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) demux_info_add(demuxer, t->key, t->value); for(i=0; i < avfc->nb_chapters; i++) { AVChapter *c = avfc->chapters[i]; uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000}); uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000}); t = av_dict_get(c->metadata, "title", NULL, 0); demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end); } for(i=0; i<avfc->nb_streams; i++) handle_stream(demuxer, avfc, i); priv->nb_streams_last = avfc->nb_streams; if(avfc->nb_programs) { int p; for (p = 0; p < avfc->nb_programs; p++) { AVProgram *program = avfc->programs[p]; t = av_dict_get(program->metadata, "title", NULL, 0); mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : ""); mp_msg(MSGT_IDENTIFY, MSGL_V, "PROGRAM_ID=%d\n", program->id); } } mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams); mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD); if(!priv->audio_streams) demuxer->audio->id=-2; // nosound // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio; if(!priv->video_streams){ if(!priv->audio_streams){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n"); return NULL; } demuxer->video->id=-2; // audio-only } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video; return demuxer; }
int mpae_init_lavc(audio_encoder_t *encoder) { encoder->params.samples_per_frame = encoder->params.sample_rate; encoder->params.bitrate = encoder->params.sample_rate * encoder->params.channels * 2 * 8; if(!lavc_param_acodec) { mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName); return 0; } init_avcodec(); lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec); if (!lavc_acodec) { mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec); return 0; } if(lavc_param_atag == 0) { lavc_param_atag = av_codec_get_tag(mp_wav_taglists, lavc_acodec->id); if(!lavc_param_atag) { mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't find wav tag for specified codec, exit\n"); return 0; } } lavc_actx = avcodec_alloc_context(); if(lavc_actx == NULL) { mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext); return 0; } lavc_actx->codec_type = CODEC_TYPE_AUDIO; lavc_actx->codec_id = lavc_acodec->id; // put sample parameters lavc_actx->channels = encoder->params.channels; lavc_actx->sample_rate = encoder->params.sample_rate; lavc_actx->time_base.num = 1; lavc_actx->time_base.den = encoder->params.sample_rate; if(lavc_param_abitrate<1000) lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate * 1000; else lavc_actx->bit_rate = encoder->params.bitrate = lavc_param_abitrate; if(lavc_param_audio_avopt){ if(parse_avopts(lavc_actx, lavc_param_audio_avopt) < 0){ mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_audio_avopt); return 0; } } /* * Special case for adpcm_ima_wav. * The bitrate is only dependent on samplerate. * We have to known frame_size and block_align in advance, * so I just copied the code from libavcodec/adpcm.c * * However, ms adpcm_ima_wav uses a block_align of 2048, * lavc defaults to 1024 */ if(lavc_param_atag == 0x11) { int blkalign = 2048; int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize; } if((lavc_param_audio_global_header&1) /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){ lavc_actx->flags |= CODEC_FLAG_GLOBAL_HEADER; } if(lavc_param_audio_global_header&2){ lavc_actx->flags2 |= CODEC_FLAG2_LOCAL_HEADER; } if(avcodec_open(lavc_actx, lavc_acodec) < 0) { mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate); return 0; } if(lavc_param_atag == 0x11) { lavc_actx->block_align = 2048; lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1; } encoder->decode_buffer_size = lavc_actx->frame_size * 2 * encoder->params.channels; while (encoder->decode_buffer_size < 1024) encoder->decode_buffer_size *= 2; encoder->bind = bind_lavc; encoder->get_frame_size = get_frame_size; encoder->encode = encode_lavc; encoder->close = close_lavc; return 1; }
int muxer_init_muxer_lavf(muxer_t *muxer) { muxer_priv_t *priv; AVOutputFormat *fmt = NULL; init_avformat(); if (conf_format && strcmp(conf_format, "help") == 0) { list_formats(); return 0; } mp_msg(MSGT_MUXER, MSGL_WARN, "** MUXER_LAVF *****************************************************************\n"); mp_msg(MSGT_MUXER, MSGL_WARN, "REMEMBER: MEncoder's libavformat muxing is presently broken and can generate\n" "INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer\n" "will play these INCORRECT files as if nothing were wrong!\n" "*******************************************************************************\n"); priv = calloc(1, sizeof(muxer_priv_t)); if(priv == NULL) return 0; priv->oc = avformat_alloc_context(); if(!priv->oc) { mp_msg(MSGT_MUXER, MSGL_FATAL, "Could not get format context.\n"); goto fail; } if(conf_format) fmt = av_guess_format(conf_format, NULL, NULL); if(! fmt) fmt = av_guess_format(NULL, out_filename, NULL); if(! fmt) { mp_msg(MSGT_MUXER, MSGL_FATAL, "Cannot get specified format.\n"); goto fail; } priv->oc->oformat = fmt; priv->oc->packet_size= mux_packet_size; priv->oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE); if (info_name) av_dict_set(&priv->oc->metadata, "title", info_name, 0); if (info_artist) av_dict_set(&priv->oc->metadata, "author", info_artist, 0); if (info_genre) av_dict_set(&priv->oc->metadata, "genre", info_genre, 0); if (info_copyright) av_dict_set(&priv->oc->metadata, "copyright", info_copyright, 0); if (info_comment) av_dict_set(&priv->oc->metadata, "comment", info_comment, 0); if(mux_avopt){ if(parse_avopts(priv->oc, mux_avopt) < 0){ mp_msg(MSGT_MUXER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal.\n", mux_avopt); goto fail; } } priv->oc->pb = avio_alloc_context(priv->buffer, BIO_BUFFER_SIZE, 1, muxer, NULL, mp_write, mp_seek); if ((muxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK) priv->oc->pb->seekable = 0; muxer->priv = priv; muxer->cont_new_stream = &lavf_new_stream; muxer->cont_write_chunk = &write_chunk; muxer->cont_write_header = &write_header; muxer->cont_write_index = &write_trailer; muxer->fix_stream_parameters = &fix_parameters; mp_msg(MSGT_MUXER, MSGL_INFO, "OK, exit.\n"); return 1; fail: free(priv); return 0; }
static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = (struct af_resample *) af->priv; struct mp_audio *in = (struct mp_audio *) arg; struct mp_audio *out = (struct mp_audio *) af->data; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio orig_in = *in; if (((out->rate == in->rate) || (out->rate == 0)) && (out->format == in->format) && (mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) && s->allow_detach) return AF_DETACH; if (out->rate == 0) out->rate = in->rate; if (mp_chmap_is_empty(&out->channels)) mp_audio_set_channels(out, &in->channels); enum AVSampleFormat in_samplefmt = af_to_avformat(in->format); if (in_samplefmt == AV_SAMPLE_FMT_NONE) { mp_audio_set_format(in, AF_FORMAT_FLOAT_NE); in_samplefmt = af_to_avformat(in->format); } enum AVSampleFormat out_samplefmt = af_to_avformat(out->format); if (out_samplefmt == AV_SAMPLE_FMT_NONE) { mp_audio_set_format(out, in->format); out_samplefmt = in_samplefmt; } af->mul = (double) (out->rate * out->nch) / (in->rate * in->nch); af->delay = out->nch * s->opts.filter_size / FFMIN(af->mul, 1); if (needs_lavrctx_reconfigure(s, in, out)) { avresample_close(s->avrctx); avresample_close(s->avrctx_out); s->ctx.out_rate = out->rate; s->ctx.in_rate = in->rate; s->ctx.out_format = out->format; s->ctx.in_format = in->format; s->ctx.out_channels= out->channels; s->ctx.in_channels = in->channels; s->ctx.filter_size = s->opts.filter_size; s->ctx.phase_shift = s->opts.phase_shift; s->ctx.linear = s->opts.linear; s->ctx.cutoff = s->opts.cutoff; ctx_opt_set_int("filter_size", s->ctx.filter_size); ctx_opt_set_int("phase_shift", s->ctx.phase_shift); ctx_opt_set_int("linear_interp", s->ctx.linear); ctx_opt_set_dbl("cutoff", s->ctx.cutoff); if (parse_avopts(s->avrctx, s->avopts) < 0) { mp_msg(MSGT_VFILTER, MSGL_FATAL, "af_lavrresample: could not set opts: '%s'\n", s->avopts); return AF_ERROR; } struct mp_chmap map_in = in->channels; struct mp_chmap map_out = out->channels; // Try not to do any remixing if at least one is "unknown". if (mp_chmap_is_unknown(&map_in) || mp_chmap_is_unknown(&map_out)) { mp_chmap_set_unknown(&map_in, map_in.num); mp_chmap_set_unknown(&map_out, map_out.num); } // unchecked: don't take any channel reordering into account uint64_t in_ch_layout = mp_chmap_to_lavc_unchecked(&map_in); uint64_t out_ch_layout = mp_chmap_to_lavc_unchecked(&map_out); ctx_opt_set_int("in_channel_layout", in_ch_layout); ctx_opt_set_int("out_channel_layout", out_ch_layout); ctx_opt_set_int("in_sample_rate", s->ctx.in_rate); ctx_opt_set_int("out_sample_rate", s->ctx.out_rate); ctx_opt_set_int("in_sample_fmt", in_samplefmt); ctx_opt_set_int("out_sample_fmt", out_samplefmt); struct mp_chmap in_lavc; mp_chmap_from_lavc(&in_lavc, in_ch_layout); mp_chmap_get_reorder(s->reorder_in, &map_in, &in_lavc); struct mp_chmap out_lavc; mp_chmap_from_lavc(&out_lavc, out_ch_layout); mp_chmap_get_reorder(s->reorder_out, &out_lavc, &map_out); // Same configuration; we just reorder. av_opt_set_int(s->avrctx_out, "in_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx_out, "out_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx_out, "in_sample_fmt", out_samplefmt, 0); av_opt_set_int(s->avrctx_out, "out_sample_fmt", out_samplefmt, 0); av_opt_set_int(s->avrctx_out, "in_sample_rate", s->ctx.out_rate, 0); av_opt_set_int(s->avrctx_out, "out_sample_rate", s->ctx.out_rate, 0); #if USE_SET_CHANNEL_MAPPING // API has weird requirements, quoting avresample.h: // * This function can only be called when the allocated context is not open. // * Also, the input channel layout must have already been set. avresample_set_channel_mapping(s->avrctx, s->reorder_in); avresample_set_channel_mapping(s->avrctx_out, s->reorder_out); #endif if (avresample_open(s->avrctx) < 0 || avresample_open(s->avrctx_out) < 0) { mp_msg(MSGT_AFILTER, MSGL_ERR, "[lavrresample] Cannot open " "Libavresample Context. \n"); return AF_ERROR; } } return ((in->format == orig_in.format) && mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; } case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET: { if (af_to_avformat(*(int*)arg) == AV_SAMPLE_FMT_NONE) return AF_FALSE; mp_audio_set_format(af->data, *(int*)arg); return AF_OK; } case AF_CONTROL_CHANNELS | AF_CONTROL_SET: { mp_audio_set_channels(af->data, (struct mp_chmap *)arg); return AF_OK; } case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: out->rate = *(int *)arg; return AF_OK; } return AF_UNKNOWN; }
static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt){ int size, i; char *p; AVDictionary *opts = NULL; mux_v->bih->biWidth=width; mux_v->bih->biHeight=height; mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); mp_msg(MSGT_MENCODER, MSGL_INFO,"videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n", mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression); lavc_venc_context->width = width; lavc_venc_context->height = height; if (lavc_param_vbitrate > 16000) /* != -1 */ lavc_venc_context->bit_rate = lavc_param_vbitrate; else if (lavc_param_vbitrate >= 0) /* != -1 */ lavc_venc_context->bit_rate = lavc_param_vbitrate*1000; else lavc_venc_context->bit_rate = 800000; /* default */ mux_v->avg_rate= lavc_venc_context->bit_rate; lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000; lavc_venc_context->time_base= (AVRational){mux_v->h.dwScale, mux_v->h.dwRate}; lavc_venc_context->qmin= lavc_param_vqmin; lavc_venc_context->qmax= lavc_param_vqmax; lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5); lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5); lavc_venc_context->mb_lmin= (int)(FF_QP2LAMBDA * lavc_param_mb_lmin + 0.5); lavc_venc_context->mb_lmax= (int)(FF_QP2LAMBDA * lavc_param_mb_lmax + 0.5); lavc_venc_context->max_qdiff= lavc_param_vqdiff; lavc_venc_context->qcompress= lavc_param_vqcompress; lavc_venc_context->qblur= lavc_param_vqblur; lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames; lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor; lavc_venc_context->rc_strategy= lavc_param_vrc_strategy; lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy; lavc_venc_context->b_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vb_qoffset + 0.5); lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold; lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold; lavc_venc_context->rtp_payload_size= lavc_param_packet_size; lavc_venc_context->strict_std_compliance= lavc_param_strict; lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor; lavc_venc_context->i_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vi_qoffset + 0.5); lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish; lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp; lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq; lavc_venc_context->rc_eq= lavc_param_rc_eq; mux_v->max_rate= lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000; lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000; mux_v->vbv_size= lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000; lavc_venc_context->rc_initial_buffer_occupancy= lavc_venc_context->rc_buffer_size * lavc_param_rc_initial_buffer_occupancy; lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity; lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx; lavc_venc_context->debug= lavc_param_debug; lavc_venc_context->last_predictor_count= lavc_param_last_pred; lavc_venc_context->pre_me= lavc_param_pre_me; lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp; lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size; lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality; lavc_venc_context->me_range= lavc_param_me_range; lavc_venc_context->intra_quant_bias= lavc_param_ibias; lavc_venc_context->inter_quant_bias= lavc_param_pbias; lavc_venc_context->coder_type= lavc_param_coder; lavc_venc_context->context_model= lavc_param_context; lavc_venc_context->scenechange_threshold= lavc_param_sc_threshold; lavc_venc_context->noise_reduction= lavc_param_noise_reduction; lavc_venc_context->quantizer_noise_shaping= lavc_param_qns; lavc_venc_context->inter_threshold= lavc_param_inter_threshold; lavc_venc_context->nsse_weight= lavc_param_nssew; lavc_venc_context->frame_skip_threshold= lavc_param_skip_threshold; lavc_venc_context->frame_skip_factor= lavc_param_skip_factor; lavc_venc_context->frame_skip_exp= lavc_param_skip_exp; lavc_venc_context->frame_skip_cmp= lavc_param_skip_cmp; if (lavc_param_intra_matrix) { char *tmp; lavc_venc_context->intra_matrix = av_malloc(sizeof(*lavc_venc_context->intra_matrix)*64); i = 0; while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64)) { if (!tmp || (tmp && !strlen(tmp))) break; lavc_venc_context->intra_matrix[i++] = atoi(tmp); } if (i != 64) av_freep(&lavc_venc_context->intra_matrix); else mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n"); } if (lavc_param_inter_matrix) { char *tmp; lavc_venc_context->inter_matrix = av_malloc(sizeof(*lavc_venc_context->inter_matrix)*64); i = 0; while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64)) { if (!tmp || (tmp && !strlen(tmp))) break; lavc_venc_context->inter_matrix[i++] = atoi(tmp); } if (i != 64) av_freep(&lavc_venc_context->inter_matrix); else mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n"); } p= lavc_param_rc_override_string; for(i=0; p; i++){ int start, end, q; int e=sscanf(p, "%d,%d,%d", &start, &end, &q); if(e!=3){ mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n"); return 0; } lavc_venc_context->rc_override= realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1)); lavc_venc_context->rc_override[i].start_frame= start; lavc_venc_context->rc_override[i].end_frame = end; if(q>0){ lavc_venc_context->rc_override[i].qscale= q; lavc_venc_context->rc_override[i].quality_factor= 1.0; } else{ lavc_venc_context->rc_override[i].qscale= 0; lavc_venc_context->rc_override[i].quality_factor= -q/100.0; } p= strchr(p, '/'); if(p) p++; } lavc_venc_context->rc_override_count=i; lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant; lavc_venc_context->dct_algo= lavc_param_fdct; lavc_venc_context->idct_algo= lavc_param_idct; lavc_venc_context->lumi_masking= lavc_param_lumi_masking; lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking; lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking; lavc_venc_context->p_masking= lavc_param_p_masking; lavc_venc_context->dark_masking= lavc_param_dark_masking; lavc_venc_context->border_masking = lavc_param_border_masking; if (lavc_param_aspect != NULL) { int par_width, par_height, e; float ratio=0; e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height); if(e==2){ if(par_height) ratio= (float)par_width / (float)par_height; }else{ e= sscanf (lavc_param_aspect, "%f", &ratio); } if (e && ratio > 0.1 && ratio < 10.0) { lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255); mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n", lavc_venc_context->sample_aspect_ratio.num, lavc_venc_context->sample_aspect_ratio.den); mux_v->aspect = ratio; mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio); } else { mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect); return 0; } } else if (lavc_param_autoaspect) { lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255); mux_v->aspect = (float)d_width/d_height; } /* keyframe interval */ if (lavc_param_keyint >= 0) /* != -1 */ lavc_venc_context->gop_size = lavc_param_keyint; else lavc_venc_context->gop_size = 250; /* default */ lavc_venc_context->flags = 0; if (lavc_param_mb_decision) { mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_HighQualityEncodingSelected); lavc_venc_context->mb_decision= lavc_param_mb_decision; } lavc_venc_context->me_cmp= lavc_param_me_cmp; lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp; lavc_venc_context->mb_cmp= lavc_param_mb_cmp; #ifdef FF_CMP_VSAD lavc_venc_context->ildct_cmp= lavc_param_ildct_cmp; #endif lavc_venc_context->dia_size= lavc_param_dia_size; lavc_venc_context->flags|= lavc_param_qpel; lavc_venc_context->trellis = lavc_param_trell; lavc_venc_context->flags|= lavc_param_lowdelay; lavc_venc_context->flags|= lavc_param_bit_exact; lavc_venc_context->flags|= lavc_param_aic; if (lavc_param_aiv) av_dict_set(&opts, "aiv", "1", 0); if (lavc_param_umv) av_dict_set(&opts, "umv", "1", 0); if (lavc_param_obmc) av_dict_set(&opts, "obmc", "1", 0); lavc_venc_context->flags|= lavc_param_loop; lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0; if (lavc_param_data_partitioning) av_dict_set(&opts, "data_partitioning", "1", 0); lavc_venc_context->flags|= lavc_param_cbp; lavc_venc_context->flags|= lavc_param_mv0; lavc_venc_context->flags|= lavc_param_qp_rd; if (lavc_param_ss) av_dict_set(&opts, "structured_slices", "1", 0); if (lavc_param_alt) av_dict_set(&opts, "alternate_scan", "1", 0); lavc_venc_context->flags|= lavc_param_ilme; lavc_venc_context->flags|= lavc_param_gmc; #ifdef CODEC_FLAG_CLOSED_GOP lavc_venc_context->flags|= lavc_param_closed_gop; #endif lavc_venc_context->flags|= lavc_param_gray; if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP; if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT; lavc_venc_context->flags|= lavc_param_psnr; lavc_venc_context->intra_dc_precision = lavc_param_dc_precision - 8; lavc_venc_context->prediction_method= lavc_param_prediction_method; lavc_venc_context->brd_scale = lavc_param_brd_scale; lavc_venc_context->bidir_refine = lavc_param_bidir_refine; lavc_venc_context->scenechange_factor = lavc_param_sc_factor; if((lavc_param_video_global_header&1) /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){ lavc_venc_context->flags |= CODEC_FLAG_GLOBAL_HEADER; } if(lavc_param_video_global_header&2){ lavc_venc_context->flags2 |= CODEC_FLAG2_LOCAL_HEADER; } lavc_venc_context->mv0_threshold = lavc_param_mv0_threshold; lavc_venc_context->refs = lavc_param_refs; lavc_venc_context->b_sensitivity = lavc_param_b_sensitivity; lavc_venc_context->level = lavc_param_level; if(lavc_param_avopt){ if(parse_avopts(lavc_venc_context, lavc_param_avopt) < 0){ mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_avopt); return 0; } } mux_v->imgfmt = lavc_param_format; lavc_venc_context->pix_fmt = imgfmt2pixfmt(lavc_param_format); if (lavc_venc_context->pix_fmt == PIX_FMT_NONE) return 0; if(!stats_file) { /* lavc internal 2pass bitrate control */ switch(lavc_param_vpass){ case 2: case 3: lavc_venc_context->flags|= CODEC_FLAG_PASS2; stats_file= fopen(passtmpfile, "rb"); if(stats_file==NULL){ mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); return 0; } fseek(stats_file, 0, SEEK_END); size= ftell(stats_file); fseek(stats_file, 0, SEEK_SET); lavc_venc_context->stats_in= av_malloc(size + 1); lavc_venc_context->stats_in[size]=0; if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){ mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile); return 0; } if(lavc_param_vpass == 2) break; else fclose(stats_file); /* fall through */ case 1: lavc_venc_context->flags|= CODEC_FLAG_PASS1; stats_file= fopen(passtmpfile, "wb"); if(stats_file==NULL){ mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); return 0; } if(lavc_param_turbo && (lavc_param_vpass == 1)) { /* uses SAD comparison functions instead of other hungrier */ lavc_venc_context->me_pre_cmp = 0; lavc_venc_context->me_cmp = 0; lavc_venc_context->me_sub_cmp = 0; lavc_venc_context->mb_cmp = 2; /* Disables diamond motion estimation */ lavc_venc_context->pre_dia_size = 0; lavc_venc_context->dia_size = 1; lavc_venc_context->quantizer_noise_shaping = 0; // qns=0 lavc_venc_context->noise_reduction = 0; // nr=0 lavc_venc_context->mb_decision = 0; // mbd=0 ("realtime" encoding) lavc_venc_context->flags &= ~CODEC_FLAG_QPEL; lavc_venc_context->flags &= ~CODEC_FLAG_4MV; lavc_venc_context->trellis = 0; lavc_venc_context->flags &= ~CODEC_FLAG_CBP_RD; lavc_venc_context->flags &= ~CODEC_FLAG_QP_RD; lavc_venc_context->flags &= ~CODEC_FLAG_MV0; } break; } } lavc_venc_context->me_method = ME_ZERO+lavc_param_vme; /* fixed qscale :p */ if (lavc_param_vqscale >= 0.0) { mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_UsingConstantQscale, lavc_param_vqscale); lavc_venc_context->flags |= CODEC_FLAG_QSCALE; lavc_venc_context->global_quality= vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5); } lavc_venc_context->thread_count = lavc_param_threads; lavc_venc_context->thread_type = FF_THREAD_FRAME | FF_THREAD_SLICE; if (avcodec_open2(lavc_venc_context, vf->priv->codec, &opts) != 0) { mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); return 0; } av_dict_free(&opts); /* free second pass buffer, its not needed anymore */ av_freep(&lavc_venc_context->stats_in); if(lavc_venc_context->bits_per_coded_sample) mux_v->bih->biBitCount= lavc_venc_context->bits_per_coded_sample; if(lavc_venc_context->extradata_size){ mux_v->bih= realloc(mux_v->bih, sizeof(*mux_v->bih) + lavc_venc_context->extradata_size); memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size); mux_v->bih->biSize= sizeof(*mux_v->bih) + lavc_venc_context->extradata_size; } mux_v->decoder_delay = lavc_venc_context->max_b_frames ? 1 : 0; return 1; }
// init driver static int init(sh_video_t *sh){ AVCodecContext *avctx; vd_ffmpeg_ctx *ctx; AVCodec *lavc_codec; int lowres_w=0; int do_vis_debug= lavc_param_vismv || (lavc_param_debug&(FF_DEBUG_VIS_MB_TYPE|FF_DEBUG_VIS_QP)); // slice is rather broken with threads, so disable that combination unless // explicitly requested int use_slices = vd_use_slices > 0 || (vd_use_slices < 0 && lavc_param_threads <= 1); AVDictionary *opts = NULL; init_avcodec(); ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx)); if (!ctx) return 0; memset(ctx, 0, sizeof(vd_ffmpeg_ctx)); lavc_codec = avcodec_find_decoder_by_name(sh->codec->dll); if(!lavc_codec){ mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MissingLAVCcodec, sh->codec->dll); uninit(sh); return 0; } if (auto_threads) { #if (defined(__MINGW32__) && HAVE_PTHREADS) lavc_param_threads = pthread_num_processors_np(); #elif defined(__linux__) /* Code stolen from x264 :) */ unsigned int bit; int np; cpu_set_t p_aff; memset( &p_aff, 0, sizeof(p_aff) ); sched_getaffinity( 0, sizeof(p_aff), &p_aff ); for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ ) np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1; lavc_param_threads = np; #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__) || defined (__NetBSD__) || defined(__OpenBSD__) /* Code stolen from x264 :) */ int numberOfCPUs; size_t length = sizeof( numberOfCPUs ); #if defined (__NetBSD__) || defined(__OpenBSD__) int mib[2] = { CTL_HW, HW_NCPU }; if( sysctl(mib, 2, &numberOfCPUs, &length, NULL, 0) ) #else if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) ) #endif { numberOfCPUs = 1; } lavc_param_threads = numberOfCPUs; #endif if(lavc_codec->id == CODEC_ID_H264 || lavc_codec->id == CODEC_ID_FFH264 || lavc_codec->id == CODEC_ID_MPEG2TS || lavc_codec->id == CODEC_ID_MPEG2VIDEO_XVMC) { if (lavc_param_threads > 16) lavc_param_threads = 16; if (lavc_param_threads > 1 && (lavc_codec->id == CODEC_ID_MPEG2VIDEO || lavc_codec->id == CODEC_ID_MPEG2TS || lavc_codec->id == CODEC_ID_MPEG2VIDEO_XVMC)) vd_use_slices = 0; mp_msg(MSGT_DECVIDEO, MSGL_INFO, "Spawning %d decoding thread%s...\n", lavc_param_threads, lavc_param_threads == 1 ? "" : "s"); } else { lavc_param_threads = 1; } } if(use_slices && (lavc_codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND) && !do_vis_debug) ctx->do_slices=1; if(lavc_codec->capabilities&CODEC_CAP_DR1 && !do_vis_debug && lavc_codec->id != CODEC_ID_INTERPLAY_VIDEO && lavc_codec->id != CODEC_ID_VP8) ctx->do_dr1=1; ctx->nonref_dr = lavc_codec->id == CODEC_ID_H264; ctx->ip_count= ctx->b_count= 0; ctx->pic = avcodec_alloc_frame(); ctx->avctx = avcodec_alloc_context3(lavc_codec); avctx = ctx->avctx; avctx->opaque = sh; avctx->codec_id = lavc_codec->id; avctx->get_format = get_format; if(ctx->do_dr1){ avctx->flags|= CODEC_FLAG_EMU_EDGE; avctx-> get_buffer= get_buffer; avctx->release_buffer= release_buffer; avctx-> reget_buffer= get_buffer; } avctx->flags|= lavc_param_bitexact; avctx->coded_width = sh->disp_w; avctx->coded_height= sh->disp_h; avctx->workaround_bugs= lavc_param_workaround_bugs; switch (lavc_param_error_resilience) { case 5: avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL; break; case 4: case 3: avctx->err_recognition |= AV_EF_AGGRESSIVE; // Fallthrough case 2: avctx->err_recognition |= AV_EF_COMPLIANT; // Fallthrough case 1: avctx->err_recognition |= AV_EF_CAREFUL; } lavc_param_gray|= CODEC_FLAG_GRAY; #ifdef CODEC_FLAG2_SHOW_ALL if(!lavc_param_wait_keyframe) avctx->flags2 |= CODEC_FLAG2_SHOW_ALL; #endif avctx->flags2|= lavc_param_fast; avctx->codec_tag= sh->format; avctx->stream_codec_tag= sh->video.fccHandler; avctx->idct_algo= lavc_param_idct_algo; avctx->error_concealment= lavc_param_error_concealment; avctx->debug= lavc_param_debug; if (lavc_param_debug) av_log_set_level(AV_LOG_DEBUG); avctx->debug_mv= lavc_param_vismv; avctx->skip_top = lavc_param_skip_top; avctx->skip_bottom= lavc_param_skip_bottom; if(lavc_param_lowres_str != NULL) { sscanf(lavc_param_lowres_str, "%d,%d", &lavc_param_lowres, &lowres_w); if(lavc_param_lowres < 1 || lavc_param_lowres > 16 || (lowres_w > 0 && avctx->width < lowres_w)) lavc_param_lowres = 0; avctx->lowres = lavc_param_lowres; } avctx->skip_loop_filter = str2AVDiscard(lavc_param_skip_loop_filter_str); avctx->skip_idct = str2AVDiscard(lavc_param_skip_idct_str); avctx->skip_frame = str2AVDiscard(lavc_param_skip_frame_str); if(lavc_avopt){ if (parse_avopts(avctx, lavc_avopt) < 0 && (!lavc_codec->priv_class || parse_avopts(avctx->priv_data, lavc_avopt) < 0)) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_avopt); uninit(sh); return 0; } } skip_idct = avctx->skip_idct; skip_frame = avctx->skip_frame; mp_dbg(MSGT_DECVIDEO, MSGL_DBG2, "libavcodec.size: %d x %d\n", avctx->width, avctx->height); switch (sh->format) { case mmioFOURCC('S','V','Q','3'): /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or in the phony AVI header if demux_lavf is used. The first case is handled here; the second case falls through to the next section. */ if (sh->ImageDesc) { avctx->extradata_size = (*(int *)sh->ImageDesc) - sizeof(int); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, ((int *)sh->ImageDesc)+1, avctx->extradata_size); break; } /* fallthrough */ case mmioFOURCC('A','V','R','n'): case mmioFOURCC('M','J','P','G'): /* AVRn stores huffman table in AVI header */ /* Pegasus MJPEG stores it also in AVI header, but it uses the common MJPG fourcc :( */ if (!sh->bih || sh->bih->biSize <= sizeof(*sh->bih)) break; av_dict_set(&opts, "extern_huff", "1", 0); avctx->extradata_size = sh->bih->biSize-sizeof(*sh->bih); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size); #if 0 { int x; uint8_t *p = avctx->extradata; for (x=0; x<avctx->extradata_size; x++) mp_msg(MSGT_DECVIDEO, MSGL_INFO, "[%x] ", p[x]); mp_msg(MSGT_DECVIDEO, MSGL_INFO, "\n"); } #endif break; case mmioFOURCC('R', 'V', '1', '0'): case mmioFOURCC('R', 'V', '1', '3'): case mmioFOURCC('R', 'V', '2', '0'): case mmioFOURCC('R', 'V', '3', '0'): case mmioFOURCC('R', 'V', '4', '0'): if(sh->bih->biSize<sizeof(*sh->bih)+8){ /* only 1 packet per frame & sub_id from fourcc */ avctx->extradata_size= 8; avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); ((uint32_t *)avctx->extradata)[0] = 0; ((uint32_t *)avctx->extradata)[1] = (sh->format == mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000; } else { /* has extra slice header (demux_rm or rm->avi streamcopy) */ avctx->extradata_size = sh->bih->biSize-sizeof(*sh->bih); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size); } avctx->sub_id= AV_RB32(avctx->extradata+4); // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]); break; default: if (!sh->bih || sh->bih->biSize <= sizeof(*sh->bih)) break; avctx->extradata_size = sh->bih->biSize-sizeof(*sh->bih); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size); break; } if(sh->bih) avctx->bits_per_coded_sample= sh->bih->biBitCount; avctx->thread_count = lavc_param_threads; avctx->thread_type = FF_THREAD_FRAME | FF_THREAD_SLICE; if(lavc_codec->capabilities & CODEC_CAP_HWACCEL) // HACK around badly placed checks in mpeg_mc_decode_init set_format_params(avctx, PIX_FMT_XVMC_MPEG2_IDCT); /* open it */ if (avcodec_open2(avctx, lavc_codec, &opts) < 0) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantOpenCodec); uninit(sh); return 0; } av_dict_free(&opts); // this is necessary in case get_format was never called and init_vo is // too late e.g. for H.264 VDPAU set_format_params(avctx, avctx->pix_fmt); mp_msg(MSGT_DECVIDEO, MSGL_V, "INFO: libavcodec init OK!\n"); return 1; //mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h, IMGFMT_YV12); }
static bool recreate_graph(struct af_instance *af, struct mp_audio *config) { void *tmp = talloc_new(NULL); struct priv *p = af->priv; AVFilterContext *in = NULL, *out = NULL; int r; if (bstr0(p->cfg_graph).len == 0) { mp_msg(MSGT_AFILTER, MSGL_FATAL, "lavfi: no filter graph set\n"); return false; } destroy_graph(af); mp_msg(MSGT_AFILTER, MSGL_V, "lavfi: create graph: '%s'\n", p->cfg_graph); AVFilterGraph *graph = avfilter_graph_alloc(); if (!graph) goto error; if (parse_avopts(graph, p->cfg_avopts) < 0) { mp_msg(MSGT_VFILTER, MSGL_FATAL, "lavfi: could not set opts: '%s'\n", p->cfg_avopts); goto error; } AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); if (!outputs || !inputs) goto error; char *src_args = talloc_asprintf(tmp, "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d:" "channel_layout=0x%"PRIx64, config->rate, av_get_sample_fmt_name(af_to_avformat(config->format)), config->channels.num, 1, config->rate, mp_chmap_to_lavc(&config->channels)); if (avfilter_graph_create_filter(&in, avfilter_get_by_name("abuffer"), "src", src_args, NULL, graph) < 0) goto error; if (avfilter_graph_create_filter(&out, avfilter_get_by_name("abuffersink"), "out", NULL, NULL, graph) < 0) goto error; static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE }; r = av_opt_set_int_list(out, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN); if (r < 0) goto error; r = av_opt_set_int(out, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN); if (r < 0) goto error; outputs->name = av_strdup("in"); outputs->filter_ctx = in; inputs->name = av_strdup("out"); inputs->filter_ctx = out; if (graph_parse(graph, p->cfg_graph, inputs, outputs, NULL) < 0) goto error; if (avfilter_graph_config(graph, NULL) < 0) goto error; p->in = in; p->out = out; p->graph = graph; assert(out->nb_inputs == 1); assert(in->nb_outputs == 1); talloc_free(tmp); return true; error: mp_msg(MSGT_AFILTER, MSGL_FATAL, "Can't configure libavfilter graph.\n"); avfilter_graph_free(&graph); talloc_free(tmp); return false; }
static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ AVFormatContext *avfc; AVFormatParameters ap; const AVOption *opt; AVMetadataTag *t = NULL; lavf_priv_t *priv= demuxer->priv; int i; //start_time = 0.0; char mp_filename[256]="mp:"; memset(&ap, 0, sizeof(AVFormatParameters)); //demux_lavf_find_geodata(demuxer); stream_seek(demuxer->stream, 0); int filepos=stream_tell(demuxer->stream); avfc = avformat_alloc_context(); if (opt_cryptokey) parse_cryptokey(avfc, opt_cryptokey); if (user_correct_pts != 0) avfc->flags |= AVFMT_FLAG_GENPTS; /* if (index_mode == 0) */ /* avfc->flags |= AVFMT_FLAG_IGNIDX; */ ap.prealloced_context = 1; #if 0 if(opt_probesize) { opt = av_set_int(avfc, "probesize", opt_probesize); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize); } if(opt_analyzeduration) { opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE); if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration); } if(opt_avopt){ if(parse_avopts(avfc, opt_avopt) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", opt_avopt); return NULL; } } #endif if(demuxer->stream->url) { if (!strncmp(demuxer->stream->url, "ffmpeg://rtsp:", 14)) av_strlcpy(mp_filename, demuxer->stream->url + 9, sizeof(mp_filename)); else av_strlcat(mp_filename, demuxer->stream->url, sizeof(mp_filename)); } else av_strlcat(mp_filename, "foobar.dummy", sizeof(mp_filename)); priv->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 0, demuxer, mp_read, NULL, mp_seek); priv->pb->read_seek = mp_read_seek; priv->pb->is_streamed = !demuxer->stream->end_pos || (demuxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK; filepos=stream_tell(demuxer->stream); if(av_open_input_stream(&avfc, priv->pb, mp_filename, priv->avif, &ap)<0){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n"); return NULL; } filepos=stream_tell(demuxer->stream); priv->avfc= avfc; if(av_find_stream_info(avfc) < 0){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n"); return NULL; } filepos=stream_tell(demuxer->stream); if(!strncmp(avfc->iformat->name,"aac",4)) get_aac_duration(demuxer,avfc); if(!strncmp(avfc->iformat->name,"mp3",4)) priv->avfc->duration = get_mp3_duration(demuxer) * AV_TIME_BASE; /* Add metadata. */ av_metadata_conv(avfc, NULL, avfc->iformat->metadata_conv); while((t = av_metadata_get(avfc->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))){ demux_info_add(demuxer, t->key, t->value); } for(i=0; i < avfc->nb_chapters; i++) { AVChapter *c = avfc->chapters[i]; uint64_t start = av_rescale_q(c->start, c->time_base, (AVRational){1,1000}); uint64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,1000}); t = av_metadata_get(c->metadata, "title", NULL, 0); demuxer_add_chapter(demuxer, t ? t->value : NULL, start, end); } demuxer->ttaflag = 0; for(i=0; i<avfc->nb_streams; i++) handle_stream(demuxer, avfc, i); if(demuxer->matroflag && !demuxer->ttaflag) return NULL; if(avfc->nb_programs) { int p; for (p = 0; p < avfc->nb_programs; p++) { AVProgram *program = avfc->programs[p]; t = av_metadata_get(program->metadata, "title", NULL, 0); mp_msg(MSGT_HEADER,MSGL_INFO,"LAVF: Program %d %s\n", program->id, t ? t->value : ""); } } mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams); mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD); if(!priv->audio_streams) demuxer->audio->id=-2; // nosound // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio; if(!priv->video_streams){ if(!priv->audio_streams){ mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n"); return NULL; } demuxer->video->id=-2; // audio-only } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video; filepos=stream_tell(demuxer->stream); int j=0; AVCodecContext *codec; AVCodec *avc; demuxer->audio_info = malloc(100*avfc->nb_streams); for(i=0; i<avfc->nb_streams; i++){ if(demuxer->a_streams[i]){ char *info = malloc(100); codec= ((AVStream *)avfc->streams[i])->codec; avc = avcodec_find_decoder(codec->codec_id); char *cn = avc ? avc->name : "unknown"; char *codec_name = malloc(100); strncpy(codec_name,cn,100); if((codec_name[0]=='d')&&(codec_name[1]=='c')&&(codec_name[2]=='a')) strncpy(codec_name,"dts",100); int a; for(a=0;codec_name[a];a++) if(codec_name[a]>='a'&&codec_name[a]<='z') codec_name[a]-=32; sprintf(info, "%s(%dHz %dCh)--%d", codec_name,codec->sample_rate, codec->channels, i); free(codec_name); memcpy(demuxer->audio_info+j*100,info, 100); j++; free(info); info = NULL; } } j = 0; demuxer->sub_info = malloc(100*avfc->nb_streams); for(i=0; i<avfc->nb_streams; i++){ if(demuxer->s_streams[i]){ char *info = malloc(100); AVStream *st= avfc->streams[i]; AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0); codec= st->codec; if (lang && lang->value) strcpy(info, lang->value); else strcpy(info, "unknown"); sprintf(info, "%s--%d", info,i); memcpy(demuxer->sub_info+j*100,info, 100); j++; free(info); info = NULL; } } if(AV_NOPTS_VALUE == priv->avfc->start_time){ LOGW("priv->avfc->start_time = AV_NOPTS_VALUE"); demuxer->start_time = 0; }else{ demuxer->start_time = priv->avfc->start_time; } return demuxer; }
// init driver static int init(sh_video_t *sh){ AVCodecContext *avctx; vd_ffmpeg_ctx *ctx; AVCodec *lavc_codec; int lowres_w=0; AVDictionary *opts = NULL; init_avcodec(); ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx)); if (!ctx) return 0; memset(ctx, 0, sizeof(vd_ffmpeg_ctx)); lavc_codec = avcodec_find_decoder_by_name(sh->codec->dll); if(!lavc_codec){ mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_MissingLAVCcodec, sh->codec->dll); uninit(sh); return 0; } ctx->ip_count= ctx->b_count= 0; ctx->pic = avcodec_alloc_frame(); ctx->avctx = avcodec_alloc_context3(lavc_codec); avctx = ctx->avctx; avctx->opaque = sh; avctx->codec_id = lavc_codec->id; avctx->get_format = get_format; avctx->flags|= lavc_param_bitexact; avctx->coded_width = sh->disp_w; avctx->coded_height= sh->disp_h; avctx->workaround_bugs= lavc_param_workaround_bugs; switch (lavc_param_error_resilience) { case 5: avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL; break; case 4: case 3: avctx->err_recognition |= AV_EF_AGGRESSIVE; // Fallthrough case 2: avctx->err_recognition |= AV_EF_COMPLIANT; // Fallthrough case 1: avctx->err_recognition |= AV_EF_CAREFUL; } lavc_param_gray|= CODEC_FLAG_GRAY; #ifdef CODEC_FLAG2_SHOW_ALL if(!lavc_param_wait_keyframe) avctx->flags2 |= CODEC_FLAG2_SHOW_ALL; #endif avctx->flags2|= lavc_param_fast; avctx->codec_tag= sh->format; avctx->stream_codec_tag= sh->video.fccHandler; avctx->idct_algo= lavc_param_idct_algo; avctx->error_concealment= lavc_param_error_concealment; avctx->debug= lavc_param_debug; if (lavc_param_debug) av_log_set_level(AV_LOG_DEBUG); avctx->debug_mv= lavc_param_vismv; avctx->skip_top = lavc_param_skip_top; avctx->skip_bottom= lavc_param_skip_bottom; if(lavc_param_lowres_str != NULL) { sscanf(lavc_param_lowres_str, "%d,%d", &lavc_param_lowres, &lowres_w); if(lavc_param_lowres < 1 || lavc_param_lowres > 16 || (lowres_w > 0 && avctx->width < lowres_w)) lavc_param_lowres = 0; avctx->lowres = lavc_param_lowres; } avctx->skip_loop_filter = str2AVDiscard(lavc_param_skip_loop_filter_str); avctx->skip_idct = str2AVDiscard(lavc_param_skip_idct_str); avctx->skip_frame = str2AVDiscard(lavc_param_skip_frame_str); av_opt_set_int(avctx, "skip_alpha", 1, 0); if(lavc_avopt){ if (parse_avopts(avctx, lavc_avopt) < 0 && (!lavc_codec->priv_class || parse_avopts(avctx->priv_data, lavc_avopt) < 0)) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_avopt); uninit(sh); return 0; } } skip_idct = avctx->skip_idct; skip_frame = avctx->skip_frame; mp_dbg(MSGT_DECVIDEO, MSGL_DBG2, "libavcodec.size: %d x %d\n", avctx->width, avctx->height); switch (sh->format) { case mmioFOURCC('S','V','Q','3'): /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or in the phony AVI header if demux_lavf is used. The first case is handled here; the second case falls through to the next section. */ if (sh->ImageDesc) { avctx->extradata_size = (*(int *)sh->ImageDesc) - sizeof(int); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, ((int *)sh->ImageDesc)+1, avctx->extradata_size); break; } /* fallthrough */ case mmioFOURCC('A','V','R','n'): case mmioFOURCC('M','J','P','G'): /* AVRn stores huffman table in AVI header */ /* Pegasus MJPEG stores it also in AVI header, but it uses the common MJPG fourcc :( */ if (!sh->bih || sh->bih->biSize <= sizeof(*sh->bih)) break; av_dict_set(&opts, "extern_huff", "1", 0); avctx->extradata_size = sh->bih->biSize-sizeof(*sh->bih); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size); #if 0 { int x; uint8_t *p = avctx->extradata; for (x=0; x<avctx->extradata_size; x++) mp_msg(MSGT_DECVIDEO, MSGL_INFO, "[%x] ", p[x]); mp_msg(MSGT_DECVIDEO, MSGL_INFO, "\n"); } #endif break; case mmioFOURCC('R', 'V', '1', '0'): case mmioFOURCC('R', 'V', '1', '3'): case mmioFOURCC('R', 'V', '2', '0'): case mmioFOURCC('R', 'V', '3', '0'): case mmioFOURCC('R', 'V', '4', '0'): if(sh->bih->biSize<sizeof(*sh->bih)+8){ /* only 1 packet per frame & sub_id from fourcc */ avctx->extradata_size= 8; avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); ((uint32_t *)avctx->extradata)[0] = 0; ((uint32_t *)avctx->extradata)[1] = (sh->format == mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000; } else { /* has extra slice header (demux_rm or rm->avi streamcopy) */ avctx->extradata_size = sh->bih->biSize-sizeof(*sh->bih); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size); } // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]); break; default: if (!sh->bih || sh->bih->biSize <= sizeof(*sh->bih)) break; avctx->extradata_size = sh->bih->biSize-sizeof(*sh->bih); avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size); break; } if(sh->bih) avctx->bits_per_coded_sample= sh->bih->biBitCount; set_dr_slice_settings(avctx, lavc_codec); if(lavc_codec->capabilities & CODEC_CAP_HWACCEL) // HACK around badly placed checks in mpeg_mc_decode_init set_format_params(avctx, PIX_FMT_XVMC_MPEG2_IDCT); avctx->thread_count = lavc_param_threads; avctx->thread_type = FF_THREAD_FRAME | FF_THREAD_SLICE; /* open it */ if (avcodec_open2(avctx, lavc_codec, &opts) < 0) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantOpenCodec); uninit(sh); return 0; } av_dict_free(&opts); // this is necessary in case get_format was never called and init_vo is // too late e.g. for H.264 VDPAU set_format_params(avctx, avctx->pix_fmt); mp_msg(MSGT_DECVIDEO, MSGL_V, "INFO: libavcodec init OK!\n"); return 1; //mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h, IMGFMT_YV12); }