Revision 5879ea6d ffmpeg.c

View differences:

ffmpeg.c
136 136
static float frame_aspect_ratio = 0;
137 137
static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
138 138
static enum SampleFormat audio_sample_fmt = SAMPLE_FMT_NONE;
139
static int frame_topBand  = 0;
140
static int frame_bottomBand = 0;
141
static int frame_leftBand  = 0;
142
static int frame_rightBand = 0;
143 139
static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
144 140
static AVRational frame_rate;
145 141
static float video_qscale = 0;
......
276 272
    int original_height;
277 273
    int original_width;
278 274

  
279
    /* cropping area sizes */
280
    int video_crop;
281
    int topBand;
282
    int bottomBand;
283
    int leftBand;
284
    int rightBand;
285

  
286
    /* cropping area of first frame */
287
    int original_topBand;
288
    int original_bottomBand;
289
    int original_leftBand;
290
    int original_rightBand;
291

  
292 275
    /* audio only */
293 276
    int audio_resample;
294 277
    ReSampleContext *resample; /* for audio resampling */
......
395 378

  
396 379
    last_filter = ist->input_video_filter;
397 380

  
398
    if (ost->video_crop) {
399
        snprintf(args, 255, "%d:%d:%d:%d",
400
                 codec->width, codec->height,
401
                 ost->leftBand, ost->topBand);
402
        if ((ret = avfilter_open(&filter, avfilter_get_by_name("crop"), NULL)) < 0)
403
            return ret;
404
        if ((ret = avfilter_init_filter(filter, args, NULL)) < 0)
405
            return ret;
406
        if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
407
            return ret;
408
        last_filter = filter;
409
        avfilter_graph_add_filter(graph, last_filter);
410
    }
