2007-05-17 19:29:11 +03:00
|
|
|
/*
|
|
|
|
* MJPEG encoder
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2000, 2001 Fabrice Bellard
|
2007-05-17 19:29:11 +03:00
|
|
|
* Copyright (c) 2003 Alex Beregszaszi
|
|
|
|
* Copyright (c) 2003-2004 Michael Niedermayer
|
|
|
|
*
|
2007-07-05 13:37:29 +03:00
|
|
|
* Support for external huffman table, various fixes (AVID workaround),
|
|
|
|
* aspecting, new decode_frame mechanism and apple mjpeg-b support
|
|
|
|
* by Alex Beregszaszi
|
|
|
|
*
|
2007-05-17 19:29:11 +03:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2007-05-17 19:29:11 +03:00
|
|
|
* MJPEG encoder.
|
|
|
|
*/
|
|
|
|
|
2013-12-02 00:05:18 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
|
2007-05-17 19:29:11 +03:00
|
|
|
#include "avcodec.h"
|
2014-06-30 15:37:59 +03:00
|
|
|
#include "mjpegenc_common.h"
|
2007-05-17 19:29:11 +03:00
|
|
|
#include "mpegvideo.h"
|
|
|
|
#include "mjpeg.h"
|
2007-05-19 01:42:49 +03:00
|
|
|
#include "mjpegenc.h"
|
2007-05-17 19:29:11 +03:00
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
|
2007-05-17 19:29:11 +03:00
|
|
|
{
|
|
|
|
MJpegContext *m;
|
|
|
|
|
2012-05-31 14:49:00 +03:00
|
|
|
if (s->width > 65500 || s->height > 65500) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
|
2013-12-29 12:54:32 +03:00
|
|
|
return AVERROR(EINVAL);
|
2012-05-31 14:49:00 +03:00
|
|
|
}
|
|
|
|
|
2007-05-17 19:29:11 +03:00
|
|
|
m = av_malloc(sizeof(MJpegContext));
|
|
|
|
if (!m)
|
2013-12-29 12:54:32 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2007-05-17 19:29:11 +03:00
|
|
|
|
|
|
|
s->min_qcoeff=-1023;
|
|
|
|
s->max_qcoeff= 1023;
|
|
|
|
|
|
|
|
/* build all the huffman tables */
|
2007-05-17 19:45:55 +03:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
|
|
|
|
m->huff_code_dc_luminance,
|
2012-09-06 16:50:20 +03:00
|
|
|
avpriv_mjpeg_bits_dc_luminance,
|
|
|
|
avpriv_mjpeg_val_dc);
|
2007-05-17 19:45:55 +03:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
|
|
|
|
m->huff_code_dc_chrominance,
|
2012-09-06 16:50:20 +03:00
|
|
|
avpriv_mjpeg_bits_dc_chrominance,
|
|
|
|
avpriv_mjpeg_val_dc);
|
2007-05-17 19:45:55 +03:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
|
|
|
|
m->huff_code_ac_luminance,
|
2012-09-06 16:50:20 +03:00
|
|
|
avpriv_mjpeg_bits_ac_luminance,
|
|
|
|
avpriv_mjpeg_val_ac_luminance);
|
2007-05-17 19:45:55 +03:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
|
|
|
|
m->huff_code_ac_chrominance,
|
2012-09-06 16:50:20 +03:00
|
|
|
avpriv_mjpeg_bits_ac_chrominance,
|
|
|
|
avpriv_mjpeg_val_ac_chrominance);
|
2007-05-17 19:29:11 +03:00
|
|
|
|
|
|
|
s->mjpeg_ctx = m;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-19 01:54:54 +03:00
|
|
|
void ff_mjpeg_encode_close(MpegEncContext *s)
|
2007-05-17 19:29:11 +03:00
|
|
|
{
|
2014-10-31 14:36:19 +02:00
|
|
|
av_freep(&s->mjpeg_ctx);
|
2007-05-17 19:29:11 +03:00
|
|
|
}
|
|
|
|
|
2013-01-20 03:02:29 +03:00
|
|
|
static void encode_block(MpegEncContext *s, int16_t *block, int n)
|
2007-05-17 19:29:11 +03: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-02 00:05:18 +03:00
|
|
|
ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
|
2007-05-17 19:29:11 +03:00
|
|
|
huff_size_ac = m->huff_size_ac_luminance;
|
|
|
|
huff_code_ac = m->huff_code_ac_luminance;
|
|
|
|
} else {
|
2013-12-02 00:05:18 +03:00
|
|
|
ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
|
2007-05-17 19:29:11 +03: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--;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:08:06 +03:00
|
|
|
nbits= av_log2_16bit(val) + 1;
|
2007-05-17 19:29:11 +03:00
|
|
|
code = (run << 4) | nbits;
|
|
|
|
|
|
|
|
put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
|
|
|
|
|
2008-07-13 17:27:48 +03:00
|
|
|
put_sbits(&s->pb, nbits, mant);
|
2007-05-17 19:29:11 +03: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]);
|
|
|
|
}
|
|
|
|
|
2014-04-16 03:06:37 +03:00
|
|
|
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
|
2007-05-17 19:29:11 +03:00
|
|
|
{
|
|
|
|
int i;
|
2012-12-16 16:42:31 +03:00
|
|
|
if (s->chroma_format == CHROMA_444) {
|
|
|
|
encode_block(s, block[0], 0);
|
|
|
|
encode_block(s, block[2], 2);
|
|
|
|
encode_block(s, block[4], 4);
|
|
|
|
encode_block(s, block[8], 8);
|
2012-11-08 00:44:27 +03:00
|
|
|
encode_block(s, block[5], 5);
|
2012-12-16 16:42:31 +03:00
|
|
|
encode_block(s, block[9], 9);
|
|
|
|
|
2012-12-18 02:46:31 +03:00
|
|
|
if (16*s->mb_x+8 < s->width) {
|
|
|
|
encode_block(s, block[1], 1);
|
|
|
|
encode_block(s, block[3], 3);
|
|
|
|
encode_block(s, block[6], 6);
|
|
|
|
encode_block(s, block[10], 10);
|
|
|
|
encode_block(s, block[7], 7);
|
|
|
|
encode_block(s, block[11], 11);
|
|
|
|
}
|
2012-12-16 16:42:31 +03:00
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
2007-05-17 19:29:11 +03:00
|
|
|
}
|
2010-12-04 07:21:03 +02:00
|
|
|
|
|
|
|
s->i_tex_bits += get_bits_diff(s);
|
2007-05-17 19:29:11 +03:00
|
|
|
}
|
2007-05-19 01:58:01 +03:00
|
|
|
|
2011-09-25 03:54:20 +03:00
|
|
|
// maximum over s->mjpeg_vsample[i]
|
|
|
|
#define V_MAX 2
|
2012-02-20 02:47:46 +03:00
|
|
|
static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
|
2012-03-07 02:40:39 +03:00
|
|
|
const AVFrame *pic_arg, int *got_packet)
|
2011-09-25 03:54:20 +03:00
|
|
|
|
2012-02-20 02:47:46 +03:00
|
|
|
{
|
2011-09-25 03:54:20 +03:00
|
|
|
MpegEncContext *s = avctx->priv_data;
|
2013-12-17 18:27:36 +03:00
|
|
|
AVFrame *pic;
|
|
|
|
int i, ret;
|
2013-12-06 14:21:31 +03:00
|
|
|
int chroma_h_shift, chroma_v_shift;
|
|
|
|
|
|
|
|
av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
|
2011-09-25 03:54:20 +03:00
|
|
|
|
|
|
|
//CODEC_FLAG_EMU_EDGE have to be cleared
|
|
|
|
if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
|
2013-12-29 12:54:32 +03:00
|
|
|
return AVERROR(EINVAL);
|
2011-09-25 03:54:20 +03:00
|
|
|
|
2014-06-01 20:53:04 +03:00
|
|
|
if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
|
2014-06-01 04:57:17 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
2014-06-01 20:53:04 +03:00
|
|
|
"Heights which are not a multiple of 16 might fail with some decoders, "
|
|
|
|
"use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
|
|
|
|
"with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
|
|
|
|
return AVERROR_EXPERIMENTAL;
|
2014-06-01 04:57:17 +03:00
|
|
|
}
|
|
|
|
|
2014-01-24 05:24:55 +03:00
|
|
|
pic = av_frame_clone(pic_arg);
|
2013-12-29 12:52:19 +03:00
|
|
|
if (!pic)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-09-25 03:54:20 +03:00
|
|
|
//picture should be flipped upside-down
|
|
|
|
for(i=0; i < 3; i++) {
|
2013-12-06 14:21:31 +03:00
|
|
|
int vsample = i ? 2 >> chroma_v_shift : 2;
|
2012-04-29 00:37:08 +03:00
|
|
|
pic->data[i] += pic->linesize[i] * (vsample * s->height / V_MAX - 1);
|
2013-12-17 18:27:36 +03:00
|
|
|
pic->linesize[i] *= -1;
|
2011-09-25 03:54:20 +03:00
|
|
|
}
|
2014-08-15 21:00:34 +03:00
|
|
|
ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
|
2013-12-17 18:27:36 +03:00
|
|
|
av_frame_free(&pic);
|
|
|
|
return ret;
|
2011-09-25 03:54:20 +03:00
|
|
|
}
|
|
|
|
|
2012-06-11 15:40:28 +03:00
|
|
|
#if CONFIG_MJPEG_ENCODER
|
2014-10-18 20:13:20 +03:00
|
|
|
FF_MPV_GENERIC_CLASS(mjpeg)
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_mjpeg_encoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "mjpeg",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
|
2011-07-17 13:54:31 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_MJPEG,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
2014-08-10 18:25:12 +03:00
|
|
|
.init = ff_mpv_encode_init,
|
|
|
|
.encode2 = ff_mpv_encode_picture,
|
|
|
|
.close = ff_mpv_encode_end,
|
2012-06-14 22:20:23 +03:00
|
|
|
.capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
|
2012-10-06 13:10:34 +03:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){
|
2012-11-08 00:44:27 +03:00
|
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
|
2012-04-06 19:19:39 +03:00
|
|
|
},
|
2014-10-18 20:13:20 +03:00
|
|
|
.priv_class = &mjpeg_class,
|
2007-05-19 01:58:01 +03:00
|
|
|
};
|
2012-06-11 15:40:28 +03:00
|
|
|
#endif
|
|
|
|
#if CONFIG_AMV_ENCODER
|
2014-10-18 20:13:20 +03:00
|
|
|
FF_MPV_GENERIC_CLASS(amv)
|
|
|
|
|
2011-09-25 03:54:20 +03:00
|
|
|
AVCodec ff_amv_encoder = {
|
2011-09-26 01:24:38 +03:00
|
|
|
.name = "amv",
|
2013-10-04 16:33:20 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
|
2011-09-26 01:24:38 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-07 23:45:46 +03:00
|
|
|
.id = AV_CODEC_ID_AMV,
|
2011-09-26 01:24:38 +03:00
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
2014-08-15 21:00:34 +03:00
|
|
|
.init = ff_mpv_encode_init,
|
2012-02-20 02:47:46 +03:00
|
|
|
.encode2 = amv_encode_picture,
|
2014-08-15 21:00:34 +03:00
|
|
|
.close = ff_mpv_encode_end,
|
2012-10-08 21:54:00 +03:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){
|
2014-09-14 18:09:38 +03:00
|
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
|
2012-06-12 23:41:18 +03:00
|
|
|
},
|
2014-10-18 20:13:20 +03:00
|
|
|
.priv_class = &amv_class,
|
2011-09-25 03:54:20 +03:00
|
|
|
};
|
2012-06-11 15:40:28 +03:00
|
|
|
#endif
|