Skip to content
Snippets Groups Projects
avconv.c 152 KiB
Newer Older
  • Learn to ignore specific revisions
  •                     codec->bits_per_raw_sample= 0;
                    }
    
                    ost->resample_height = icodec->height;
                    ost->resample_width  = icodec->width;
                    ost->resample_pix_fmt= icodec->pix_fmt;
                    ost->encoding_needed = 1;
                    ist->decoding_needed = 1;
    
                    if (!ost->frame_rate.num)
                        ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25,1};
                    if (ost->enc && ost->enc->supported_framerates && !force_fps) {
                        int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
                        ost->frame_rate = ost->enc->supported_framerates[idx];
                    }
                    codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num};
    
    #if CONFIG_AVFILTER
                    if (configure_video_filters(ist, ost)) {
                        fprintf(stderr, "Error opening filters!\n");
                        exit(1);
                    }
    #endif
                    break;
                case AVMEDIA_TYPE_SUBTITLE:
                    ost->encoding_needed = 1;
                    ist->decoding_needed = 1;
                    break;
                default:
                    abort();
                    break;
                }
                /* two pass mode */
                if (ost->encoding_needed &&
                    (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
                    char logfilename[1024];
                    FILE *f;
    
                    snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
                             pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
                             i);
                    if (codec->flags & CODEC_FLAG_PASS1) {
                        f = fopen(logfilename, "wb");
                        if (!f) {
                            fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno));
                            exit_program(1);
                        }
                        ost->logfile = f;
                    } else {
                        char  *logbuffer;
                        size_t logbuffer_size;
                        if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
                            fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
                            exit_program(1);
                        }
                        codec->stats_in = logbuffer;
                    }
                }
            }
            if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
                int size= codec->width * codec->height;
                bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200);
            }
        }
    
        if (!bit_buffer)
            bit_buffer = av_malloc(bit_buffer_size);
        if (!bit_buffer) {
            fprintf(stderr, "Cannot allocate %d bytes output buffer\n",
                    bit_buffer_size);
            ret = AVERROR(ENOMEM);
            goto fail;
        }
    
        /* open each encoder */
        for(i=0;i<nb_ostreams;i++) {
            ost = ost_table[i];
            if (ost->encoding_needed) {
                AVCodec *codec = ost->enc;
                AVCodecContext *dec = input_streams[ost->source_index].st->codec;
                if (!codec) {
                    snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d",
                             ost->st->codec->codec_id, ost->file_index, ost->index);
                    ret = AVERROR(EINVAL);
                    goto dump_format;
                }
                if (dec->subtitle_header) {
                    ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
                    if (!ost->st->codec->subtitle_header) {
                        ret = AVERROR(ENOMEM);
                        goto dump_format;
                    }
                    memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
                    ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
                }
                if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
                    snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
                            ost->file_index, ost->index);
                    ret = AVERROR(EINVAL);
                    goto dump_format;
                }
                assert_codec_experimental(ost->st->codec, 1);
                assert_avoptions(ost->opts);
                if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
                    av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
                                                 "It takes bits/s as argument, not kbits/s\n");
                extra_size += ost->st->codec->extradata_size;
            }
        }
    
        /* open each decoder */
        for (i = 0; i < nb_input_streams; i++) {
            ist = &input_streams[i];
            if (ist->decoding_needed) {
                AVCodec *codec = ist->dec;
                if (!codec)
                    codec = avcodec_find_decoder(ist->st->codec->codec_id);
                if (!codec) {
                    snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d",
                            ist->st->codec->codec_id, ist->file_index, ist->st->index);
                    ret = AVERROR(EINVAL);
                    goto dump_format;
                }
    
                /* update requested sample format for the decoder based on the
                   corresponding encoder sample format */
                for (j = 0; j < nb_ostreams; j++) {
                    ost = ost_table[j];
                    if (ost->source_index == i) {
                        update_sample_fmt(ist->st->codec, codec, ost->st->codec);
                        break;
                    }
                }
    
                if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
                    snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d",
                            ist->file_index, ist->st->index);
                    ret = AVERROR(EINVAL);
                    goto dump_format;
                }
                assert_codec_experimental(ist->st->codec, 0);
                assert_avoptions(ost->opts);
            }
        }
    
        /* init pts */
        for (i = 0; i < nb_input_streams; i++) {
            AVStream *st;
            ist = &input_streams[i];
            st= ist->st;
            ist->pts = st->avg_frame_rate.num ? - st->codec->has_b_frames*AV_TIME_BASE / av_q2d(st->avg_frame_rate) : 0;
            ist->next_pts = AV_NOPTS_VALUE;
            init_pts_correction(&ist->pts_ctx);
            ist->is_start = 1;
        }
    
        /* open files and write file headers */
        for(i=0;i<nb_output_files;i++) {
            os = output_files[i];
            if (avformat_write_header(os, &output_opts[i]) < 0) {
                snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
                ret = AVERROR(EINVAL);
                goto dump_format;
            }
            assert_avoptions(output_opts[i]);
            if (strcmp(output_files[i]->oformat->name, "rtp")) {
                want_sdp = 0;
            }
        }
    
     dump_format:
        /* dump the file output parameters - cannot be done before in case
           of stream copy */
        for(i=0;i<nb_output_files;i++) {
            av_dump_format(output_files[i], i, output_files[i]->filename, 1);
        }
    
        /* dump the stream mapping */
        if (verbose >= 0) {
            fprintf(stderr, "Stream mapping:\n");
            for(i=0;i<nb_ostreams;i++) {
                ost = ost_table[i];
                fprintf(stderr, "  Stream #%d.%d -> #%d.%d",
                        input_streams[ost->source_index].file_index,
                        input_streams[ost->source_index].st->index,
                        ost->file_index,
                        ost->index);
                if (ost->sync_ist != &input_streams[ost->source_index])
                    fprintf(stderr, " [sync #%d.%d]",
                            ost->sync_ist->file_index,
                            ost->sync_ist->st->index);
    
                if (ost->st->stream_copy)
                    fprintf(stderr, " (copy)");
    
                fprintf(stderr, "\n");
            }
        }
    
        if (ret) {
            fprintf(stderr, "%s\n", error);
            goto fail;
        }
    
        if (want_sdp) {
            print_sdp(output_files, nb_output_files);
        }
    
        if (verbose >= 0)
            fprintf(stderr, "Press ctrl-c to stop encoding\n");
        term_init();
    
        timer_start = av_gettime();
    
        for(; received_sigterm == 0;) {
            int file_index, ist_index;
            AVPacket pkt;
    
            opts_min= 1e100;
    
            /* select the stream that we must read now by looking at the
               smallest output pts */
            file_index = -1;
            for(i=0;i<nb_ostreams;i++) {
    
                ost = ost_table[i];
                os = output_files[ost->file_index];
                ist = &input_streams[ost->source_index];
                if(ist->is_past_recording_time || no_packet[ist->file_index])
                    continue;
                    opts = ost->st->pts.val * av_q2d(ost->st->time_base);
    
    2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
                if (!input_files[ist->file_index].eof_reached){
                    if(ipts < ipts_min) {
                        ipts_min = ipts;
                        if(input_sync ) file_index = ist->file_index;
                    }
                    if(opts < opts_min) {
                        opts_min = opts;
                        if(!input_sync) file_index = ist->file_index;
                    }
                }
                if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
                    file_index= -1;
                    break;
                }
            }
            /* if none, if is finished */
            if (file_index < 0) {
                if(no_packet_count){
                    no_packet_count=0;
                    memset(no_packet, 0, sizeof(no_packet));
                    usleep(10000);
                    continue;
                }
                break;
            }
    
            /* finish if limit size exhausted */
            if (limit_filesize != 0 && limit_filesize <= avio_tell(output_files[0]->pb))
                break;
    
            /* read a frame from it and output it in the fifo */
            is = input_files[file_index].ctx;
            ret= av_read_frame(is, &pkt);
            if(ret == AVERROR(EAGAIN)){
                no_packet[file_index]=1;
                no_packet_count++;
                continue;
            }
            if (ret < 0) {
                input_files[file_index].eof_reached = 1;
                if (opt_shortest)
                    break;
                else
                    continue;
            }
    
            no_packet_count=0;
            memset(no_packet, 0, sizeof(no_packet));
    
            if (do_pkt_dump) {
                av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
                                 is->streams[pkt.stream_index]);
            }
            /* the following test is needed in case new streams appear
               dynamically in stream : we ignore them */
            if (pkt.stream_index >= input_files[file_index].ctx->nb_streams)
                goto discard_packet;
            ist_index = input_files[file_index].ist_index + pkt.stream_index;
            ist = &input_streams[ist_index];
            if (ist->discard)
                goto discard_packet;
    
            if (pkt.dts != AV_NOPTS_VALUE)
                pkt.dts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
            if (pkt.pts != AV_NOPTS_VALUE)
                pkt.pts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
    
            if (ist->ts_scale) {
                if(pkt.pts != AV_NOPTS_VALUE)
                    pkt.pts *= ist->ts_scale;
                if(pkt.dts != AV_NOPTS_VALUE)
                    pkt.dts *= ist->ts_scale;
            }
    
    //        fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", ist->next_pts, pkt.dts, input_files[ist->file_index].ts_offset, ist->st->codec->codec_type);
            if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE
                && (is->iformat->flags & AVFMT_TS_DISCONT)) {
                int64_t pkt_dts= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
                int64_t delta= pkt_dts - ist->next_pts;
                if((FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts+1<ist->pts)&& !copy_ts){
                    input_files[ist->file_index].ts_offset -= delta;
                    if (verbose > 2)
                        fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
                                delta, input_files[ist->file_index].ts_offset);
                    pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                    if(pkt.pts != AV_NOPTS_VALUE)
                        pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                }
            }
    
            /* finish if recording time exhausted */
            if (recording_time != INT64_MAX &&
                av_compare_ts(pkt.pts, ist->st->time_base, recording_time + start_time, (AVRational){1, 1000000}) >= 0) {
                ist->is_past_recording_time = 1;
                goto discard_packet;
            }
    
            //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size);
            if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
    
                if (verbose >= 0)
                    fprintf(stderr, "Error while decoding stream #%d.%d\n",
                            ist->file_index, ist->st->index);
                if (exit_on_error)
                    exit_program(1);
                av_free_packet(&pkt);
                goto redo;
            }
    
        discard_packet:
            av_free_packet(&pkt);
    
            /* dump report by using the output first video and audio streams */
            print_report(output_files, ost_table, nb_ostreams, 0);
        }
    
        /* at the end of stream, we must flush the decoder buffers */
        for (i = 0; i < nb_input_streams; i++) {
            ist = &input_streams[i];
            if (ist->decoding_needed) {
                output_packet(ist, i, ost_table, nb_ostreams, NULL);
            }
        }
    
        term_exit();
    
        /* write the trailer if needed and close file */
        for(i=0;i<nb_output_files;i++) {
            os = output_files[i];
            av_write_trailer(os);
        }
    
        /* dump report by using the first video and audio streams */
        print_report(output_files, ost_table, nb_ostreams, 1);
    
        /* close each encoder */
        for(i=0;i<nb_ostreams;i++) {
            ost = ost_table[i];
            if (ost->encoding_needed) {
                av_freep(&ost->st->codec->stats_in);
                avcodec_close(ost->st->codec);
            }
    #if CONFIG_AVFILTER
            avfilter_graph_free(&ost->graph);
    #endif
        }
    
        /* close each decoder */
        for (i = 0; i < nb_input_streams; i++) {
            ist = &input_streams[i];
            if (ist->decoding_needed) {
                avcodec_close(ist->st->codec);
            }
        }
    
        /* finished ! */
        ret = 0;
    
     fail:
        av_freep(&bit_buffer);
    
        if (ost_table) {
            for(i=0;i<nb_ostreams;i++) {
                ost = ost_table[i];
                if (ost) {
                    if (ost->st->stream_copy)
                        av_freep(&ost->st->codec->extradata);
                    if (ost->logfile) {
                        fclose(ost->logfile);
                        ost->logfile = NULL;
                    }
                    av_fifo_free(ost->fifo); /* works even if fifo is not
                                                 initialized but set to zero */
                    av_freep(&ost->st->codec->subtitle_header);
                    av_free(ost->pict_tmp.data[0]);
                    av_free(ost->forced_kf_pts);
                    if (ost->video_resample)
                        sws_freeContext(ost->img_resample_ctx);
                    if (ost->resample)
                        audio_resample_close(ost->resample);
                    if (ost->reformat_ctx)
                        av_audio_convert_free(ost->reformat_ctx);
                    av_dict_free(&ost->opts);
                    av_free(ost);
                }
            }
            av_free(ost_table);
        }
        return ret;
    }
    
    static int opt_format(const char *opt, const char *arg)
    {
        last_asked_format = arg;
        return 0;
    }
    
    static int opt_video_rc_override_string(const char *opt, const char *arg)
    {
        video_rc_override_string = arg;
        return 0;
    }
    
    static int opt_me_threshold(const char *opt, const char *arg)
    {
        me_threshold = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
        return 0;
    }
    
    static int opt_verbose(const char *opt, const char *arg)
    {
        verbose = parse_number_or_die(opt, arg, OPT_INT64, -10, 10);
        return 0;
    }
    
    static int opt_frame_rate(const char *opt, const char *arg)
    {
        if (av_parse_video_rate(&frame_rate, arg) < 0) {
            fprintf(stderr, "Incorrect value for %s: %s\n", opt, arg);
            exit_program(1);
        }
        return 0;
    }
    
    static int opt_frame_crop(const char *opt, const char *arg)
    {
        fprintf(stderr, "Option '%s' has been removed, use the crop filter instead\n", opt);
        return AVERROR(EINVAL);
    }
    
    static int opt_frame_size(const char *opt, const char *arg)
    {
        if (av_parse_video_size(&frame_width, &frame_height, arg) < 0) {
            fprintf(stderr, "Incorrect frame size\n");
            return AVERROR(EINVAL);
        }
        return 0;
    }
    
    static int opt_pad(const char *opt, const char *arg) {
        fprintf(stderr, "Option '%s' has been removed, use the pad filter instead\n", opt);
        return -1;
    }
    
    static int opt_frame_pix_fmt(const char *opt, const char *arg)
    {
        if (strcmp(arg, "list")) {
            frame_pix_fmt = av_get_pix_fmt(arg);
            if (frame_pix_fmt == PIX_FMT_NONE) {
                fprintf(stderr, "Unknown pixel format requested: %s\n", arg);
                return AVERROR(EINVAL);
            }
        } else {
            show_pix_fmts();
            exit_program(0);
        }
        return 0;
    }
    
    static int opt_frame_aspect_ratio(const char *opt, const char *arg)
    {
        int x = 0, y = 0;
        double ar = 0;
        const char *p;
        char *end;
    
        p = strchr(arg, ':');
        if (p) {
            x = strtol(arg, &end, 10);
            if (end == p)
                y = strtol(end+1, &end, 10);
            if (x > 0 && y > 0)
                ar = (double)x / (double)y;
        } else
            ar = strtod(arg, NULL);
    
        if (!ar) {
            fprintf(stderr, "Incorrect aspect ratio specification.\n");
            return AVERROR(EINVAL);
        }
        frame_aspect_ratio = ar;
        return 0;
    }
    
    static int opt_metadata(const char *opt, const char *arg)
    {
        char *mid= strchr(arg, '=');
    
        if(!mid){
            fprintf(stderr, "Missing =\n");
            exit_program(1);
        }
        *mid++= 0;
    
        av_dict_set(&metadata, arg, mid, 0);
    
        return 0;
    }
    
    static int opt_qscale(const char *opt, const char *arg)
    {
        video_qscale = parse_number_or_die(opt, arg, OPT_FLOAT, 0, 255);
        if (video_qscale == 0) {
            fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
            return AVERROR(EINVAL);
        }
        return 0;
    }
    
    static int opt_top_field_first(const char *opt, const char *arg)
    {
        top_field_first = parse_number_or_die(opt, arg, OPT_INT, 0, 1);
        return 0;
    }
    
    static int opt_thread_count(const char *opt, const char *arg)
    {
        thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
    #if !HAVE_THREADS
        if (verbose >= 0)
            fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
    #endif
        return 0;
    }
    
    static int opt_audio_sample_fmt(const char *opt, const char *arg)
    {
        if (strcmp(arg, "list")) {
            audio_sample_fmt = av_get_sample_fmt(arg);
            if (audio_sample_fmt == AV_SAMPLE_FMT_NONE) {
                av_log(NULL, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
                return AVERROR(EINVAL);
            }
        } else {
            int i;
            char fmt_str[128];
            for (i = -1; i < AV_SAMPLE_FMT_NB; i++)
                printf("%s\n", av_get_sample_fmt_string(fmt_str, sizeof(fmt_str), i));
            exit_program(0);
        }
        return 0;
    }
    
    static int opt_audio_rate(const char *opt, const char *arg)
    {
        audio_sample_rate = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
        return 0;
    }
    
    static int opt_audio_channels(const char *opt, const char *arg)
    {
        audio_channels = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
        return 0;
    }
    
    
    static int opt_codec(const char *opt, const char *arg)
    
        return av_dict_set(&codec_names, opt, arg, 0);
    
    }
    
    static int opt_audio_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:a", arg);
    
    }
    
    static int opt_video_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:v", arg);
    
    }
    
    static int opt_subtitle_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:s", arg);
    
    }
    
    static int opt_data_codec(const char *opt, const char *arg)
    {
    
        return opt_codec("codec:d", arg);
    
    }
    
    static int opt_codec_tag(const char *opt, const char *arg)
    {
        char *tail;
        uint32_t *codec_tag;
    
        codec_tag = !strcmp(opt, "atag") ? &audio_codec_tag :
                    !strcmp(opt, "vtag") ? &video_codec_tag :
                    !strcmp(opt, "stag") ? &subtitle_codec_tag : NULL;
        if (!codec_tag)
            return -1;
    
        *codec_tag = strtol(arg, &tail, 0);
        if (!tail || *tail)
            *codec_tag = AV_RL32(arg);
    
        return 0;
    }
    
    static int opt_map(const char *opt, const char *arg)
    {
    
        StreamMap *m = NULL;
        int i, negative = 0, file_idx;
        int sync_file_idx = -1, sync_stream_idx;
        char *p, *sync;
        char *map;
    
        if (*arg == '-') {
            negative = 1;
            arg++;
        }
        map = av_strdup(arg);
    
        /* parse sync stream first, just pick first matching stream */
        if (sync = strchr(map, ',')) {
            *sync = 0;
            sync_file_idx = strtol(sync + 1, &sync, 0);
            if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
                av_log(NULL, AV_LOG_ERROR, "Invalid sync file index: %d.\n", sync_file_idx);
                exit_program(1);
            }
            if (*sync)
                sync++;
            for (i = 0; i < input_files[sync_file_idx].ctx->nb_streams; i++)
                if (check_stream_specifier(input_files[sync_file_idx].ctx,
                                           input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
                    sync_stream_idx = i;
                    break;
                }
            if (i == input_files[sync_file_idx].ctx->nb_streams) {
                av_log(NULL, AV_LOG_ERROR, "Sync stream specification in map %s does not "
                                           "match any streams.\n", arg);
                exit_program(1);
            }
        }
    
    
        file_idx = strtol(map, &p, 0);
        if (file_idx >= nb_input_files || file_idx < 0) {
            av_log(NULL, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx);
            exit_program(1);
    
        if (negative)
            /* disable some already defined maps */
            for (i = 0; i < nb_stream_maps; i++) {
                m = &stream_maps[i];
                if (check_stream_specifier(input_files[m->file_index].ctx,
                                           input_files[m->file_index].ctx->streams[m->stream_index],
                                           *p == ':' ? p + 1 : p) > 0)
                    m->disabled = 1;
            }
        else
            for (i = 0; i < input_files[file_idx].ctx->nb_streams; i++) {
                if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i],
                            *p == ':' ? p + 1 : p) <= 0)
                    continue;
                stream_maps = grow_array(stream_maps, sizeof(*stream_maps), &nb_stream_maps, nb_stream_maps + 1);
                m = &stream_maps[nb_stream_maps - 1];
    
                m->file_index   = file_idx;
                m->stream_index = i;
    
                if (sync_file_idx >= 0) {
                    m->sync_file_index   = sync_file_idx;
                    m->sync_stream_index = sync_stream_idx;
                } else {
                    m->sync_file_index   = file_idx;
                    m->sync_stream_index = i;
                }
            }
    
        if (!m) {
            av_log(NULL, AV_LOG_ERROR, "Stream map '%s' matches no streams.\n", arg);
            exit_program(1);
        }
    
        av_freep(&map);
    
    static void parse_meta_type(char *arg, char *type, int *index)
    
            *type = *(++arg);
            switch (*arg) {
            case 'g':
                break;
            case 's':
            case 'c':
            case 'p':
    
                if (*(++arg) == ':')
                    *index = strtol(++arg, NULL, 0);
    
                break;
            default:
                fprintf(stderr, "Invalid metadata type %c.\n", *arg);
                exit_program(1);
            }
        } else
            *type = 'g';
    }
    
    static int opt_map_metadata(const char *opt, const char *arg)
    {
        MetadataMap *m, *m1;
        char *p;
    
        meta_data_maps = grow_array(meta_data_maps, sizeof(*meta_data_maps),
                                    &nb_meta_data_maps, nb_meta_data_maps + 1);
    
    
        m = &meta_data_maps[nb_meta_data_maps - 1][1];
    
        m->file = strtol(arg, &p, 0);
    
        parse_meta_type(p, &m->type, &m->index);
    
        m1 = &meta_data_maps[nb_meta_data_maps - 1][0];
        if (p = strchr(opt, ':'))
            parse_meta_type(p, &m1->type, &m1->index);
        else
            m1->type = 'g';
    
    
        if (m->type == 'g' || m1->type == 'g')
            metadata_global_autocopy = 0;
        if (m->type == 's' || m1->type == 's')
            metadata_streams_autocopy = 0;
        if (m->type == 'c' || m1->type == 'c')
            metadata_chapters_autocopy = 0;
    
        return 0;
    }
    
    static int opt_input_ts_scale(const char *opt, const char *arg)
    {
        unsigned int stream;
        double scale;
        char *p;
    
        stream = strtol(arg, &p, 0);
        if (*p)
            p++;
        scale= strtod(p, &p);
    
        ts_scale = grow_array(ts_scale, sizeof(*ts_scale), &nb_ts_scale, stream + 1);
        ts_scale[stream] = scale;
        return 0;
    }
    
    static int opt_recording_time(const char *opt, const char *arg)
    {
        recording_time = parse_time_or_die(opt, arg, 1);
        return 0;
    }
    
    static int opt_start_time(const char *opt, const char *arg)
    {
        start_time = parse_time_or_die(opt, arg, 1);
        return 0;
    }
    
    static int opt_input_ts_offset(const char *opt, const char *arg)
    {
        input_ts_offset = parse_time_or_die(opt, arg, 1);
        return 0;
    }
    
    
    static enum CodecID find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
    {
        const char *codec_string = encoder ? "encoder" : "decoder";
        AVCodec *codec;
    
        if(!name)
            return CODEC_ID_NONE;
        codec = encoder ?
            avcodec_find_encoder_by_name(name) :
            avcodec_find_decoder_by_name(name);
        if(!codec) {
            av_log(NULL, AV_LOG_ERROR, "Unknown %s '%s'\n", codec_string, name);
            exit_program(1);
        }
        if(codec->type != type) {
            av_log(NULL, AV_LOG_ERROR, "Invalid %s type '%s'\n", codec_string, name);
            exit_program(1);
        }
        return codec->id;
    }
    
    static AVCodec *choose_codec(AVFormatContext *s, AVStream *st, enum AVMediaType type, AVDictionary *codec_names)
    {
        AVDictionaryEntry *e = NULL;
        char *codec_name = NULL;
        int ret;
    
        while (e = av_dict_get(codec_names, "", e, AV_DICT_IGNORE_SUFFIX)) {
            char *p = strchr(e->key, ':');
    
            if ((ret = check_stream_specifier(s, st, p ? p + 1 : "")) > 0)
                codec_name = e->value;
            else if (ret < 0)
                exit_program(1);
        }
    
        if (!codec_name) {
    
            if (s->oformat) {
                st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename, NULL, type);
                return avcodec_find_encoder(st->codec->codec_id);
            }
    
        } else if (!strcmp(codec_name, "copy"))
            st->stream_copy = 1;
        else {
            st->codec->codec_id = find_codec_or_die(codec_name, type, s->iformat == NULL);
            return s->oformat ? avcodec_find_encoder_by_name(codec_name) :
                                avcodec_find_decoder_by_name(codec_name);
        }
    
        return NULL;
    }
    
    
    static int opt_input_file(const char *opt, const char *filename)
    {
        AVFormatContext *ic;
        AVInputFormat *file_iformat = NULL;
        int err, i, ret, rfps, rfps_base;
        int64_t timestamp;
        uint8_t buf[128];
        AVDictionary **opts;
        int orig_nb_streams;                     // number of streams before avformat_find_stream_info
    
        if (last_asked_format) {
            if (!(file_iformat = av_find_input_format(last_asked_format))) {
                fprintf(stderr, "Unknown input format: '%s'\n", last_asked_format);
                exit_program(1);
            }
            last_asked_format = NULL;
        }
    
        if (!strcmp(filename, "-"))
            filename = "pipe:";
    
        using_stdin |= !strncmp(filename, "pipe:", 5) ||
                        !strcmp(filename, "/dev/stdin");
    
        /* get default parameters from command line */
        ic = avformat_alloc_context();
        if (!ic) {
            print_error(filename, AVERROR(ENOMEM));
            exit_program(1);
        }
        if (audio_sample_rate) {
            snprintf(buf, sizeof(buf), "%d", audio_sample_rate);
            av_dict_set(&format_opts, "sample_rate", buf, 0);
        }
        if (audio_channels) {
            snprintf(buf, sizeof(buf), "%d", audio_channels);
            av_dict_set(&format_opts, "channels", buf, 0);
        }
        if (frame_rate.num) {
            snprintf(buf, sizeof(buf), "%d/%d", frame_rate.num, frame_rate.den);
            av_dict_set(&format_opts, "framerate", buf, 0);
        }
        if (frame_width && frame_height) {
            snprintf(buf, sizeof(buf), "%dx%d", frame_width, frame_height);
            av_dict_set(&format_opts, "video_size", buf, 0);
        }
        if (frame_pix_fmt != PIX_FMT_NONE)
            av_dict_set(&format_opts, "pixel_format", av_get_pix_fmt_name(frame_pix_fmt), 0);
    
        ic->flags |= AVFMT_FLAG_NONBLOCK;
    
        /* open the input file with generic libav function */
        err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
        if (err < 0) {
            print_error(filename, err);
            exit_program(1);
        }
        assert_avoptions(format_opts);
    
        if(opt_programid) {
            int i, j;
            int found=0;
            for(i=0; i<ic->nb_streams; i++){
                ic->streams[i]->discard= AVDISCARD_ALL;
            }
            for(i=0; i<ic->nb_programs; i++){
                AVProgram *p= ic->programs[i];
                if(p->id != opt_programid){
                    p->discard = AVDISCARD_ALL;
                }else{
                    found=1;
                    for(j=0; j<p->nb_stream_indexes; j++){
                        ic->streams[p->stream_index[j]]->discard= AVDISCARD_DEFAULT;
                    }
                }
            }
            if(!found){
                fprintf(stderr, "Specified program id not found\n");
                exit_program(1);
            }
            opt_programid=0;
        }
    
    
        /* apply forced codec ids */
        for (i = 0; i < ic->nb_streams; i++)
            choose_codec(ic, ic->streams[i], ic->streams[i]->codec->codec_type, codec_names);
    
    
        /* Set AVCodecContext options for avformat_find_stream_info */
        opts = setup_find_stream_info_opts(ic, codec_opts);
        orig_nb_streams = ic->nb_streams;
    
        /* If not enough info to get the stream parameters, we decode the
           first frames to get it. (used in mpeg case for example) */
        ret = avformat_find_stream_info(ic, opts);
        if (ret < 0 && verbose >= 0) {
            fprintf(stderr, "%s: could not find codec parameters\n", filename);
            av_close_input_file(ic);
            exit_program(1);
        }
    
        timestamp = start_time;
        /* add the stream start time */
        if (ic->start_time != AV_NOPTS_VALUE)
            timestamp += ic->start_time;
    
        /* if seeking requested, we execute it */
        if (start_time != 0) {
            ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
            if (ret < 0) {
                fprintf(stderr, "%s: could not seek to position %0.3f\n",
                        filename, (double)timestamp / AV_TIME_BASE);
            }
            /* reset seek info */
            start_time = 0;
        }
    
        /* update the current parameters so that they match the one of the input stream */
        for(i=0;i<ic->nb_streams;i++) {
            AVStream *st = ic->streams[i];
            AVCodecContext *dec = st->codec;
            InputStream *ist;
    
            dec->thread_count = thread_count;
    
            input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
            ist = &input_streams[nb_input_streams - 1];
            ist->st = st;
            ist->file_index = nb_input_files;
            ist->discard = 1;
    
            ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
    
    
            if (i < nb_ts_scale)
                ist->ts_scale = ts_scale[i];
    
    
            ist->dec = choose_codec(ic, st, dec->codec_type, codec_names);
    
    
            switch (dec->codec_type) {
            case AVMEDIA_TYPE_AUDIO:
                if(audio_disable)
                    st->discard= AVDISCARD_ALL;
                break;
            case AVMEDIA_TYPE_VIDEO:
                rfps      = ic->streams[i]->r_frame_rate.num;
                rfps_base = ic->streams[i]->r_frame_rate.den;
                if (dec->lowres) {
                    dec->flags |= CODEC_FLAG_EMU_EDGE;
                    dec->height >>= dec->lowres;
                    dec->width  >>= dec->lowres;
                }
                if(me_threshold)
                    dec->debug |= FF_DEBUG_MV;