[FFmpeg-devel] [PATCH] MJPEG/AVI1 to JPEG/JFIF byte stream format filter

Stefano Sabatini stefano.sabatini-lala
Wed Nov 3 22:41:23 CET 2010


On date Wednesday 2010-11-03 11:11:53 +0100, Nicolas George encoded:
> Hi.
> 
> I am sorry that my plugin proposal started such a flame. Back to a less
> controversial matter.
> 
> The attached patch implements a new bitstream filter, mjpeg2jpeg, that adds
> the constant omitted Huffman tables to MJPEG-in-AVI packets to turn them to
> self-contained JPEG packets.
> 
> Regards,
> 
> -- 
>   Nicolas George

>  Changelog                   |    1 +
>  libavcodec/Makefile         |    1 +
>  libavcodec/allcodecs.c      |    1 +
>  libavcodec/mjpeg2jpeg_bsf.c |  146 +++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 149 insertions(+), 0 deletions(-)
> 
> diff --git a/Changelog b/Changelog
> index 0c97475..a22e16e 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -53,6 +53,7 @@ version <next>:
>  - single stream LATM/LOAS decoder
>  - setpts filter added
>  - Win64 support for optimized asm functions
> +- MJPEG/AVI1 to JPEG/JFIF byte stream format filter
>  
>  
>  version 0.6:
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 5abc495..04fe935 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -602,6 +602,7 @@ OBJS-$(CONFIG_CHOMP_BSF)                  += chomp_bsf.o
>  OBJS-$(CONFIG_DUMP_EXTRADATA_BSF)         += dump_extradata_bsf.o
>  OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)       += h264_mp4toannexb_bsf.o
>  OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)        += imx_dump_header_bsf.o
> +OBJS-$(CONFIG_MJPEG2JPEG_BSF)             += mjpeg2jpeg_bsf.o
>  OBJS-$(CONFIG_MJPEGA_DUMP_HEADER_BSF)     += mjpega_dump_header_bsf.o
>  OBJS-$(CONFIG_MOV2TEXTSUB_BSF)            += movsub_bsf.o
>  OBJS-$(CONFIG_MP3_HEADER_COMPRESS_BSF)    += mp3_header_compress_bsf.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index c3e4647..66b858d 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -394,6 +394,7 @@ void avcodec_register_all(void)
>      REGISTER_BSF     (DUMP_EXTRADATA, dump_extradata);
>      REGISTER_BSF     (H264_MP4TOANNEXB, h264_mp4toannexb);
>      REGISTER_BSF     (IMX_DUMP_HEADER, imx_dump_header);
> +    REGISTER_BSF     (MJPEG2JPEG, mjpeg2jpeg);
>      REGISTER_BSF     (MJPEGA_DUMP_HEADER, mjpega_dump_header);
>      REGISTER_BSF     (MP3_HEADER_COMPRESS, mp3_header_compress);
>      REGISTER_BSF     (MP3_HEADER_DECOMPRESS, mp3_header_decompress);
> diff --git a/libavcodec/mjpeg2jpeg_bsf.c b/libavcodec/mjpeg2jpeg_bsf.c
> new file mode 100644
> index 0000000..2a28b4e
> --- /dev/null
> +++ b/libavcodec/mjpeg2jpeg_bsf.c
> @@ -0,0 +1,146 @@
> +/*
> + * MJPEG/AVI1 to JPEG/JFIF byte stream format filter
> + * Copyright (c) 2010 Adrian Daerr and Nicolas George
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/*
> + * MJPEG is a vid?o codec wherein each video frame is essentially a
> + * jpeg image. The individual frames can be extracted without loss,
> + * e.g. by
> + * ffmpeg -i ../some_mjpeg.avi -vcodec copy frames_%d.jpg
> + * Unfortunately, these chunks are incomplete jpeg images, because
> + * they lack the DHT segment required for decoding. Quoting from
> + *
> + *  http://www.digitalpreservation.gov/formats/fdd/fdd000063.shtml
> + *
> + * > Avery Lee, writing in the rec.video.desktop newsgroup in 2001,
> + * > commented that "MJPEG, or at least the MJPEG in AVIs having the
> + * > MJPG fourcc, is restricted JPEG with a fixed -- and *omitted* --
> + * > Huffman table. The JPEG must be YCbCr colorspace, it must be 4:2:2,
> + * > and it must use basic Huffman encoding, not arithmetic or
> + * > progressive. . . . You can indeed extract the MJPEG frames and
> + * > decode them with a regular JPEG decoder, but you have to prepend
> + * > the DHT segment to them, or else the decoder won't have any idea
> + * > how to decompress the data. The exact table necessary is given in
> + * > the OpenDML spec."
> + *
> + * This program patches the header of frames extracted from an mjpeg
> + * stream (carrying the AVI1 header ID and lacking a DHT segment) to
> + * produce fully qualified JPEG images. The program is not robust. It
> + * seems to work with frames extracted by ffmpeg, but testing is
> + * restricted to checking the presence of (and skipping) the AVI1 header.
> + *
> + * EXAMPLE
> + * to losslessly rotate a MJPEG movie using ffmpeg and exiftran:
> + *   ffmpeg -i mjpeg-movie.avi -vcodec copy frame_%d.jpg
> + *   for f in frame*jpg ; do mjpeg2jpeg $f mod_$f ; done
> + *   exiftran -i -9 mod_*jpg
> + *   ffmpeg -i mod_frame_%d.jpg -vcodec copy rotated.avi
> + *
> + * Paris 2010 Adrian Daerr, public domain

This doc is great and it should deserve its place in bitstreams.texi,
would you mind to create that as well (possibly as a separate patch)?

> + */
> +
> +#include <string.h>
> +#include "avcodec.h"
> +#include "mjpeg.h"
> +
> +static const uint8_t jpeg_header[] = {
> +    0xff, 0xd8,                     // SOI
> +    0xff, 0xe0,                     // APP0
> +    0x00, 0x10,                     // APP0 header size (including
> +                                    // this field, but excluding preceding)
> +    0x4a, 0x46, 0x49, 0x46, 0x00,   // ID string 'JFIF\0'
> +    0x01, 0x01,                     // version
> +    0x00,                           // bits per type
> +    0x00, 0x00,                     // X density
> +    0x00, 0x00,                     // Y density
> +    0x00,                           // X thumbnail size
> +    0x00,                           // Y thumbnail size
> +};
> +
> +static const int dht_segment_size = 420;
> +static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
> +static const uint8_t dht_segment_frag[] = {
> +    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
> +    0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
> +    0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
> +};
> +
> +static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
> +{
> +    memcpy(buf, src, size);
> +    return buf + size;
> +}
> +
> +static uint8_t *append_dht_segment(uint8_t *buf)
> +{
> +    buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
> +    buf = append(buf, ff_mjpeg_bits_dc_luminance + 1, 16);
> +    buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
> +    buf = append(buf, ff_mjpeg_val_dc, 12);
> +    *(buf++) = 0x10;
> +    buf = append(buf, ff_mjpeg_bits_ac_luminance + 1, 16);
> +    buf = append(buf, ff_mjpeg_val_ac_luminance, 162);
> +    *(buf++) = 0x11;
> +    buf = append(buf, ff_mjpeg_bits_ac_chrominance + 1, 16);
> +    buf = append(buf, ff_mjpeg_val_ac_chrominance, 162);
> +    return buf;
> +}
> +
> +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;
> +
> +}

stray empty line

> +AVBitStreamFilter mjpeg2jpeg_bsf = {
> +    "mjpeg2jpeg",
> +    0,
> +    mjpeg2jpeg_filter,
> +};

named fields.
-- 
FFmpeg = Fiendish Foolish Miracolous Patchable Enigmatic Gorilla



More information about the ffmpeg-devel mailing list