[Ffmpeg-devel] I need help using sws_scale()

Cool_Zer0 c00jz3r0
Tue Apr 10 20:51:01 CEST 2007


Hi.

I have the following code:

av_register_all();
avcodec_init();
avcodec_register_all();

AVFormatContext *inputContext;
int original_width, original_heigth;
int new_width, new_heigth;

AVCodec *dec_codec;
AVCodec *enc_codec;
AVCodecContext *outputContext;

AVFrame *original_frame = avcodec_alloc_frame();
AVFrame *resized_frame = avcodec_alloc_frame();

int r=0;

av_open_input_file(&inputContext, "bomba.jpg", NULL, 0, NULL);
av_find_stream_info(inputContext);
if (inputContext->nb_streams > 0) {
        int original_width = inputContext->streams[0]->codec->width;
        int original_heigth = inputContext->streams[0]->codec->height;

    (...)
        /* I now know the new dimensions */
        new_width = 200;
        new_heigth = 200;

        int gotPicture = -1;

        AVPacket pkt;
        av_read_frame(inputContext, &pkt);

        AVCodec *dec_codec =
avcodec_find_decoder(inputContext->streams[0]->codec->codec_id);
        r = avcodec_open(inputContext->streams[0]->codec, dec_codec);
        r = avcodec_decode_video(inputContext->streams[0]->codec,
original_frame, &gotPicture, pkt.data, pkt.size);

    /* *************** PROBLEMS START HERE  *************** */
        SwsContext *scale_context;
        scale_context = sws_getContext(original_width, original_heigth,
inputContext->streams[0]->codec->pix_fmt,
            new_width, new_heigth,
inputContext->streams[0]->codec->pix_fmt, 0, NULL, NULL, NULL);


        outputContext = avcodec_alloc_context();
        outputContext->time_base.num=1;
        outputContext->time_base.den=25;
        outputContext->width = new_width;
        outputContext->height = new_heigth;
        outputContext->pix_fmt = inputContext->streams[0]->codec->pix_fmt;
        outputContext->bit_rate = 200000;

        enc_codec = avcodec_find_encoder(CODEC_ID_MJPEG);
        r = avcodec_open(outputContext, enc_codec); /* Always returns -1 */

        // Determine required buffer size and allocate buffer
        int
numBytes=avpicture_get_size(inputContext->streams[0]->codec->pix_fmt,
new_width, new_heigth);
        uint8_t *buffer=new uint8_t[numBytes];
        memset(buffer, 0, numBytes);
        avpicture_fill((AVPicture *)resized_frame, buffer,
inputContext->streams[0]->codec->pix_fmt, new_width, new_heigth);

        r = sws_scale(scale_context, original_frame->data,
original_frame->linesize,
                new_width, new_heigth, resized_frame->data,
resized_frame->linesize);

        int video_outbuf_size = 200000;
        uint8_t *video_outbuf = (uint8_t*)av_malloc(video_outbuf_size);

        r = avcodec_encode_video(outputContext, video_outbuf,
video_outbuf_size, resized_frame);

}

av_close_input_file(inputContext);



All this to resize a simple .jpg image to 200x200...
Using the ffmpeg program everything works...
$ ffmpeg -i bomba.jpg -s 300x300 -f image2 bomba2.jpg
FFmpeg version SVN-r8538, Copyright (c) 2000-2007 Fabrice Bellard, et al.
  configuration: --enable-memalign-hack --enable-debug --enable-shared
--disable-static --disable-ffserver
  libavutil version: 49.4.0
  libavcodec version: 51.40.2
  libavformat version: 51.11.0
  built on Apr 10 2007 19:36:49, gcc: 3.4.5 (mingw special)
Input #0, image2, from 'bomba.jpg':
  Duration: 00:00:00.0, start: 0.000000, bitrate: N/A
  Stream #0.0: Video: mjpeg, yuvj444p, 636x529, 25.00 fps(r)
PIX_FMT_YUV420P will be used as an intermediate format for rescaling
Output #0, image2, to 'bomba2.jpg':
  Stream #0.0: Video: mjpeg, yuvj420p, 300x300, q=2-31, 200 kb/s, 25.00
fps(c)
Stream mapping:
  Stream #0.0 -> #0.0
Press [q] to stop encoding
Compiler did not align stack variables. Libavcodec has been miscompiled
and may be very slow or crash. This is not a bug in libavcodec,
but in the compiler. Do not report crashes to FFmpeg developers.
frame=    1 fps=  0 q=4.6 Lsize=       0kB time=0.0 bitrate=
0.0kbits/s
video:16kB audio:0kB global headers:0kB muxing overhead -100.000000%




When using the API it always returns -1 on:
r = avcodec_open(outputContext, enc_codec); /* Always returns -1 */

Anyone can explain me why?


Next...
To resize the image... I'm following the correct procedure or something
more "friendly" exists?

I appreciate your help.

Thanks





More information about the ffmpeg-devel mailing list