Browse code

Merge commit 'ab7b038906f3e40ed474676d8e3029902a2078f5'

* commit 'ab7b038906f3e40ed474676d8e3029902a2078f5':
avconv: factor out the output stream initialization

Conflicts:
ffmpeg.c

Merged-by: Michael Niedermayer <michael@niedermayer.cc>

Michael Niedermayer authored on 2015/07/19 20:50:33
Showing 1 changed files
... ...
@@ -2549,6 +2549,71 @@ static int compare_int64(const void *a, const void *b)
2549 2549
     return va < vb ? -1 : va > vb ? +1 : 0;
2550 2550
 }
2551 2551
 
2552
+static int init_output_stream(OutputStream *ost, char *error, int error_len)
2553
+{
2554
+    int ret = 0;
2555
+
2556
+    if (ost->encoding_needed) {
2557
+        AVCodec      *codec = ost->enc;
2558
+        AVCodecContext *dec = NULL;
2559
+        InputStream *ist;
2560
+
2561
+        if ((ist = get_input_stream(ost)))
2562
+            dec = ist->dec_ctx;
2563
+        if (dec && dec->subtitle_header) {
2564
+            /* ASS code assumes this buffer is null terminated so add extra byte. */
2565
+            ost->enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
2566
+            if (!ost->enc_ctx->subtitle_header)
2567
+                return AVERROR(ENOMEM);
2568
+            memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
2569
+            ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
2570
+        }
2571
+        if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
2572
+            av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
2573
+        av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
2574
+
2575
+        if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
2576
+            if (ret == AVERROR_EXPERIMENTAL)
2577
+                abort_codec_experimental(codec, 1);
2578
+            snprintf(error, error_len,
2579
+                     "Error while opening encoder for output stream #%d:%d - "
2580
+                     "maybe incorrect parameters such as bit_rate, rate, width or height",
2581
+                    ost->file_index, ost->index);
2582
+            return ret;
2583
+        }
2584
+        if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
2585
+            !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
2586
+            av_buffersink_set_frame_size(ost->filter->filter,
2587
+                                            ost->enc_ctx->frame_size);
2588
+        assert_avoptions(ost->encoder_opts);
2589
+        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
2590
+            av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
2591
+                                         " It takes bits/s as argument, not kbits/s\n");
2592
+
2593
+        ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
2594
+        if (ret < 0) {
2595
+            av_log(NULL, AV_LOG_FATAL,
2596
+                   "Error initializing the output stream codec context.\n");
2597
+            exit_program(1);
2598
+        }
2599
+
2600
+        // copy timebase while removing common factors
2601
+        ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
2602
+        ost->st->codec->codec= ost->enc_ctx->codec;
2603
+    } else {
2604
+        ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
2605
+        if (ret < 0) {
2606
+           av_log(NULL, AV_LOG_FATAL,
2607
+                  "Error setting up codec context options.\n");
2608
+           return ret;
2609
+        }
2610
+        // copy timebase while removing common factors
2611
+        ost->st->time_base = av_add_q(ost->st->codec->time_base, (AVRational){0, 1});
2612
+    }
2613
+
2614
+    return ret;
2615
+}
2616
+
2552 2617
 static void parse_forced_key_frames(char *kf, OutputStream *ost,
2553 2618
                                     AVCodecContext *avctx)
2554 2619
 {
... ...
@@ -3087,63 +3152,9 @@ static int transcode_init(void)
3087 3087
 
3088 3088
     /* open each encoder */
3089 3089
     for (i = 0; i < nb_output_streams; i++) {
3090
-        ost = output_streams[i];
3091
-        if (ost->encoding_needed) {
3092
-            AVCodec      *codec = ost->enc;
3093
-            AVCodecContext *dec = NULL;
3094
-
3095
-            if ((ist = get_input_stream(ost)))
3096
-                dec = ist->dec_ctx;
3097
-            if (dec && dec->subtitle_header) {
3098
-                /* ASS code assumes this buffer is null terminated so add extra byte. */
3099
-                ost->enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
3100
-                if (!ost->enc_ctx->subtitle_header) {
3101
-                    ret = AVERROR(ENOMEM);
3102
-                    goto dump_format;
3103
-                }
3104
-                memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
3105
-                ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
3106
-            }
3107
-            if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
3108
-                av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
3109
-            av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
3110
-
3111
-            if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
3112
-                if (ret == AVERROR_EXPERIMENTAL)
3113
-                    abort_codec_experimental(codec, 1);
3114
-                snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
3115
-                        ost->file_index, ost->index);
3116
-                goto dump_format;
3117
-            }
3118
-            if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
3119
-                !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
3120
-                av_buffersink_set_frame_size(ost->filter->filter,
3121
-                                             ost->enc_ctx->frame_size);
3122
-            assert_avoptions(ost->encoder_opts);
3123
-            if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
3124
-                av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
3125
-                                             " It takes bits/s as argument, not kbits/s\n");
3126
-
3127
-            ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
3128
-            if (ret < 0) {
3129
-                av_log(NULL, AV_LOG_FATAL,
3130
-                       "Error initializing the output stream codec context.\n");
3131
-                exit_program(1);
3132
-            }
3133
-
3134
-            // copy timebase while removing common factors
3135
-            ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
3136
-            ost->st->codec->codec= ost->enc_ctx->codec;
3137
-        } else {
3138
-            ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
3139
-            if (ret < 0) {
3140
-                av_log(NULL, AV_LOG_FATAL,
3141
-                    "Error setting up codec context options.\n");
3142
-                return ret;
3143
-            }
3144
-            // copy timebase while removing common factors
3145
-            ost->st->time_base = av_add_q(ost->st->codec->time_base, (AVRational){0, 1});
3146
-        }
3090
+        ret = init_output_stream(output_streams[i], error, sizeof(error));
3091
+        if (ret < 0)
3092
+            goto dump_format;
3147 3093
     }
3148 3094
 
3149 3095
     /* init input streams */