2007-05-17 18:29:11 +02:00
|
|
|
/*
|
|
|
|
* MJPEG encoder
|
2009-01-19 16:46:40 +01:00
|
|
|
* Copyright (c) 2000, 2001 Fabrice Bellard
|
2007-05-17 18:29:11 +02:00
|
|
|
* Copyright (c) 2003 Alex Beregszaszi
|
|
|
|
* Copyright (c) 2003-2004 Michael Niedermayer
|
|
|
|
*
|
2007-07-05 12:37:29 +02: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 18:29:11 +02: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 16:45:34 +02:00
|
|
|
* @file
|
2007-05-17 18:29:11 +02:00
|
|
|
* MJPEG encoder.
|
|
|
|
*/
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
|
2007-05-17 18:29:11 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "mpegvideo.h"
|
|
|
|
#include "mjpeg.h"
|
2007-05-19 00:42:49 +02:00
|
|
|
#include "mjpegenc.h"
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
|
|
|
MJpegContext *m;
|
|
|
|
|
2012-05-31 13:49:00 +02: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 10:54:32 +01:00
|
|
|
return AVERROR(EINVAL);
|
2012-05-31 13:49:00 +02:00
|
|
|
}
|
|
|
|
|
2007-05-17 18:29:11 +02:00
|
|
|
m = av_malloc(sizeof(MJpegContext));
|
|
|
|
if (!m)
|
2013-12-29 10:54:32 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
s->min_qcoeff=-1023;
|
|
|
|
s->max_qcoeff= 1023;
|
|
|
|
|
|
|
|
/* build all the huffman tables */
|
2007-05-17 18:45:55 +02:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
|
|
|
|
m->huff_code_dc_luminance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_bits_dc_luminance,
|
|
|
|
avpriv_mjpeg_val_dc);
|
2007-05-17 18:45:55 +02:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
|
|
|
|
m->huff_code_dc_chrominance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_bits_dc_chrominance,
|
|
|
|
avpriv_mjpeg_val_dc);
|
2007-05-17 18:45:55 +02:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
|
|
|
|
m->huff_code_ac_luminance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_bits_ac_luminance,
|
|
|
|
avpriv_mjpeg_val_ac_luminance);
|
2007-05-17 18:45:55 +02:00
|
|
|
ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
|
|
|
|
m->huff_code_ac_chrominance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_bits_ac_chrominance,
|
|
|
|
avpriv_mjpeg_val_ac_chrominance);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
s->mjpeg_ctx = m;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-19 00:54:54 +02:00
|
|
|
void ff_mjpeg_encode_close(MpegEncContext *s)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
|
|
|
av_free(s->mjpeg_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* table_class: 0 = DC coef, 1 = AC coefs */
|
2013-12-01 21:35:02 +01:00
|
|
|
static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
|
2007-05-17 18:29:11 +02:00
|
|
|
const uint8_t *bits_table, const uint8_t *value_table)
|
|
|
|
{
|
|
|
|
int n, i;
|
|
|
|
|
|
|
|
put_bits(p, 4, table_class);
|
|
|
|
put_bits(p, 4, table_id);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for(i=1;i<=16;i++) {
|
|
|
|
n += bits_table[i];
|
|
|
|
put_bits(p, 8, bits_table[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
put_bits(p, 8, value_table[i]);
|
|
|
|
|
|
|
|
return n + 17;
|
|
|
|
}
|
|
|
|
|
2013-12-06 10:14:15 +01:00
|
|
|
static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
|
|
|
|
ScanTable *intra_scantable,
|
2014-01-11 21:26:51 +01:00
|
|
|
uint16_t luma_intra_matrix[64],
|
|
|
|
uint16_t chroma_intra_matrix[64],
|
2013-12-06 10:14:15 +01:00
|
|
|
int hsample[3])
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
|
|
|
int i, j, size;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
2013-12-06 10:14:15 +01:00
|
|
|
if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
|
2014-01-11 21:45:41 +01:00
|
|
|
int matrix_count = 1 + !!memcmp(luma_intra_matrix,
|
|
|
|
chroma_intra_matrix,
|
|
|
|
sizeof(luma_intra_matrix[0]) * 64);
|
2007-05-17 18:29:11 +02:00
|
|
|
/* quant matrixes */
|
|
|
|
put_marker(p, DQT);
|
2014-01-11 21:45:41 +01:00
|
|
|
put_bits(p, 16, 2 + matrix_count * (1 + 64));
|
2007-05-17 18:29:11 +02:00
|
|
|
put_bits(p, 4, 0); /* 8 bit precision */
|
|
|
|
put_bits(p, 4, 0); /* table 0 */
|
|
|
|
for(i=0;i<64;i++) {
|
2013-12-01 21:45:40 +01:00
|
|
|
j = intra_scantable->permutated[i];
|
2014-01-11 21:26:51 +01:00
|
|
|
put_bits(p, 8, luma_intra_matrix[j]);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
2014-01-11 21:45:41 +01:00
|
|
|
|
|
|
|
if (matrix_count > 1) {
|
|
|
|
put_bits(p, 4, 0); /* 8 bit precision */
|
|
|
|
put_bits(p, 4, 1); /* table 1 */
|
|
|
|
for(i=0;i<64;i++) {
|
|
|
|
j = intra_scantable->permutated[i];
|
|
|
|
put_bits(p, 8, chroma_intra_matrix[j]);
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 01:02:51 +02:00
|
|
|
}
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-06 10:14:15 +01:00
|
|
|
if(avctx->active_thread_type & FF_THREAD_SLICE){
|
2012-06-14 16:26:15 +02:00
|
|
|
put_marker(p, DRI);
|
|
|
|
put_bits(p, 16, 4);
|
2013-12-06 10:14:15 +01:00
|
|
|
put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
|
2012-06-14 16:26:15 +02:00
|
|
|
}
|
|
|
|
|
2007-05-17 18:29:11 +02:00
|
|
|
/* huffman table */
|
|
|
|
put_marker(p, DHT);
|
|
|
|
flush_put_bits(p);
|
2009-04-13 18:59:38 +02:00
|
|
|
ptr = put_bits_ptr(p);
|
2007-05-17 18:29:11 +02:00
|
|
|
put_bits(p, 16, 0); /* patched later */
|
|
|
|
size = 2;
|
2013-12-01 21:35:02 +01:00
|
|
|
size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_val_dc);
|
2013-12-01 21:35:02 +01:00
|
|
|
size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_val_dc);
|
|
|
|
|
2013-12-01 21:35:02 +01:00
|
|
|
size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_val_ac_luminance);
|
2013-12-01 21:35:02 +01:00
|
|
|
size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
|
2012-09-06 15:50:20 +02:00
|
|
|
avpriv_mjpeg_val_ac_chrominance);
|
2007-06-02 03:41:07 +02:00
|
|
|
AV_WB16(ptr, size);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 21:31:25 +01:00
|
|
|
static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
2013-12-01 21:33:47 +01:00
|
|
|
if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
|
|
|
|
/* JFIF header */
|
|
|
|
put_marker(p, APP0);
|
|
|
|
put_bits(p, 16, 16);
|
|
|
|
avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
|
2013-12-06 04:25:56 +01:00
|
|
|
put_bits(p, 16, 0x0102); /* v 1.02 */
|
2013-12-01 21:33:47 +01:00
|
|
|
put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
|
|
|
|
put_bits(p, 16, avctx->sample_aspect_ratio.num);
|
|
|
|
put_bits(p, 16, avctx->sample_aspect_ratio.den);
|
|
|
|
put_bits(p, 8, 0); /* thumbnail width */
|
|
|
|
put_bits(p, 8, 0); /* thumbnail height */
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* comment */
|
2013-12-01 21:31:25 +01:00
|
|
|
if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
|
2007-05-17 18:29:11 +02:00
|
|
|
put_marker(p, COM);
|
|
|
|
flush_put_bits(p);
|
2009-04-13 18:59:38 +02:00
|
|
|
ptr = put_bits_ptr(p);
|
2007-05-17 18:29:11 +02:00
|
|
|
put_bits(p, 16, 0); /* patched later */
|
2012-09-09 15:03:52 +02:00
|
|
|
avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
|
2007-05-17 18:29:11 +02:00
|
|
|
size = strlen(LIBAVCODEC_IDENT)+3;
|
2007-06-02 03:41:07 +02:00
|
|
|
AV_WB16(ptr, size);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 21:33:47 +01:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
|
2007-05-17 18:29:11 +02:00
|
|
|
put_marker(p, COM);
|
|
|
|
flush_put_bits(p);
|
2009-04-13 18:59:38 +02:00
|
|
|
ptr = put_bits_ptr(p);
|
2007-05-17 18:29:11 +02:00
|
|
|
put_bits(p, 16, 0); /* patched later */
|
2012-09-09 15:03:52 +02:00
|
|
|
avpriv_put_string(p, "CS=ITU601", 1);
|
2007-05-17 18:29:11 +02:00
|
|
|
size = strlen("CS=ITU601")+3;
|
2007-06-02 03:41:07 +02:00
|
|
|
AV_WB16(ptr, size);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 14:24:06 +01:00
|
|
|
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
2013-12-01 22:05:18 +01:00
|
|
|
int chroma_h_shift, chroma_v_shift;
|
|
|
|
|
|
|
|
av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
|
|
|
|
&chroma_v_shift);
|
|
|
|
if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
|
2013-12-06 13:48:20 +01:00
|
|
|
( avctx->pix_fmt == AV_PIX_FMT_BGR0
|
2013-12-06 12:21:31 +01:00
|
|
|
|| avctx->pix_fmt == AV_PIX_FMT_BGRA
|
|
|
|
|| avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
|
2013-12-01 22:05:18 +01:00
|
|
|
vsample[0] = hsample[0] =
|
|
|
|
vsample[1] = hsample[1] =
|
|
|
|
vsample[2] = hsample[2] = 1;
|
2013-12-06 11:04:27 +01:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
|
|
|
|
vsample[0] = vsample[1] = vsample[2] = 2;
|
|
|
|
hsample[0] = hsample[1] = hsample[2] = 1;
|
2013-12-01 22:05:18 +01:00
|
|
|
} else {
|
|
|
|
vsample[0] = 2;
|
|
|
|
vsample[1] = 2 >> chroma_v_shift;
|
|
|
|
vsample[2] = 2 >> chroma_v_shift;
|
|
|
|
hsample[0] = 2;
|
|
|
|
hsample[1] = 2 >> chroma_h_shift;
|
|
|
|
hsample[2] = 2 >> chroma_h_shift;
|
|
|
|
}
|
2013-12-06 14:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
|
|
|
|
ScanTable *intra_scantable,
|
2014-01-11 21:26:51 +01:00
|
|
|
uint16_t luma_intra_matrix[64],
|
|
|
|
uint16_t chroma_intra_matrix[64])
|
2013-12-06 14:24:06 +01:00
|
|
|
{
|
|
|
|
const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
|
|
|
|
int hsample[3], vsample[3];
|
|
|
|
int i;
|
2014-01-11 21:45:41 +01:00
|
|
|
int chroma_matrix = !!memcmp(luma_intra_matrix,
|
|
|
|
chroma_intra_matrix,
|
|
|
|
sizeof(luma_intra_matrix[0])*64);
|
2013-12-06 14:24:06 +01:00
|
|
|
|
|
|
|
ff_mjpeg_init_hvsample(avctx, hsample, vsample);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_marker(pb, SOI);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2011-09-25 02:54:20 +02:00
|
|
|
// hack for AMV mjpeg format
|
2013-12-06 11:04:27 +01:00
|
|
|
if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
|
2011-09-25 02:54:20 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
jpeg_put_comments(avctx, pb);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2014-01-11 21:26:51 +01:00
|
|
|
jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
switch (avctx->codec_id) {
|
|
|
|
case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
|
|
|
|
case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
|
2012-06-20 18:20:04 +02:00
|
|
|
default: av_assert0(0);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 16, 17);
|
2013-12-06 11:04:27 +01:00
|
|
|
if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
|
|
|
|
|| avctx->pix_fmt == AV_PIX_FMT_BGRA
|
|
|
|
|| avctx->pix_fmt == AV_PIX_FMT_BGR24))
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 9); /* 9 bits/component RCT */
|
2007-05-17 18:29:11 +02:00
|
|
|
else
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 8); /* 8 bits/component */
|
|
|
|
put_bits(pb, 16, avctx->height);
|
|
|
|
put_bits(pb, 16, avctx->width);
|
|
|
|
put_bits(pb, 8, 3); /* 3 components */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* Y component */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 1); /* component number */
|
|
|
|
put_bits(pb, 4, hsample[0]); /* H factor */
|
|
|
|
put_bits(pb, 4, vsample[0]); /* V factor */
|
|
|
|
put_bits(pb, 8, 0); /* select matrix */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* Cb component */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 2); /* component number */
|
|
|
|
put_bits(pb, 4, hsample[1]); /* H factor */
|
|
|
|
put_bits(pb, 4, vsample[1]); /* V factor */
|
2014-01-11 21:45:41 +01:00
|
|
|
put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* Cr component */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 3); /* component number */
|
|
|
|
put_bits(pb, 4, hsample[2]); /* H factor */
|
|
|
|
put_bits(pb, 4, vsample[2]); /* V factor */
|
2014-01-11 21:45:41 +01:00
|
|
|
put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* scan header */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_marker(pb, SOS);
|
|
|
|
put_bits(pb, 16, 12); /* length */
|
|
|
|
put_bits(pb, 8, 3); /* 3 components */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* Y component */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 1); /* index */
|
|
|
|
put_bits(pb, 4, 0); /* DC huffman table index */
|
|
|
|
put_bits(pb, 4, 0); /* AC huffman table index */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* Cb component */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 2); /* index */
|
|
|
|
put_bits(pb, 4, 1); /* DC huffman table index */
|
|
|
|
put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
/* Cr component */
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 3); /* index */
|
|
|
|
put_bits(pb, 4, 1); /* DC huffman table index */
|
|
|
|
put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
switch (avctx->codec_id) {
|
|
|
|
case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
|
|
|
|
case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
|
2012-06-20 18:20:04 +02:00
|
|
|
default: av_assert0(0);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, 8, 0); /* Ah/Al (not used) */
|
2012-06-14 16:26:15 +02:00
|
|
|
|
|
|
|
end:
|
2013-12-06 13:23:17 +01:00
|
|
|
if (!lossless) {
|
2013-12-06 12:21:31 +01:00
|
|
|
MpegEncContext *s = avctx->priv_data;
|
|
|
|
av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
|
|
|
|
|
|
|
|
s->esc_pos = put_bits_count(pb) >> 3;
|
|
|
|
for(i=1; i<s->slice_context_count; i++)
|
|
|
|
s->thread_context[i]->esc_pos = 0;
|
|
|
|
}
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 11:49:53 +01:00
|
|
|
void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
2013-12-06 11:55:23 +01:00
|
|
|
int size;
|
2007-05-17 18:29:11 +02:00
|
|
|
int i, ff_count;
|
2013-12-01 22:05:18 +01:00
|
|
|
uint8_t *buf = pb->buf + start;
|
2007-05-17 18:29:11 +02:00
|
|
|
int align= (-(size_t)(buf))&3;
|
2013-12-06 11:55:23 +01:00
|
|
|
int pad = (-put_bits_count(pb))&7;
|
|
|
|
|
|
|
|
if (pad)
|
|
|
|
put_bits(pb, pad, (1<<pad)-1);
|
|
|
|
|
|
|
|
flush_put_bits(pb);
|
|
|
|
size = put_bits_count(pb) - start * 8;
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2012-06-20 18:20:04 +02:00
|
|
|
av_assert1((size&7) == 0);
|
2007-05-17 18:29:11 +02:00
|
|
|
size >>= 3;
|
|
|
|
|
|
|
|
ff_count=0;
|
|
|
|
for(i=0; i<size && i<align; i++){
|
|
|
|
if(buf[i]==0xFF) ff_count++;
|
|
|
|
}
|
|
|
|
for(; i<size-15; i+=16){
|
|
|
|
int acc, v;
|
|
|
|
|
|
|
|
v= *(uint32_t*)(&buf[i]);
|
|
|
|
acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
|
|
v= *(uint32_t*)(&buf[i+4]);
|
|
|
|
acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
|
|
v= *(uint32_t*)(&buf[i+8]);
|
|
|
|
acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
|
|
v= *(uint32_t*)(&buf[i+12]);
|
|
|
|
acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
|
|
|
|
|
|
|
|
acc>>=4;
|
|
|
|
acc+= (acc>>16);
|
|
|
|
acc+= (acc>>8);
|
|
|
|
ff_count+= acc&0xFF;
|
|
|
|
}
|
|
|
|
for(; i<size; i++){
|
|
|
|
if(buf[i]==0xFF) ff_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ff_count==0) return;
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
flush_put_bits(pb);
|
|
|
|
skip_put_bytes(pb, ff_count);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
|
|
|
for(i=size-1; ff_count; i--){
|
|
|
|
int v= buf[i];
|
|
|
|
|
|
|
|
if(v==0xFF){
|
|
|
|
buf[i+ff_count]= 0;
|
|
|
|
ff_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[i+ff_count]= v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:26:15 +02:00
|
|
|
void ff_mjpeg_encode_stuffing(MpegEncContext *s)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
2013-12-06 14:19:11 +01:00
|
|
|
int i;
|
2012-06-14 16:26:15 +02:00
|
|
|
PutBitContext *pbc = &s->pb;
|
|
|
|
int mb_y = s->mb_y - !s->mb_x;
|
|
|
|
|
2013-12-06 11:55:23 +01:00
|
|
|
ff_mjpeg_escape_FF(pbc, s->esc_pos);
|
2012-06-14 16:26:15 +02:00
|
|
|
|
|
|
|
if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
|
|
|
|
put_marker(pbc, RST0 + (mb_y&7));
|
|
|
|
s->esc_pos = put_bits_count(pbc) >> 3;
|
|
|
|
|
|
|
|
for(i=0; i<3; i++)
|
|
|
|
s->last_dc[i] = 128 << s->intra_dc_precision;
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
2013-12-06 11:33:07 +01:00
|
|
|
av_assert1((header_bits & 7) == 0);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_marker(pb, EOI);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
|
2007-05-19 00:54:54 +02:00
|
|
|
uint8_t *huff_size, uint16_t *huff_code)
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
|
|
|
int mant, nbits;
|
|
|
|
|
|
|
|
if (val == 0) {
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, huff_size[0], huff_code[0]);
|
2007-05-17 18:29:11 +02:00
|
|
|
} else {
|
|
|
|
mant = val;
|
|
|
|
if (val < 0) {
|
|
|
|
val = -val;
|
|
|
|
mant--;
|
|
|
|
}
|
|
|
|
|
|
|
|
nbits= av_log2_16bit(val) + 1;
|
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_bits(pb, huff_size[nbits], huff_code[nbits]);
|
2007-05-17 18:29:11 +02:00
|
|
|
|
2013-12-01 22:05:18 +01:00
|
|
|
put_sbits(pb, nbits, mant);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 01:02:29 +01:00
|
|
|
static void encode_block(MpegEncContext *s, int16_t *block, int n)
|
2007-05-17 18:29:11 +02: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 22:05:18 +01:00
|
|
|
ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
|
2007-05-17 18:29:11 +02:00
|
|
|
huff_size_ac = m->huff_size_ac_luminance;
|
|
|
|
huff_code_ac = m->huff_code_ac_luminance;
|
|
|
|
} else {
|
2013-12-01 22:05:18 +01:00
|
|
|
ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
|
2007-05-17 18:29:11 +02: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 14:08:06 +01:00
|
|
|
nbits= av_log2_16bit(val) + 1;
|
2007-05-17 18:29:11 +02:00
|
|
|
code = (run << 4) | nbits;
|
|
|
|
|
|
|
|
put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
|
|
|
|
|
2008-07-13 16:27:48 +02:00
|
|
|
put_sbits(&s->pb, nbits, mant);
|
2007-05-17 18:29:11 +02: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 02:06:37 +02:00
|
|
|
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64])
|
2007-05-17 18:29:11 +02:00
|
|
|
{
|
|
|
|
int i;
|
2012-12-16 14:42:31 +01: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-07 22:44:27 +01:00
|
|
|
encode_block(s, block[5], 5);
|
2012-12-16 14:42:31 +01:00
|
|
|
encode_block(s, block[9], 9);
|
|
|
|
|
2012-12-18 00:46:31 +01: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 14:42:31 +01: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 18:29:11 +02:00
|
|
|
}
|
2010-12-04 06:21:03 +01:00
|
|
|
|
|
|
|
s->i_tex_bits += get_bits_diff(s);
|
2007-05-17 18:29:11 +02:00
|
|
|
}
|
2007-05-19 00:58:01 +02:00
|
|
|
|
2011-09-25 02:54:20 +02:00
|
|
|
// maximum over s->mjpeg_vsample[i]
|
|
|
|
#define V_MAX 2
|
2012-02-20 00:47:46 +01:00
|
|
|
static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
|
2012-03-07 00:40:39 +01:00
|
|
|
const AVFrame *pic_arg, int *got_packet)
|
2011-09-25 02:54:20 +02:00
|
|
|
|
2012-02-20 00:47:46 +01:00
|
|
|
{
|
2011-09-25 02:54:20 +02:00
|
|
|
MpegEncContext *s = avctx->priv_data;
|
2013-12-17 16:27:36 +01:00
|
|
|
AVFrame *pic;
|
|
|
|
int i, ret;
|
2013-12-06 12:21:31 +01: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 02:54:20 +02:00
|
|
|
|
|
|
|
//CODEC_FLAG_EMU_EDGE have to be cleared
|
|
|
|
if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
|
2013-12-29 10:54:32 +01:00
|
|
|
return AVERROR(EINVAL);
|
2011-09-25 02:54:20 +02:00
|
|
|
|
2014-01-24 03:24:55 +01:00
|
|
|
pic = av_frame_clone(pic_arg);
|
2013-12-29 10:52:19 +01:00
|
|
|
if (!pic)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-09-25 02:54:20 +02:00
|
|
|
//picture should be flipped upside-down
|
|
|
|
for(i=0; i < 3; i++) {
|
2013-12-06 12:21:31 +01:00
|
|
|
int vsample = i ? 2 >> chroma_v_shift : 2;
|
2013-12-17 16:27:36 +01:00
|
|
|
pic->data[i] += (pic->linesize[i] * (vsample * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
|
|
|
|
pic->linesize[i] *= -1;
|
2011-09-25 02:54:20 +02:00
|
|
|
}
|
2013-12-17 16:27:36 +01:00
|
|
|
ret = ff_MPV_encode_picture(avctx, pkt, pic, got_packet);
|
|
|
|
av_frame_free(&pic);
|
|
|
|
return ret;
|
2011-09-25 02:54:20 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 14:40:28 +02:00
|
|
|
#if CONFIG_MJPEG_ENCODER
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_mjpeg_encoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "mjpeg",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
|
2011-07-17 12:54:31 +02:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_MJPEG,
|
2011-07-17 12:54:31 +02:00
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
2012-02-15 13:10:33 +01:00
|
|
|
.init = ff_MPV_encode_init,
|
2012-02-10 10:30:05 +01:00
|
|
|
.encode2 = ff_MPV_encode_picture,
|
2012-02-15 13:10:33 +01:00
|
|
|
.close = ff_MPV_encode_end,
|
2012-06-14 21:20:23 +02:00
|
|
|
.capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
|
2012-10-06 12:10:34 +02:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){
|
2012-11-07 22:44:27 +01:00
|
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
|
2012-04-06 18:19:39 +02:00
|
|
|
},
|
2007-05-19 00:58:01 +02:00
|
|
|
};
|
2012-06-11 14:40:28 +02:00
|
|
|
#endif
|
|
|
|
#if CONFIG_AMV_ENCODER
|
2011-09-25 02:54:20 +02:00
|
|
|
AVCodec ff_amv_encoder = {
|
2011-09-26 00:24:38 +02:00
|
|
|
.name = "amv",
|
2013-10-04 15:33:20 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
|
2011-09-26 00:24:38 +02:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-07 22:45:46 +02:00
|
|
|
.id = AV_CODEC_ID_AMV,
|
2011-09-26 00:24:38 +02:00
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
2012-02-16 00:49:16 +01:00
|
|
|
.init = ff_MPV_encode_init,
|
2012-02-20 00:47:46 +01:00
|
|
|
.encode2 = amv_encode_picture,
|
2012-02-16 00:49:16 +01:00
|
|
|
.close = ff_MPV_encode_end,
|
2012-10-08 20:54:00 +02:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){
|
|
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_NONE
|
2012-06-12 22:41:18 +02:00
|
|
|
},
|
2011-09-25 02:54:20 +02:00
|
|
|
};
|
2012-06-11 14:40:28 +02:00
|
|
|
#endif
|