You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
9.9 KiB

/*
* Microsoft Video-1 Encoder
* Copyright (c) 2009 Konstantin Shishkov
*
* 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
*/
/**
* @file
* Microsoft Video-1 encoder
*/
#include "avcodec.h"
#include "internal.h"
#include "bytestream.h"
#include "libavutil/lfg.h"
#include "elbg.h"
#include "libavutil/imgutils.h"
/**
* Encoder context
*/
typedef struct Msvideo1EncContext {
AVCodecContext *avctx;
AVLFG rnd;
uint8_t *prev;
int block[16*3];
int block2[16*3];
int codebook[8*3];
int codebook2[8*3];
int output[16*3];
int output2[16*3];
int avg[3];
int bestpos;
int keyint;
} Msvideo1EncContext;
enum MSV1Mode{
MODE_SKIP = 0,
MODE_FILL,
MODE_2COL,
MODE_8COL,
};
#define SKIP_PREFIX 0x8400
#define SKIPS_MAX 0x03FF
#define MKRGB555(in, off) (((in)[off] << 10) | ((in)[(off) + 1] << 5) | ((in)[(off) + 2]))
static const int remap[16] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 };
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet)
{
Msvideo1EncContext * const c = avctx->priv_data;
const AVFrame *p = pict;
uint16_t *src;
uint8_t *prevptr;
uint8_t *dst, *buf;
int keyframe = 0;
int no_skips = 1;
int i, j, k, x, y, ret;
int skips = 0;
int quality = 24;
Merge commit '059a934806d61f7af9ab3fd9f74994b838ea5eba' * commit '059a934806d61f7af9ab3fd9f74994b838ea5eba': lavc: Consistently prefix input buffer defines Conflicts: doc/examples/decoding_encoding.c libavcodec/4xm.c libavcodec/aac_adtstoasc_bsf.c libavcodec/aacdec.c libavcodec/aacenc.c libavcodec/ac3dec.h libavcodec/asvenc.c libavcodec/avcodec.h libavcodec/avpacket.c libavcodec/dvdec.c libavcodec/ffv1enc.c libavcodec/g2meet.c libavcodec/gif.c libavcodec/h264.c libavcodec/h264_mp4toannexb_bsf.c libavcodec/huffyuvdec.c libavcodec/huffyuvenc.c libavcodec/jpeglsenc.c libavcodec/libxvid.c libavcodec/mdec.c libavcodec/motionpixels.c libavcodec/mpeg4videodec.c libavcodec/mpegvideo.c libavcodec/noise_bsf.c libavcodec/nuv.c libavcodec/nvenc.c libavcodec/options.c libavcodec/parser.c libavcodec/pngenc.c libavcodec/proresenc_kostya.c libavcodec/qsvdec.c libavcodec/svq1enc.c libavcodec/tiffenc.c libavcodec/truemotion2.c libavcodec/utils.c libavcodec/utvideoenc.c libavcodec/vc1dec.c libavcodec/wmalosslessdec.c libavformat/adxdec.c libavformat/aiffdec.c libavformat/apc.c libavformat/apetag.c libavformat/avidec.c libavformat/bink.c libavformat/cafdec.c libavformat/flvdec.c libavformat/id3v2.c libavformat/isom.c libavformat/matroskadec.c libavformat/mov.c libavformat/mpc.c libavformat/mpc8.c libavformat/mpegts.c libavformat/mvi.c libavformat/mxfdec.c libavformat/mxg.c libavformat/nutdec.c libavformat/oggdec.c libavformat/oggparsecelt.c libavformat/oggparseflac.c libavformat/oggparseopus.c libavformat/oggparsespeex.c libavformat/omadec.c libavformat/rawdec.c libavformat/riffdec.c libavformat/rl2.c libavformat/rmdec.c libavformat/rtpdec_latm.c libavformat/rtpdec_mpeg4.c libavformat/rtpdec_qdm2.c libavformat/rtpdec_svq3.c libavformat/sierravmd.c libavformat/smacker.c libavformat/smush.c libavformat/spdifenc.c libavformat/takdec.c libavformat/tta.c libavformat/utils.c libavformat/vqf.c libavformat/westwood_vqa.c libavformat/xmv.c libavformat/xwma.c libavformat/yop.c Merged-by: Michael Niedermayer <michael@niedermayer.cc>
10 years ago
if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
return ret;
dst= buf= pkt->data;
if(!c->prev)
c->prev = av_malloc(avctx->width * 3 * (avctx->height + 3));
prevptr = c->prev + avctx->width * 3 * (FFALIGN(avctx->height, 4) - 1);
src = (uint16_t*)(p->data[0] + p->linesize[0]*(FFALIGN(avctx->height, 4) - 1));
if(c->keyint >= avctx->keyint_min)
keyframe = 1;
for(y = 0; y < avctx->height; y += 4){
for(x = 0; x < avctx->width; x += 4){
int bestmode = MODE_SKIP;
int bestscore = INT_MAX;
int flags = 0;
int score;
for(j = 0; j < 4; j++){
for(i = 0; i < 4; i++){
uint16_t val = src[x + i - j*p->linesize[0]/2];
for(k = 0; k < 3; k++){
c->block[(i + j*4)*3 + k] =
c->block2[remap[i + j*4]*3 + k] = (val >> (10-k*5)) & 0x1F;
}
}
}
if(!keyframe){
bestscore = 0;
for(j = 0; j < 4; j++){
for(i = 0; i < 4*3; i++){
int t = prevptr[x*3 + i - j*3*avctx->width] - c->block[i + j*4*3];
bestscore += t*t;
}
}
bestscore /= quality;
}
// try to find optimal value to fill whole 4x4 block
score = 0;
avpriv_init_elbg(c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
avpriv_do_elbg (c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
if(c->avg[0] == 1) // red component = 1 will be written as skip code
c->avg[0] = 0;
for(j = 0; j < 4; j++){
for(i = 0; i < 4; i++){
for(k = 0; k < 3; k++){
int t = c->avg[k] - c->block[(i+j*4)*3+k];
score += t*t;
}
}
}
score /= quality;
score += 2;
if(score < bestscore){
bestscore = score;
bestmode = MODE_FILL;
}
// search for optimal filling of 2-color block
score = 0;
avpriv_init_elbg(c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
avpriv_do_elbg (c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
// last output value should be always 1, swap codebooks if needed
if(!c->output[15]){
for(i = 0; i < 3; i++)
FFSWAP(uint8_t, c->codebook[i], c->codebook[i+3]);
for(i = 0; i < 16; i++)
c->output[i] ^= 1;
}
for(j = 0; j < 4; j++){
for(i = 0; i < 4; i++){
for(k = 0; k < 3; k++){
int t = c->codebook[c->output[i+j*4]*3 + k] - c->block[i*3+k+j*4*3];
score += t*t;
}
}
}
score /= quality;
score += 6;
if(score < bestscore){
bestscore = score;
bestmode = MODE_2COL;
}
// search for optimal filling of 2-color 2x2 subblocks
score = 0;
for(i = 0; i < 4; i++){
avpriv_init_elbg(c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
avpriv_do_elbg (c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
}
// last value should be always 1, swap codebooks if needed
if(!c->output2[15]){
for(i = 0; i < 3; i++)
FFSWAP(uint8_t, c->codebook2[i+18], c->codebook2[i+21]);
for(i = 12; i < 16; i++)
c->output2[i] ^= 1;
}
for(j = 0; j < 4; j++){
for(i = 0; i < 4; i++){
for(k = 0; k < 3; k++){
int t = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3+k] - c->block[i*3+k + j*4*3];
score += t*t;
}
}
}
score /= quality;
score += 18;
if(score < bestscore){
bestscore = score;
bestmode = MODE_8COL;
}
if(bestmode == MODE_SKIP){
skips++;
no_skips = 0;
}
if((bestmode != MODE_SKIP && skips) || skips == SKIPS_MAX){
bytestream_put_le16(&dst, skips | SKIP_PREFIX);
skips = 0;
}
switch(bestmode){
case MODE_FILL:
bytestream_put_le16(&dst, MKRGB555(c->avg,0) | 0x8000);
for(j = 0; j < 4; j++)
for(i = 0; i < 4; i++)
for(k = 0; k < 3; k++)
prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->avg[k];
break;
case MODE_2COL:
for(j = 0; j < 4; j++){
for(i = 0; i < 4; i++){
flags |= (c->output[i + j*4]^1) << (i + j*4);
for(k = 0; k < 3; k++)
prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->codebook[c->output[i + j*4]*3 + k];
}
}
bytestream_put_le16(&dst, flags);
bytestream_put_le16(&dst, MKRGB555(c->codebook, 0));
bytestream_put_le16(&dst, MKRGB555(c->codebook, 3));
break;
case MODE_8COL:
for(j = 0; j < 4; j++){
for(i = 0; i < 4; i++){
flags |= (c->output2[remap[i + j*4]]^1) << (i + j*4);
for(k = 0; k < 3; k++)
prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3 + k];
}
}
bytestream_put_le16(&dst, flags);
bytestream_put_le16(&dst, MKRGB555(c->codebook2, 0) | 0x8000);
for(i = 3; i < 24; i += 3)
bytestream_put_le16(&dst, MKRGB555(c->codebook2, i));
break;
}
}
src -= p->linesize[0] << 1;
prevptr -= avctx->width * 3 * 4;
}
if(skips)
bytestream_put_le16(&dst, skips | SKIP_PREFIX);
//EOF
bytestream_put_byte(&dst, 0);
bytestream_put_byte(&dst, 0);
if(no_skips)
keyframe = 1;
if(keyframe)
c->keyint = 0;
else
c->keyint++;
if (keyframe) pkt->flags |= AV_PKT_FLAG_KEY;
pkt->size = dst - buf;
*got_packet = 1;
return 0;
}
/**
* init encoder
*/
static av_cold int encode_init(AVCodecContext *avctx)
{
Msvideo1EncContext * const c = avctx->priv_data;
c->avctx = avctx;
if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
return -1;
}
if((avctx->width&3) || (avctx->height&3)){
av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of 4\n");
return -1;
}
avctx->bits_per_coded_sample = 16;
c->keyint = avctx->keyint_min;
av_lfg_init(&c->rnd, 1);
return 0;
}
/**
* Uninit encoder
*/
static av_cold int encode_end(AVCodecContext *avctx)
{
Msvideo1EncContext * const c = avctx->priv_data;
av_freep(&c->prev);
return 0;
}
AVCodec ff_msvideo1_encoder = {
.name = "msvideo1",
.long_name = NULL_IF_CONFIG_SMALL("Microsoft Video-1"),
.type = AVMEDIA_TYPE_VIDEO,
Merge commit '36ef5369ee9b336febc2c270f8718cec4476cb85' * commit '36ef5369ee9b336febc2c270f8718cec4476cb85': Replace all CODEC_ID_* with AV_CODEC_ID_* lavc: add AV prefix to codec ids. Conflicts: doc/APIchanges doc/examples/decoding_encoding.c doc/examples/muxing.c ffmpeg.c ffprobe.c ffserver.c libavcodec/8svx.c libavcodec/avcodec.h libavcodec/dnxhd_parser.c libavcodec/dvdsubdec.c libavcodec/error_resilience.c libavcodec/h263dec.c libavcodec/libvorbisenc.c libavcodec/mjpeg_parser.c libavcodec/mjpegenc.c libavcodec/mpeg12.c libavcodec/mpeg4videodec.c libavcodec/mpegvideo.c libavcodec/mpegvideo_enc.c libavcodec/pcm.c libavcodec/r210dec.c libavcodec/utils.c libavcodec/v210dec.c libavcodec/version.h libavdevice/alsa-audio-dec.c libavdevice/bktr.c libavdevice/v4l2.c libavformat/asfdec.c libavformat/asfenc.c libavformat/avformat.h libavformat/avidec.c libavformat/caf.c libavformat/electronicarts.c libavformat/flacdec.c libavformat/flvdec.c libavformat/flvenc.c libavformat/framecrcenc.c libavformat/img2.c libavformat/img2dec.c libavformat/img2enc.c libavformat/ipmovie.c libavformat/isom.c libavformat/matroska.c libavformat/matroskadec.c libavformat/matroskaenc.c libavformat/mov.c libavformat/movenc.c libavformat/mp3dec.c libavformat/mpeg.c libavformat/mpegts.c libavformat/mxf.c libavformat/mxfdec.c libavformat/mxfenc.c libavformat/nsvdec.c libavformat/nut.c libavformat/oggenc.c libavformat/pmpdec.c libavformat/rawdec.c libavformat/rawenc.c libavformat/riff.c libavformat/sdp.c libavformat/utils.c libavformat/vocenc.c libavformat/wtv.c libavformat/xmv.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
13 years ago
.id = AV_CODEC_ID_MSVIDEO1,
.priv_data_size = sizeof(Msvideo1EncContext),
.init = encode_init,
.encode2 = encode_frame,
.close = encode_end,
Merge commit '716d413c13981da15323c7a3821860536eefdbbb' * commit '716d413c13981da15323c7a3821860536eefdbbb': Replace PIX_FMT_* -> AV_PIX_FMT_*, PixelFormat -> AVPixelFormat Conflicts: doc/examples/muxing.c ffmpeg.h ffmpeg_filter.c ffmpeg_opt.c ffplay.c ffprobe.c libavcodec/8bps.c libavcodec/aasc.c libavcodec/aura.c libavcodec/avcodec.h libavcodec/avs.c libavcodec/bfi.c libavcodec/bmp.c libavcodec/bmpenc.c libavcodec/c93.c libavcodec/cscd.c libavcodec/cyuv.c libavcodec/dpx.c libavcodec/dpxenc.c libavcodec/eatgv.c libavcodec/escape124.c libavcodec/ffv1.c libavcodec/flashsv.c libavcodec/fraps.c libavcodec/h264.c libavcodec/huffyuv.c libavcodec/iff.c libavcodec/imgconvert.c libavcodec/indeo3.c libavcodec/kmvc.c libavcodec/libopenjpegdec.c libavcodec/libopenjpegenc.c libavcodec/libx264.c libavcodec/ljpegenc.c libavcodec/mjpegdec.c libavcodec/mjpegenc.c libavcodec/motionpixels.c libavcodec/mpeg12.c libavcodec/mpeg12enc.c libavcodec/mpeg4videodec.c libavcodec/mpegvideo_enc.c libavcodec/pamenc.c libavcodec/pcxenc.c libavcodec/pgssubdec.c libavcodec/pngdec.c libavcodec/pngenc.c libavcodec/pnm.c libavcodec/pnmdec.c libavcodec/pnmenc.c libavcodec/ptx.c libavcodec/qdrw.c libavcodec/qpeg.c libavcodec/qtrleenc.c libavcodec/raw.c libavcodec/rawdec.c libavcodec/rl2.c libavcodec/sgidec.c libavcodec/sgienc.c libavcodec/snowdec.c libavcodec/snowenc.c libavcodec/sunrast.c libavcodec/targa.c libavcodec/targaenc.c libavcodec/tiff.c libavcodec/tiffenc.c libavcodec/tmv.c libavcodec/truemotion2.c libavcodec/utils.c libavcodec/vb.c libavcodec/vp3.c libavcodec/wnv1.c libavcodec/xl.c libavcodec/xwddec.c libavcodec/xwdenc.c libavcodec/yop.c libavdevice/v4l2.c libavdevice/x11grab.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersrc.c libavfilter/drawutils.c libavfilter/formats.c libavfilter/src_movie.c libavfilter/vf_ass.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_format.c libavfilter/vf_hflip.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_transpose.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavformat/movenc.c libavformat/mxf.h libavformat/utils.c libavformat/yuv4mpeg.c libavutil/imgutils.c libavutil/pixdesc.c libswscale/input.c libswscale/output.c libswscale/swscale_internal.h libswscale/swscale_unscaled.c libswscale/utils.c libswscale/x86/swscale_template.c libswscale/x86/yuv2rgb.c libswscale/x86/yuv2rgb_template.c libswscale/yuv2rgb.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
12 years ago
.pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_RGB555, AV_PIX_FMT_NONE},
};