[FFmpeg-devel] mjpeg2jpeg_filter

Don Moir donmoir at comcast.net
Sun Dec 14 14:10:31 CET 2014


I needed to convert AVI1 to normal jpeg. This happens a lot with camera streams and the code in mjpeg2jpeg_filter works fine.

I was looking at the code in mjpeg2jpeg_filter in file mjpeg2jpeg_bsf.c and noticing that just creates a constant array of bytes each time for the replacement header.

Unless I am missing something, those bytes could just be declared in a const array rather then building it each frame. Not sure if the values in the various const arrays are subject to change or not and just an observation.

The only thing variable appears to be the input_skip value.

Here's the code. 

These 2 just build a constant array of (sizeof(jpeg_header) + dht_segment_size) bytes.  dht_segment_size is a const.

    out = append(out, jpeg_header, sizeof(jpeg_header));
    out = append_dht_segment(out);

static int mjpeg2jpeg_filter(AVBitStreamFilterContext *bsfc,
                             AVCodecContext *avctx, const char *args,
                             uint8_t **poutbuf, int *poutbuf_size,
                             const uint8_t *buf, int buf_size,
                             int keyframe)
{
    int input_skip, output_size;
    uint8_t *output, *out;

    if (buf_size < 12) {
        av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
        return AVERROR_INVALIDDATA;
    }
    if (memcmp("AVI1", buf + 6, 4)) {
        av_log(avctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
        return AVERROR_INVALIDDATA;
    }
    input_skip = (buf[4] << 8) + buf[5] + 4;
    if (buf_size < input_skip) {
        av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
        return AVERROR_INVALIDDATA;
    }
    output_size = buf_size - input_skip +
                  sizeof(jpeg_header) + dht_segment_size;
    output = out = av_malloc(output_size);
    if (!output)
        return AVERROR(ENOMEM);
    out = append(out, jpeg_header, sizeof(jpeg_header));
    out = append_dht_segment(out);
    out = append(out, buf + input_skip, buf_size - input_skip);
    *poutbuf = output;
    *poutbuf_size = output_size;
    return 1;
}


More information about the ffmpeg-devel mailing list