* MJPEG/AVI1 to JPEG/JFIF bitstream format filter
* Copyright (c) 2010 Adrian Daerr and Nicolas George
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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,
+ * Libav 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
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
*/
#include <string.h>
+
+#include "libavutil/error.h"
+#include "libavutil/mem.h"
+
#include "avcodec.h"
-#include "mjpeg.h"
+#include "bsf.h"
+#include "jpegtables.h"
static const uint8_t jpeg_header[] = {
0xff, 0xd8, // SOI
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, avpriv_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 = append(buf, avpriv_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 = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
+ buf = append(buf, avpriv_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);
+ buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
+ buf = append(buf, avpriv_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)
+static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
{
+ AVPacket *in;
+ int ret = 0;
int input_skip, output_size;
- uint8_t *output, *out;
+ uint8_t *output;
- if (buf_size < 12) {
- av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
- return AVERROR_INVALIDDATA;
+ ret = ff_bsf_get_packet(ctx, &in);
+
+ if (in->size < 12) {
+ av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
}
- if (memcmp("AVI1", buf + 6, 4)) {
- av_log(avctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
- return AVERROR_INVALIDDATA;
+ if (memcmp("AVI1", in->data + 6, 4)) {
+ av_log(ctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
}
- 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;
+
+ input_skip = (in->data[4] << 8) + in->data[5] + 4;
+ if (in->size < input_skip) {
+ av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
+ ret = AVERROR_INVALIDDATA;
+ goto fail;
}
- output_size = buf_size - input_skip +
+ output_size = in->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;
+ ret = av_new_packet(out, output_size);
+ if (ret < 0)
+ goto fail;
+
+ output = out->data;
+
+ output = append(output, jpeg_header, sizeof(jpeg_header));
+ output = append_dht_segment(output);
+ output = append(output, in->data + input_skip, in->size - input_skip);
+
+ ret = av_packet_copy_props(out, in);
+ if (ret < 0)
+ goto fail;
+
+fail:
+ if (ret < 0)
+ av_packet_unref(out);
+ av_packet_free(&in);
+ return ret;
}
-AVBitStreamFilter mjpeg2jpeg_bsf = {
+const AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
.name = "mjpeg2jpeg",
- .priv_data_size = 0,
.filter = mjpeg2jpeg_filter,
};