411

  
412
    if((codec->width !=
413
        icodec->width - (frame_leftBand + frame_rightBand)) ||
414
       (codec->height != icodec->height - (frame_topBand  + frame_bottomBand))) {
381
    if (codec->width  != icodec->width || codec->height != icodec->height) {
415 382
        snprintf(args, 255, "%d:%d:flags=0x%X",
416 383
                 codec->width,
417 384
                 codec->height,
......
1113 1080
                         int *frame_size)
1114 1081
{
1115 1082
    int nb_frames, i, ret;
1116
#if !CONFIG_AVFILTER
1117
    int64_t topBand, bottomBand, leftBand, rightBand;
1118
#endif
1119 1083
    AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;
1120 1084
    AVFrame picture_crop_temp, picture_pad_temp;
1121 1085
    AVCodecContext *enc, *dec;
......
1163 1127
    if (nb_frames <= 0)
1164 1128
        return;
1165 1129

  
1166
#if CONFIG_AVFILTER
1167 1130
    formatted_picture = in_picture;
1168
#else
1169
    if (ost->video_crop) {
1170
        if (av_picture_crop((AVPicture *)&picture_crop_temp, (AVPicture *)in_picture, dec->pix_fmt, ost->topBand, ost->leftBand) < 0) {
1171
            fprintf(stderr, "error cropping picture\n");
1172
            if (exit_on_error)
1173
                ffmpeg_exit(1);
1174
            return;
1175
        }
1176
        formatted_picture = &picture_crop_temp;
1177
    } else {
1178
        formatted_picture = in_picture;
1179
    }
1180
#endif
1181

  
1182 1131
    final_picture = formatted_picture;
1183 1132
    padding_src = formatted_picture;
1184 1133
    resampling_dst = &ost->pict_tmp;
1185 1134

  
1186
    if(    (ost->resample_height != (ist->st->codec->height - (ost->topBand  + ost->bottomBand)))
1187
        || (ost->resample_width  != (ist->st->codec->width  - (ost->leftBand + ost->rightBand)))
1135
    if (   ost->resample_height != ist->st->codec->height
1136
        || ost->resample_width  != ist->st->codec->width
1188 1137
        || (ost->resample_pix_fmt!= ist->st->codec->pix_fmt) ) {
1189 1138

  
1190 1139
        fprintf(stderr,"Input Stream #%d.%d frame size changed to %dx%d, %s\n", ist->file_index, ist->index, ist->st->codec->width,     ist->st->codec->height,avcodec_get_pix_fmt_name(ist->st->codec->pix_fmt));
......
1196 1145
    if (ost->video_resample) {
1197 1146
        padding_src = NULL;
1198 1147
        final_picture = &ost->pict_tmp;
1199
        if(  (ost->resample_height != (ist->st->codec->height - (ost->topBand  + ost->bottomBand)))
1200
          || (ost->resample_width  != (ist->st->codec->width  - (ost->leftBand + ost->rightBand)))
1148
        if(  ost->resample_height != ist->st->codec->height
1149
          || ost->resample_width  != ist->st->codec->width
1201 1150
          || (ost->resample_pix_fmt!= ist->st->codec->pix_fmt) ) {
1202 1151

  
1203
            /* keep bands proportional to the frame size */
1204
            topBand    = ((int64_t)ist->st->codec->height * ost->original_topBand    / ost->original_height) & ~1;
1205
            bottomBand = ((int64_t)ist->st->codec->height * ost->original_bottomBand / ost->original_height) & ~1;
1206
            leftBand   = ((int64_t)ist->st->codec->width  * ost->original_leftBand   / ost->original_width)  & ~1;
1207
            rightBand  = ((int64_t)ist->st->codec->width  * ost->original_rightBand  / ost->original_width)  & ~1;
1208

  
1209
            /* sanity check to ensure no bad band sizes sneak in */
1210
            av_assert0(topBand    <= INT_MAX && topBand    >= 0);
1211
            av_assert0(bottomBand <= INT_MAX && bottomBand >= 0);
1212
            av_assert0(leftBand   <= INT_MAX && leftBand   >= 0);
1213
            av_assert0(rightBand  <= INT_MAX && rightBand  >= 0);
1214

  
1215
            ost->topBand    = topBand;
1216
            ost->bottomBand = bottomBand;
1217
            ost->leftBand   = leftBand;
1218
            ost->rightBand  = rightBand;
1219

  
1220
            ost->resample_height = ist->st->codec->height - (ost->topBand  + ost->bottomBand);
1221
            ost->resample_width  = ist->st->codec->width  - (ost->leftBand + ost->rightBand);
1222
            ost->resample_pix_fmt= ist->st->codec->pix_fmt;
1223

  
1224 1152
            /* initialize a new scaler context */
1225 1153
            sws_freeContext(ost->img_resample_ctx);
1226 1154
            sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
1227 1155
            ost->img_resample_ctx = sws_getContext(
1228
                ist->st->codec->width  - (ost->leftBand + ost->rightBand),
1229
                ist->st->codec->height - (ost->topBand  + ost->bottomBand),
1156
                ist->st->codec->width,
1157
                ist->st->codec->height,
1230 1158
                ist->st->codec->pix_fmt,
1231 1159
                ost->st->codec->width,
1232 1160
                ost->st->codec->height,
......
2209 2137
                    fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n");
2210 2138
                    ffmpeg_exit(1);
2211 2139
                }
2212
                ost->video_crop = ((frame_leftBand + frame_rightBand + frame_topBand + frame_bottomBand) != 0);
2213
                ost->video_resample = ((codec->width != icodec->width -
2214
                                (frame_leftBand + frame_rightBand)) ||
2215
                        (codec->height != icodec->height -
2216
                                (frame_topBand  + frame_bottomBand)) ||
2140
                ost->video_resample = (codec->width != icodec->width   ||
2141
                                       codec->height != icodec->height ||
2217 2142
                        (codec->pix_fmt != icodec->pix_fmt));
2218
                if (ost->video_crop) {
2219
                    ost->topBand    = ost->original_topBand    = frame_topBand;
2220
                    ost->bottomBand = ost->original_bottomBand = frame_bottomBand;
2221
                    ost->leftBand   = ost->original_leftBand   = frame_leftBand;
2222
                    ost->rightBand  = ost->original_rightBand  = frame_rightBand;
2223
                }
2224 2143
                if (ost->video_resample) {
2225 2144
                    avcodec_get_frame_defaults(&ost->pict_tmp);
2226 2145
                    if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt,
......
2230 2149
                    }
2231 2150
                    sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
2232 2151
                    ost->img_resample_ctx = sws_getContext(
2233
                            icodec->width - (frame_leftBand + frame_rightBand),
2234
                            icodec->height - (frame_topBand + frame_bottomBand),
2152
                        icodec->width,
2153
                        icodec->height,
2235 2154
                            icodec->pix_fmt,
2236 2155
                            codec->width,
2237 2156
                            codec->height,
......
2248 2167
#endif
2249 2168
                    codec->bits_per_raw_sample= 0;
2250 2169
                }
2251
                ost->resample_height = icodec->height - (frame_topBand  + frame_bottomBand);
2252
                ost->resample_width  = icodec->width  - (frame_leftBand + frame_rightBand);
2170
                ost->resample_height = icodec->height;
2171
                ost->resample_width  = icodec->width;
2253 2172
                ost->resample_pix_fmt= icodec->pix_fmt;
2254 2173
                ost->encoding_needed = 1;
2255 2174
                ist->decoding_needed = 1;
......
2744 2663
    return 0;
2745 2664
}
2746 2665

  
2747
static void opt_frame_crop_top(const char *arg)
2748
{
2749
    frame_topBand = atoi(arg);
2750
    if (frame_topBand < 0) {
2751
        fprintf(stderr, "Incorrect top crop size\n");
2752
        ffmpeg_exit(1);
2753
    }
2754
    if ((frame_topBand) >= frame_height){
2755
        fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2756
        ffmpeg_exit(1);
2757
    }
2758
    fprintf(stderr, "-crop* is deprecated in favor of the crop avfilter\n");
2759
    frame_height -= frame_topBand;
2760
}
2761

  
2762
static void opt_frame_crop_bottom(const char *arg)
2763
{
2764
    frame_bottomBand = atoi(arg);
2765
    if (frame_bottomBand < 0) {
2766
        fprintf(stderr, "Incorrect bottom crop size\n");
2767
        ffmpeg_exit(1);
2768
    }
2769
    if ((frame_bottomBand) >= frame_height){
2770
        fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2771
        ffmpeg_exit(1);
2772
    }
2773
    fprintf(stderr, "-crop* is deprecated in favor of the crop avfilter\n");
2774
    frame_height -= frame_bottomBand;
2775
}
2776

  
2777
static void opt_frame_crop_left(const char *arg)
2666
static int opt_frame_crop(const char *opt, const char *arg)
2778 2667
{
2779
    frame_leftBand = atoi(arg);
2780
    if (frame_leftBand < 0) {
2781
        fprintf(stderr, "Incorrect left crop size\n");
2782
        ffmpeg_exit(1);
2783
    }
2784
    if ((frame_leftBand) >= frame_width){
2785
        fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2786
        ffmpeg_exit(1);
2787
    }
2788
    fprintf(stderr, "-crop* is deprecated in favor of the crop avfilter\n");
2789
    frame_width -= frame_leftBand;
2790
}
2791

  
2792
static void opt_frame_crop_right(const char *arg)
2793
{
2794
    frame_rightBand = atoi(arg);
2795
    if (frame_rightBand < 0) {
2796
        fprintf(stderr, "Incorrect right crop size\n");
2797
        ffmpeg_exit(1);
2798
    }
2799
    if ((frame_rightBand) >= frame_width){
2800
        fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2801
        ffmpeg_exit(1);
2802
    }
2803
    fprintf(stderr, "-crop* is deprecated in favor of the crop avfilter\n");
2804
    frame_width -= frame_rightBand;
2668
    fprintf(stderr, "Option '%s' has been removed, use the crop filter instead\n", opt);
2669
    return AVERROR(EINVAL);
2805 2670
}
2806 2671

  
2807 2672
static void opt_frame_size(const char *arg)
......
4211 4076
    { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
4212 4077
    { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
4213 4078
    { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format, 'list' as argument shows all the pixel formats supported", "format" },
4214
    { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_top}, "Deprecated, please use the crop avfilter", "size" },
4215
    { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_bottom}, "Deprecated, please use the crop avfilter", "size" },
4216
    { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_left}, "Deprecated, please use the crop avfilter", "size" },
4217
    { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_right}, "Deprecated, please use the crop avfilter", "size" },
4079
    { "croptop", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Deprecated, please use the crop avfilter", "size" },
4080
    { "cropbottom", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, please use the crop avfilter", "size" },
4081
    { "cropleft", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, please use the crop avfilter", "size" },
4082
    { "cropright", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop}, "Removed, please use the crop avfilter", "size" },
4218 4083
    { "padtop", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_pad}, "Removed, use the pad filter instead", "size" },
4219 4084
    { "padbottom", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_pad}, "Removed, use the pad filter instead", "size" },
4220 4085
    { "padleft", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_pad}, "Removed, use the pad filter instead", "size" },

Also available in: Unified diff