]> git.sesse.net Git - ffmpeg/blobdiff - ffmpeg.c
fabs is better then abs for floats ...
[ffmpeg] / ffmpeg.c
index c5f73624b23d21010016fc33be3f0498318d32b3..348bab084df126f005332383e96c9bb467c5f49a 100644 (file)
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -213,8 +213,12 @@ static int rate_emu = 0;
 #ifdef CONFIG_BKTR
 static char *video_grab_format = "bktr";
 #else
+#ifdef CONFIG_VIDEO4LINUX2
+static char *video_grab_format = "video4linux2";
+#else
 static char *video_grab_format = "video4linux";
 #endif
+#endif
 static char *video_device = NULL;
 static char *grab_device = NULL;
 static int  video_channel = 0;
@@ -239,6 +243,7 @@ static int input_sync;
 static int limit_filesize = 0; //
 
 static int pgmyuv_compatibility_hack=0;
+static int dts_delta_threshold = 10;
 
 const char **opt_names=NULL;
 int opt_name_count=0;
@@ -262,15 +267,15 @@ typedef struct AVOutputStream {
     struct AVInputStream *sync_ist; /* input stream to sync against */
     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number
     /* video only */
-    int video_resample;      /* video_resample and video_crop are mutually exclusive */
+    int video_resample;
     AVFrame pict_tmp;      /* temporary image for resampling */
     ImgReSampleContext *img_resample_ctx; /* for image resampling */
 
-    int video_crop;          /* video_resample and video_crop are mutually exclusive */
+    int video_crop;
     int topBand;             /* cropping area sizes */
     int leftBand;
 
-    int video_pad;           /* video_resample and video_pad are mutually exclusive */
+    int video_pad;
     int padtop;              /* padding area sizes */
     int padbottom;
     int padleft;
@@ -621,13 +626,7 @@ static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void
                 picture2 = picture;
             }
         } else {
-            if (img_convert(picture2, dec->pix_fmt, picture,
-                            dec->pix_fmt, dec->width, dec->height) < 0) {
-                /* if error, do not copy */
-                av_free(buf);
-                buf = NULL;
-                picture2 = picture;
-            }
+            img_copy(picture2, picture, dec->pix_fmt, dec->width, dec->height);
         }
     } else {
         picture2 = picture;
@@ -643,39 +642,6 @@ static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void
 /* we begin to correct av delay at this threshold */
 #define AV_DELAY_MAX 0.100
 
-
-/* Expects img to be yuv420 */
-static void fill_pad_region(AVPicture* img, int height, int width,
-        int padtop, int padbottom, int padleft, int padright, int *color) {
-
-    int i, y, shift;
-    uint8_t *optr;
-
-    for (i = 0; i < 3; i++) {
-        shift = (i == 0) ? 0 : 1;
-
-        if (padtop || padleft) {
-            memset(img->data[i], color[i], (((img->linesize[i] * padtop) +
-                            padleft) >> shift));
-        }
-
-        if (padleft || padright) {
-            optr = img->data[i] + (img->linesize[i] * (padtop >> shift)) +
-                (img->linesize[i] - (padright >> shift));
-
-            for (y = 0; y < ((height - (padtop + padbottom) - 1) >> shift); y++) {
-                memset(optr, color[i], (padleft + padright) >> shift);
-                optr += img->linesize[i];
-            }
-        }
-
-        if (padbottom || padright) {
-            optr = img->data[i] + (((img->linesize[i] * (height - padbottom)) - padright) >> shift);
-            memset(optr, color[i], (((img->linesize[i] * padbottom) + padright) >> shift));
-        }
-    }
-}
-
 static void do_subtitle_out(AVFormatContext *s,
                             AVOutputStream *ost,
                             AVInputStream *ist,
@@ -738,14 +704,15 @@ static void do_video_out(AVFormatContext *s,
                          int *frame_size)
 {
     int nb_frames, i, ret;
-    AVFrame *final_picture, *formatted_picture;
-    AVFrame picture_format_temp, picture_crop_temp;
+    AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;
+    AVFrame picture_format_temp, picture_crop_temp, picture_pad_temp;
     uint8_t *buf = NULL, *buf1 = NULL;
     AVCodecContext *enc, *dec;
     enum PixelFormat target_pixfmt;
 
     avcodec_get_frame_defaults(&picture_format_temp);
     avcodec_get_frame_defaults(&picture_crop_temp);
+    avcodec_get_frame_defaults(&picture_pad_temp);
 
     enc = ost->st->codec;
     dec = ist->st->codec;
@@ -781,8 +748,7 @@ static void do_video_out(AVFormatContext *s,
         return;
 
     /* convert pixel format if needed */
-    target_pixfmt = ost->video_resample || ost->video_pad
-        ? PIX_FMT_YUV420P : enc->pix_fmt;
+    target_pixfmt = ost->video_resample ? PIX_FMT_YUV420P : enc->pix_fmt;
     if (dec->pix_fmt != target_pixfmt) {
         int size;
 
@@ -807,108 +773,66 @@ static void do_video_out(AVFormatContext *s,
         formatted_picture = in_picture;
     }
 
-    /* XXX: resampling could be done before raw format conversion in
-       some cases to go faster */
-    /* XXX: only works for YUV420P */
-    if (ost->video_resample) {
-        final_picture = &ost->pict_tmp;
-        img_resample(ost->img_resample_ctx, (AVPicture*)final_picture, (AVPicture*)formatted_picture);
-
-        if (ost->padtop || ost->padbottom || ost->padleft || ost->padright) {
-            fill_pad_region((AVPicture*)final_picture, enc->height, enc->width,
-                    ost->padtop, ost->padbottom, ost->padleft, ost->padright,
-                    padcolor);
+    if (ost->video_crop) {
+        if (img_crop((AVPicture *)&picture_crop_temp, (AVPicture *)formatted_picture, target_pixfmt, ost->topBand, ost->leftBand) < 0) {
+            av_log(NULL, AV_LOG_ERROR, "error cropping picture\n");
+            goto the_end;
         }
+        formatted_picture = &picture_crop_temp;
+    }
 
-        if (enc->pix_fmt != PIX_FMT_YUV420P) {
-            int size;
-
-            av_free(buf);
-            /* create temporary picture */
-            size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
-            buf = av_malloc(size);
-            if (!buf)
-                return;
-            final_picture = &picture_format_temp;
-            avpicture_fill((AVPicture*)final_picture, buf, enc->pix_fmt, enc->width, enc->height);
-
-            if (img_convert((AVPicture*)final_picture, enc->pix_fmt,
-                            (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
-                            enc->width, enc->height) < 0) {
-
-                if (verbose >= 0)
-                    fprintf(stderr, "pixel format conversion not handled\n");
-
-                goto the_end;
-            }
-        }
-    } else if (ost->video_crop) {
-        picture_crop_temp.data[0] = formatted_picture->data[0] +
-                (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
-
-        picture_crop_temp.data[1] = formatted_picture->data[1] +
-                ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
-                (ost->leftBand >> 1);
-
-        picture_crop_temp.data[2] = formatted_picture->data[2] +
-                ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
-                (ost->leftBand >> 1);
-
-        picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
-        picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
-        picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
-        final_picture = &picture_crop_temp;
-    } else if (ost->video_pad) {
+    final_picture = formatted_picture;
+    padding_src = formatted_picture;
+    resampling_dst = &ost->pict_tmp;
+    if (ost->video_pad) {
         final_picture = &ost->pict_tmp;
-
-        for (i = 0; i < 3; i++) {
-            uint8_t *optr, *iptr;
-            int shift = (i == 0) ? 0 : 1;
-            int y, yheight;
-
-            /* set offset to start writing image into */
-            optr = final_picture->data[i] + (((final_picture->linesize[i] *
-                            ost->padtop) + ost->padleft) >> shift);
-            iptr = formatted_picture->data[i];
-
-            yheight = (enc->height - ost->padtop - ost->padbottom) >> shift;
-            for (y = 0; y < yheight; y++) {
-                /* copy unpadded image row into padded image row */
-                memcpy(optr, iptr, formatted_picture->linesize[i]);
-                optr += final_picture->linesize[i];
-                iptr += formatted_picture->linesize[i];
+        if (ost->video_resample) {
+            if (img_crop((AVPicture *)&picture_pad_temp, (AVPicture *)final_picture, target_pixfmt, ost->padtop, ost->padleft) < 0) {
+                av_log(NULL, AV_LOG_ERROR, "error padding picture\n");
+                goto the_end;
             }
+            resampling_dst = &picture_pad_temp;
         }
+    }
 
-        fill_pad_region((AVPicture*)final_picture, enc->height, enc->width,
-                ost->padtop, ost->padbottom, ost->padleft, ost->padright,
-                padcolor);
+    /* XXX: resampling could be done before raw format conversion in
+       some cases to go faster */
+    /* XXX: only works for YUV420P */
+    if (ost->video_resample) {
+        padding_src = NULL;
+        final_picture = &ost->pict_tmp;
+        img_resample(ost->img_resample_ctx, (AVPicture *)resampling_dst, (AVPicture*)formatted_picture);
+    }
 
-        if (enc->pix_fmt != PIX_FMT_YUV420P) {
-            int size;
+    if (enc->pix_fmt != target_pixfmt) {
+        int size;
 
-            av_free(buf);
-            /* create temporary picture */
-            size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
-            buf = av_malloc(size);
-            if (!buf)
-                return;
-            final_picture = &picture_format_temp;
-            avpicture_fill((AVPicture*)final_picture, buf, enc->pix_fmt, enc->width, enc->height);
+        av_free(buf);
+        /* create temporary picture */
+        size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
+        buf = av_malloc(size);
+        if (!buf)
+            return;
+        final_picture = &picture_format_temp;
+        avpicture_fill((AVPicture*)final_picture, buf, enc->pix_fmt, enc->width, enc->height);
 
-            if (img_convert((AVPicture*)final_picture, enc->pix_fmt,
-                        (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
+        if (img_convert((AVPicture*)final_picture, enc->pix_fmt,
+                        (AVPicture*)&ost->pict_tmp, target_pixfmt,
                         enc->width, enc->height) < 0) {
 
-                if (verbose >= 0)
-                    fprintf(stderr, "pixel format conversion not handled\n");
+            if (verbose >= 0)
+                fprintf(stderr, "pixel format conversion not handled\n");
 
-                goto the_end;
-            }
+            goto the_end;
         }
-    } else {
-        final_picture = formatted_picture;
     }
+
+    if (ost->video_pad) {
+        img_pad((AVPicture*)final_picture, (AVPicture *)padding_src,
+                enc->height, enc->width, enc->pix_fmt,
+                ost->padtop, ost->padbottom, ost->padleft, ost->padright, padcolor);
+    }
+
     /* duplicates frame if needed */
     for(i=0;i<nb_frames;i++) {
         AVPacket pkt;
@@ -1172,7 +1096,7 @@ static int output_packet(AVInputStream *ist, int ist_index,
     int data_size, got_picture;
     AVFrame picture;
     void *buffer_to_free;
-    static int samples_size= 0;
+    static unsigned int samples_size= 0;
     static short *samples= NULL;
     AVSubtitle subtitle, *subtitle_to_free;
     int got_subtitle;
@@ -1657,6 +1581,7 @@ static int av_encode(AVFormatContext **output_files,
                 codec->block_align= icodec->block_align;
                 break;
             case CODEC_TYPE_VIDEO:
+                codec->pix_fmt = icodec->pix_fmt;
                 codec->width = icodec->width;
                 codec->height = icodec->height;
                 codec->has_b_frames = icodec->has_b_frames;
@@ -1707,64 +1632,41 @@ static int av_encode(AVFormatContext **output_files,
                 ost->encoding_needed = 1;
                 break;
             case CODEC_TYPE_VIDEO:
-                if (codec->width == icodec->width &&
-                    codec->height == icodec->height &&
-                    frame_topBand == 0 &&
-                    frame_bottomBand == 0 &&
-                    frame_leftBand == 0 &&
-                    frame_rightBand == 0 &&
-                    frame_padtop == 0 &&
-                    frame_padbottom == 0 &&
-                    frame_padleft == 0 &&
-                    frame_padright == 0)
-                {
-                    ost->video_resample = 0;
-                    ost->video_crop = 0;
-                    ost->video_pad = 0;
-                } else if ((codec->width == icodec->width -
-                                (frame_leftBand + frame_rightBand)) &&
-                        (codec->height == icodec->height -
-                                (frame_topBand  + frame_bottomBand)))
-                {
-                    ost->video_resample = 0;
-                    ost->video_crop = 1;
+                ost->video_crop = ((frame_leftBand + frame_rightBand + frame_topBand + frame_bottomBand) != 0);
+                ost->video_pad = ((frame_padleft + frame_padright + frame_padtop + frame_padbottom) != 0);
+                ost->video_resample = ((codec->width != icodec->width -
+                                (frame_leftBand + frame_rightBand) +
+                                (frame_padleft + frame_padright)) ||
+                        (codec->height != icodec->height -
+                                (frame_topBand  + frame_bottomBand) +
+                                (frame_padtop + frame_padbottom)));
+                if (ost->video_crop) {
                     ost->topBand = frame_topBand;
                     ost->leftBand = frame_leftBand;
-                } else if ((codec->width == icodec->width +
-                                (frame_padleft + frame_padright)) &&
-                        (codec->height == icodec->height +
-                                (frame_padtop + frame_padbottom))) {
-                    ost->video_resample = 0;
-                    ost->video_crop = 0;
-                    ost->video_pad = 1;
+                }
+                if (ost->video_pad) {
                     ost->padtop = frame_padtop;
                     ost->padleft = frame_padleft;
                     ost->padbottom = frame_padbottom;
                     ost->padright = frame_padright;
-                    avcodec_get_frame_defaults(&ost->pict_tmp);
-                    if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
-                                codec->width, codec->height ) )
-                        goto fail;
-                } else {
-                    ost->video_resample = 1;
-                    ost->video_crop = 0; // cropping is handled as part of resample
+                    if (!ost->video_resample) {
+                        avcodec_get_frame_defaults(&ost->pict_tmp);
+                        if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, codec->pix_fmt,
+                                         codec->width, codec->height ) )
+                            goto fail;
+                    }
+                }
+                if (ost->video_resample) {
                     avcodec_get_frame_defaults(&ost->pict_tmp);
                     if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, PIX_FMT_YUV420P,
                                          codec->width, codec->height ) )
                         goto fail;
 
-                    ost->img_resample_ctx = img_resample_full_init(
-                                      codec->width, codec->height,
-                                      icodec->width, icodec->height,
-                                      frame_topBand, frame_bottomBand,
-                            frame_leftBand, frame_rightBand,
-                            frame_padtop, frame_padbottom,
-                            frame_padleft, frame_padright);
-
-                    ost->padtop = frame_padtop;
-                    ost->padleft = frame_padleft;
-                    ost->padbottom = frame_padbottom;
-                    ost->padright = frame_padright;
+                    ost->img_resample_ctx = img_resample_init(
+                            codec->width - (frame_padleft + frame_padright),
+                            codec->height - (frame_padtop + frame_padbottom),
+                            icodec->width - (frame_leftBand + frame_rightBand),
+                            icodec->height - (frame_topBand + frame_bottomBand));
 
                 }
                 ost->encoding_needed = 1;
@@ -2051,7 +1953,7 @@ static int av_encode(AVFormatContext **output_files,
 //        fprintf(stderr, "next:%lld dts:%lld off:%lld %d\n", ist->next_pts, pkt.dts, input_files_ts_offset[ist->file_index], ist->st->codec->codec_type);
         if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE) {
             int64_t delta= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q) - ist->next_pts;
-            if(ABS(delta) > 10LL*AV_TIME_BASE && !copy_ts){
+            if(ABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE && !copy_ts){
                 input_files_ts_offset[ist->file_index]-= delta;
                 if (verbose > 2)
                     fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", delta, input_files_ts_offset[ist->file_index]);
@@ -2500,9 +2402,9 @@ static void opt_pre_me(const char *arg)
 static void opt_qscale(const char *arg)
 {
     video_qscale = atof(arg);
-    if (video_qscale < 0.01 ||
+    if (video_qscale <= 0 ||
         video_qscale > 255) {
-        fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
+        fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
         exit(1);
     }
 }
@@ -3561,6 +3463,10 @@ static void prepare_grab(void)
 
                 if (vp->time_base.num*(int64_t)enc->time_base.den > enc->time_base.num*(int64_t)vp->time_base.den){
                     vp->time_base = enc->time_base;
+                    vp->width += frame_leftBand + frame_rightBand;
+                    vp->width -= (frame_padleft + frame_padright);
+                    vp->height += frame_topBand + frame_bottomBand;
+                    vp->height -= (frame_padtop + frame_padbottom);
                 }
                 has_video = 1;
                 break;
@@ -3581,6 +3487,7 @@ static void prepare_grab(void)
         vp->device  = video_device;
         vp->channel = video_channel;
         vp->standard = video_standard;
+        vp->pix_fmt = frame_pix_fmt;
         if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
             fprintf(stderr, "Could not find video grab device\n");
             exit(1);
@@ -3789,7 +3696,7 @@ static void show_formats(void)
     exit(1);
 }
 
-void parse_matrix_coeffs(uint16_t *dest, const char *str)
+static void parse_matrix_coeffs(uint16_t *dest, const char *str)
 {
     int i;
     const char *p = str;
@@ -3806,13 +3713,13 @@ void parse_matrix_coeffs(uint16_t *dest, const char *str)
     }
 }
 
-void opt_inter_matrix(const char *arg)
+static void opt_inter_matrix(const char *arg)
 {
     inter_matrix = av_mallocz(sizeof(uint16_t) * 64);
     parse_matrix_coeffs(inter_matrix, arg);
 }
 
-void opt_intra_matrix(const char *arg)
+static void opt_intra_matrix(const char *arg)
 {
     intra_matrix = av_mallocz(sizeof(uint16_t) * 64);
     parse_matrix_coeffs(intra_matrix, arg);
@@ -3944,11 +3851,13 @@ static void opt_target(const char *arg)
         audio_bit_rate = 448000;
         audio_sample_rate = 48000;
 
-    } else if(!strcmp(arg, "dv")) {
+    } else if(!strncmp(arg, "dv", 2)) {
 
         opt_format("dv");
 
         opt_frame_size(norm ? "720x480" : "720x576");
+        opt_frame_pix_fmt(!strncmp(arg, "dv50", 4) ? "yuv422p" :
+                                             (norm ? "yuv411p" : "yuv420p"));
         opt_frame_rate(frame_rates[norm]);
 
         audio_sample_rate = 48000;
@@ -3962,10 +3871,12 @@ static void opt_target(const char *arg)
 
 static void show_version(void)
 {
+    /* TODO: add function interface to avutil and avformat */
     fprintf(stderr, "ffmpeg      " FFMPEG_VERSION "\n"
+           "libavutil   %d\n"
            "libavcodec  %d\n"
            "libavformat %d\n",
-           avcodec_build(), LIBAVFORMAT_BUILD);
+           LIBAVUTIL_BUILD, avcodec_build(), LIBAVFORMAT_BUILD);
     exit(1);
 }
 
@@ -4017,16 +3928,17 @@ const OptionDef options[] = {
     { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
       "when dumping packets, also dump the payload" },
     { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
-    { "loop", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
+    { "loop_input", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
     { "loop_output", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&loop_output}, "number of times to loop output in formats that support looping (0 loops forever)", "" },
     { "v", HAS_ARG, {(void*)opt_verbose}, "control amount of logging", "verbose" },
-    { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
+    { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
     { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
     { "vsync", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_sync_method}, "video sync method", "" },
     { "async", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&audio_sync_method}, "audio sync method", "" },
     { "vglobal", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_global_header}, "video global header storage type", "" },
     { "copyts", OPT_BOOL | OPT_EXPERT, {(void*)&copy_ts}, "copy timestamps" },
     { "shortest", OPT_BOOL | OPT_EXPERT, {(void*)&opt_shortest}, "finish encoding within shortest input" }, //
+    { "dts_delta_threshold", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&dts_delta_threshold}, "timestamp discontinuity delta threshold", "" },
 
     /* video options */
     { "b", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
@@ -4149,6 +4061,7 @@ static void show_banner(void)
 {
     fprintf(stderr, "FFmpeg version " FFMPEG_VERSION ", Copyright (c) 2000-2004 Fabrice Bellard\n");
     fprintf(stderr, "  configuration: " FFMPEG_CONFIGURATION "\n");
+    fprintf(stderr, "  libavutil version: " AV_STRINGIFY(LIBAVUTIL_VERSION) "\n");
     fprintf(stderr, "  libavcodec version: " AV_STRINGIFY(LIBAVCODEC_VERSION) "\n");
     fprintf(stderr, "  libavformat version: " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\n");
     fprintf(stderr, "  built on " __DATE__ " " __TIME__);