mjpega_dump_header_bsf.c

Go to the documentation of this file.
00001 /*
00002  * MJPEG A dump header bitstream filter
00003  * Copyright (c) 2006 Baptiste Coudurier.
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /**
00023  * @file mjpega_dump_header_bsf.c
00024  * MJPEG A dump header bitstream filter
00025  * modifies bitstream to be decoded by quicktime
00026  */
00027 
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "mjpeg.h"
00031 
00032 
00033 static int mjpega_dump_header(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
00034                               uint8_t **poutbuf, int *poutbuf_size,
00035                               const uint8_t *buf, int buf_size, int keyframe)
00036 {
00037     uint8_t *poutbufp;
00038     int i;
00039 
00040     if (avctx->codec_id != CODEC_ID_MJPEG) {
00041         av_log(avctx, AV_LOG_ERROR, "mjpega bitstream filter only applies to mjpeg codec\n");
00042         return 0;
00043     }
00044 
00045     *poutbuf_size = 0;
00046     *poutbuf = av_malloc(buf_size + 44 + FF_INPUT_BUFFER_PADDING_SIZE);
00047     poutbufp = *poutbuf;
00048     bytestream_put_byte(&poutbufp, 0xff);
00049     bytestream_put_byte(&poutbufp, SOI);
00050     bytestream_put_byte(&poutbufp, 0xff);
00051     bytestream_put_byte(&poutbufp, APP1);
00052     bytestream_put_be16(&poutbufp, 42); /* size */
00053     bytestream_put_be32(&poutbufp, 0);
00054     bytestream_put_buffer(&poutbufp, "mjpg", 4);
00055     bytestream_put_be32(&poutbufp, buf_size + 44); /* field size */
00056     bytestream_put_be32(&poutbufp, buf_size + 44); /* pad field size */
00057     bytestream_put_be32(&poutbufp, 0);             /* next ptr */
00058 
00059     for (i = 0; i < buf_size - 1; i++) {
00060         if (buf[i] == 0xff) {
00061             switch (buf[i + 1]) {
00062             case DQT:  /* quant off */
00063             case DHT:  /* huff  off */
00064             case SOF0: /* image off */
00065                 bytestream_put_be32(&poutbufp, i + 46);
00066                 break;
00067             case SOS:
00068                 bytestream_put_be32(&poutbufp, i + 46); /* scan off */
00069                 bytestream_put_be32(&poutbufp, i + 46 + AV_RB16(buf + i + 2)); /* data off */
00070                 bytestream_put_buffer(&poutbufp, buf + 2, buf_size - 2); /* skip already written SOI */
00071                 *poutbuf_size = poutbufp - *poutbuf;
00072                 return 1;
00073             case APP1:
00074                 if (i + 8 < buf_size && AV_RL32(buf + i + 8) == ff_get_fourcc("mjpg")) {
00075                     av_log(avctx, AV_LOG_ERROR, "bitstream already formatted\n");
00076                     memcpy(*poutbuf, buf, buf_size);
00077                     *poutbuf_size = buf_size;
00078                     return 1;
00079                 }
00080             }
00081         }
00082     }
00083     av_freep(poutbuf);
00084     av_log(avctx, AV_LOG_ERROR, "could not find SOS marker in bitstream\n");
00085     return 0;
00086 }
00087 
00088 AVBitStreamFilter mjpega_dump_header_bsf = {
00089     "mjpegadump",
00090     0,
00091     mjpega_dump_header,
00092 };

Generated on Thu Nov 20 08:44:44 2008 for libextractor by  doxygen 1.5.1