2007-05-17 16:29:11 +00:00
|
|
|
/*
|
|
|
|
* MJPEG encoder
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2000, 2001 Fabrice Bellard
|
2007-05-17 16:29:11 +00:00
|
|
|
* Copyright (c) 2003 Alex Beregszaszi
|
|
|
|
* Copyright (c) 2003-2004 Michael Niedermayer
|
|
|
|
*
|
2007-07-05 10:37:29 +00:00
|
|
|
* Support for external huffman table, various fixes (AVID workaround),
|
|
|
|
* aspecting, new decode_frame mechanism and apple mjpeg-b support
|
|
|
|
* by Alex Beregszaszi
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2007-05-17 16:29:11 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-05-17 16:29:11 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-05-17 16:29:11 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-05-17 16:29:11 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2007-05-17 16:29:11 +00:00
|
|
|
* MJPEG encoder.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2013-12-01 21:05:18 +00:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
|
2007-05-17 16:29:11 +00:00
|
|
|
#include "avcodec.h"
|
2014-06-30 12:37:59 +00:00
|
|
|
#include "mjpegenc_common.h"
|
2007-05-17 16:29:11 +00:00
|
|
|
#include "mpegvideo.h"
|
|
|
|
#include "mjpeg.h"
|
2007-05-18 22:42:49 +00:00
|
|
|
#include "mjpegenc.h"
|
2007-05-17 16:29:11 +00:00
|
|
|
|
2008-03-21 03:11:20 +00:00
|
|
|
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
|
2007-05-17 16:29:11 +00:00
|
|
|
{
|
|
|
|
MJpegContext *m;
|
|
|
|
|
|
|
|
m = av_malloc(sizeof(MJpegContext));
|
|
|
|
if (!m)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s->min_qcoeff=-1023;
|
|
|
|
s->max_qcoeff= 1023;
|
|
|
|
|
|
|
|
/* build all the huffman tables */
|
2007-05-17 16:45:55 +00:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
|
|
|
|
m->huff_code_dc_luminance,
|
2012-09-06 13:50:20 +00:00
|
|
|
avpriv_mjpeg_bits_dc_luminance,
|
|
|
|
avpriv_mjpeg_val_dc);
|
2007-05-17 16:45:55 +00:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
|
|
|
|
m->huff_code_dc_chrominance,
|
2012-09-06 13:50:20 +00:00
|
|
|
avpriv_mjpeg_bits_dc_chrominance,
|
|
|
|
avpriv_mjpeg_val_dc);
|
2007-05-17 16:45:55 +00:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
|
|
|
|
m->huff_code_ac_luminance,
|
2012-09-06 13:50:20 +00:00
|
|
|
avpriv_mjpeg_bits_ac_luminance,
|
|
|
|
avpriv_mjpeg_val_ac_luminance);
|
2007-05-17 16:45:55 +00:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
|
|
|
|
m->huff_code_ac_chrominance,
|
2012-09-06 13:50:20 +00:00
|
|
|
avpriv_mjpeg_bits_ac_chrominance,
|
|
|
|
avpriv_mjpeg_val_ac_chrominance);
|
2007-05-17 16:29:11 +00:00
|
|
|
|
|
|
|
s->mjpeg_ctx = m;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-18 22:54:54 +00:00
|
|
|
void ff_mjpeg_encode_close(MpegEncContext *s)
|
2007-05-17 16:29:11 +00:00
|
|
|
{
|
|
|
|
av_free(s->mjpeg_ctx);
|
|
|
|
}
|
|
|
|
|
2013-01-20 00:02:29 +00:00
|
|
|
static void encode_block(MpegEncContext *s, int16_t *block, int n)
|
2007-05-17 16:29:11 +00:00
|
|
|
{
|
|
|
|
int mant, nbits, code, i, j;
|
|
|
|
int component, dc, run, last_index, val;
|
|
|
|
MJpegContext *m = s->mjpeg_ctx;
|
|
|
|
uint8_t *huff_size_ac;
|
|
|
|
uint16_t *huff_code_ac;
|
|
|
|
|
|
|
|
/* DC coef */
|
|
|
|
component = (n <= 3 ? 0 : (n&1) + 1);
|
|
|
|
dc = block[0]; /* overflow is impossible */
|
|
|
|
val = dc - s->last_dc[component];
|
|
|
|
if (n < 4) {
|
2013-12-01 21:05:18 +00:00
|
|
|
ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
|
2007-05-17 16:29:11 +00:00
|
|
|
huff_size_ac = m->huff_size_ac_luminance;
|
|
|
|
huff_code_ac = m->huff_code_ac_luminance;
|
|
|
|
} else {
|
2013-12-01 21:05:18 +00:00
|
|
|
ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
|
2007-05-17 16:29:11 +00:00
|
|
|
huff_size_ac = m->huff_size_ac_chrominance;
|
|
|
|
huff_code_ac = m->huff_code_ac_chrominance;
|
|
|
|
}
|
|
|
|
s->last_dc[component] = dc;
|
|
|
|
|
|
|
|
/* AC coefs */
|
|
|
|
|
|
|
|
run = 0;
|
|
|
|
last_index = s->block_last_index[n];
|
|
|
|
for(i=1;i<=last_index;i++) {
|
|
|
|
j = s->intra_scantable.permutated[i];
|
|
|
|
val = block[j];
|
|
|
|
if (val == 0) {
|
|
|
|
run++;
|
|
|
|
} else {
|
|
|
|
while (run >= 16) {
|
|
|
|
put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
|
|
|
|
run -= 16;
|
|
|
|
}
|
|
|
|
mant = val;
|
|
|
|
if (val < 0) {
|
|
|
|
val = -val;
|
|
|
|
mant--;
|
|
|
|
}
|
|
|
|
|
|
|
|
nbits= av_log2(val) + 1;
|
|
|
|
code = (run << 4) | nbits;
|
|
|
|
|
|
|
|
put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
|
|
|
|
|
2008-07-13 14:27:48 +00:00
|
|
|
put_sbits(&s->pb, nbits, mant);
|
2007-05-17 16:29:11 +00:00
|
|
|
run = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output EOB only if not already 64 values */
|
|
|
|
if (last_index < 63 || run != 0)
|
|
|
|
put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
|
|
|
|
}
|
|
|
|
|
2013-01-20 00:02:29 +00:00
|
|
|
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[6][64])
|
2007-05-17 16:29:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<5;i++) {
|
|
|
|
encode_block(s, block[i], i);
|
|
|
|
}
|
|
|
|
if (s->chroma_format == CHROMA_420) {
|
|
|
|
encode_block(s, block[5], 5);
|
|
|
|
} else {
|
|
|
|
encode_block(s, block[6], 6);
|
|
|
|
encode_block(s, block[5], 5);
|
|
|
|
encode_block(s, block[7], 7);
|
|
|
|
}
|
2010-12-04 05:21:03 +00:00
|
|
|
|
|
|
|
s->i_tex_bits += get_bits_diff(s);
|
2007-05-17 16:29:11 +00:00
|
|
|
}
|
2007-05-18 22:58:01 +00:00
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodec ff_mjpeg_encoder = {
|
2011-07-17 10:54:31 +00:00
|
|
|
.name = "mjpeg",
|
2013-10-03 20:57:53 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
|
2011-07-17 10:54:31 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_MJPEG,
|
2011-07-17 10:54:31 +00:00
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
2012-02-15 12:10:33 +00:00
|
|
|
.init = ff_MPV_encode_init,
|
2012-02-10 09:30:05 +00:00
|
|
|
.encode2 = ff_MPV_encode_picture,
|
2012-02-15 12:10:33 +00:00
|
|
|
.close = ff_MPV_encode_end,
|
2012-10-06 10:10:34 +00:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){
|
|
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
|
2012-04-06 16:19:39 +00:00
|
|
|
},
|
2007-05-18 22:58:01 +00:00
|
|
|
};
|