annotate ffmpeg/libavcodec/mjpeg2jpeg_bsf.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * MJPEG/AVI1 to JPEG/JFIF bitstream format filter
yading@10 3 * Copyright (c) 2010 Adrian Daerr and Nicolas George
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /*
yading@10 23 * Adapted from mjpeg2jpeg.c, with original copyright:
yading@10 24 * Paris 2010 Adrian Daerr, public domain
yading@10 25 */
yading@10 26
yading@10 27 #include <string.h>
yading@10 28 #include "avcodec.h"
yading@10 29 #include "mjpeg.h"
yading@10 30
yading@10 31 static const uint8_t jpeg_header[] = {
yading@10 32 0xff, 0xd8, // SOI
yading@10 33 0xff, 0xe0, // APP0
yading@10 34 0x00, 0x10, // APP0 header size (including
yading@10 35 // this field, but excluding preceding)
yading@10 36 0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0'
yading@10 37 0x01, 0x01, // version
yading@10 38 0x00, // bits per type
yading@10 39 0x00, 0x00, // X density
yading@10 40 0x00, 0x00, // Y density
yading@10 41 0x00, // X thumbnail size
yading@10 42 0x00, // Y thumbnail size
yading@10 43 };
yading@10 44
yading@10 45 static const int dht_segment_size = 420;
yading@10 46 static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
yading@10 47 static const uint8_t dht_segment_frag[] = {
yading@10 48 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
yading@10 49 0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
yading@10 50 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
yading@10 51 };
yading@10 52
yading@10 53 static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
yading@10 54 {
yading@10 55 memcpy(buf, src, size);
yading@10 56 return buf + size;
yading@10 57 }
yading@10 58
yading@10 59 static uint8_t *append_dht_segment(uint8_t *buf)
yading@10 60 {
yading@10 61 buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
yading@10 62 buf = append(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16);
yading@10 63 buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
yading@10 64 buf = append(buf, avpriv_mjpeg_val_dc, 12);
yading@10 65 *(buf++) = 0x10;
yading@10 66 buf = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
yading@10 67 buf = append(buf, avpriv_mjpeg_val_ac_luminance, 162);
yading@10 68 *(buf++) = 0x11;
yading@10 69 buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
yading@10 70 buf = append(buf, avpriv_mjpeg_val_ac_chrominance, 162);
yading@10 71 return buf;
yading@10 72 }
yading@10 73
yading@10 74 static int mjpeg2jpeg_filter(AVBitStreamFilterContext *bsfc,
yading@10 75 AVCodecContext *avctx, const char *args,
yading@10 76 uint8_t **poutbuf, int *poutbuf_size,
yading@10 77 const uint8_t *buf, int buf_size,
yading@10 78 int keyframe)
yading@10 79 {
yading@10 80 int input_skip, output_size;
yading@10 81 uint8_t *output, *out;
yading@10 82
yading@10 83 if (buf_size < 12) {
yading@10 84 av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
yading@10 85 return AVERROR_INVALIDDATA;
yading@10 86 }
yading@10 87 if (memcmp("AVI1", buf + 6, 4)) {
yading@10 88 av_log(avctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
yading@10 89 return AVERROR_INVALIDDATA;
yading@10 90 }
yading@10 91 input_skip = (buf[4] << 8) + buf[5] + 4;
yading@10 92 if (buf_size < input_skip) {
yading@10 93 av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
yading@10 94 return AVERROR_INVALIDDATA;
yading@10 95 }
yading@10 96 output_size = buf_size - input_skip +
yading@10 97 sizeof(jpeg_header) + dht_segment_size;
yading@10 98 output = out = av_malloc(output_size);
yading@10 99 if (!output)
yading@10 100 return AVERROR(ENOMEM);
yading@10 101 out = append(out, jpeg_header, sizeof(jpeg_header));
yading@10 102 out = append_dht_segment(out);
yading@10 103 out = append(out, buf + input_skip, buf_size - input_skip);
yading@10 104 *poutbuf = output;
yading@10 105 *poutbuf_size = output_size;
yading@10 106 return 1;
yading@10 107 }
yading@10 108
yading@10 109 AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
yading@10 110 .name = "mjpeg2jpeg",
yading@10 111 .filter = mjpeg2jpeg_filter,
yading@10 112 };