00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "internal.h"
00029 #include "bytestream.h"
00030 #include "libavutil/lfg.h"
00031 #include "elbg.h"
00032 #include "libavutil/imgutils.h"
00036 typedef struct Msvideo1EncContext {
00037 AVCodecContext *avctx;
00038 AVFrame pic;
00039 AVLFG rnd;
00040 uint8_t *prev;
00041
00042 int block[16*3];
00043 int block2[16*3];
00044 int codebook[8*3];
00045 int codebook2[8*3];
00046 int output[16*3];
00047 int output2[16*3];
00048 int avg[3];
00049 int bestpos;
00050 int keyint;
00051 } Msvideo1EncContext;
00052
00053 enum MSV1Mode{
00054 MODE_SKIP = 0,
00055 MODE_FILL,
00056 MODE_2COL,
00057 MODE_8COL,
00058 };
00059
00060 #define SKIP_PREFIX 0x8400
00061 #define SKIPS_MAX 0x0FFF
00062 #define MKRGB555(in, off) ((in[off] << 10) | (in[off + 1] << 5) | (in[off + 2]))
00063
00064 static const int remap[16] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 };
00065
00066 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00067 const AVFrame *pict, int *got_packet)
00068 {
00069 Msvideo1EncContext * const c = avctx->priv_data;
00070 AVFrame * const p = &c->pic;
00071 uint16_t *src;
00072 uint8_t *prevptr;
00073 uint8_t *dst, *buf;
00074 int keyframe = 1;
00075 int no_skips = 1;
00076 int i, j, k, x, y, ret;
00077 int skips = 0;
00078
00079 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
00080 return ret;
00081 dst= buf= pkt->data;
00082
00083 *p = *pict;
00084 if(!c->prev)
00085 c->prev = av_malloc(avctx->width * 3 * (avctx->height + 3));
00086 prevptr = c->prev + avctx->width * 3 * (FFALIGN(avctx->height, 4) - 1);
00087 src = (uint16_t*)(p->data[0] + p->linesize[0]*(FFALIGN(avctx->height, 4) - 1));
00088 if(c->keyint >= avctx->keyint_min)
00089 keyframe = 1;
00090
00091 p->quality = 24;
00092
00093 for(y = 0; y < avctx->height; y += 4){
00094 for(x = 0; x < avctx->width; x += 4){
00095 int bestmode = MODE_SKIP;
00096 int bestscore = INT_MAX;
00097 int flags = 0;
00098 int score;
00099
00100 for(j = 0; j < 4; j++){
00101 for(i = 0; i < 4; i++){
00102 uint16_t val = src[x + i - j*p->linesize[0]/2];
00103 for(k = 0; k < 3; k++){
00104 c->block[(i + j*4)*3 + k] =
00105 c->block2[remap[i + j*4]*3 + k] = (val >> (10-k*5)) & 0x1F;
00106 }
00107 }
00108 }
00109 if(!keyframe){
00110 bestscore = 0;
00111 for(j = 0; j < 4; j++){
00112 for(i = 0; i < 4*3; i++){
00113 int t = prevptr[x*3 + i + j*p->linesize[0]] - c->block[i + j*4*3];
00114 bestscore += t*t;
00115 }
00116 }
00117 bestscore /= p->quality;
00118 }
00119
00120 score = 0;
00121 ff_init_elbg(c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
00122 ff_do_elbg (c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
00123 if(c->avg[0] == 1)
00124 c->avg[0] = 0;
00125 for(j = 0; j < 4; j++){
00126 for(i = 0; i < 4; i++){
00127 for(k = 0; k < 3; k++){
00128 int t = c->avg[k] - c->block[(i+j*4)*3+k];
00129 score += t*t;
00130 }
00131 }
00132 }
00133 score /= p->quality;
00134 score += 2;
00135 if(score < bestscore){
00136 bestscore = score;
00137 bestmode = MODE_FILL;
00138 }
00139
00140 score = 0;
00141 ff_init_elbg(c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
00142 ff_do_elbg (c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
00143
00144 if(!c->output[15]){
00145 for(i = 0; i < 3; i++)
00146 FFSWAP(uint8_t, c->codebook[i], c->codebook[i+3]);
00147 for(i = 0; i < 16; i++)
00148 c->output[i] ^= 1;
00149 }
00150 for(j = 0; j < 4; j++){
00151 for(i = 0; i < 4; i++){
00152 for(k = 0; k < 3; k++){
00153 int t = c->codebook[c->output[i+j*4]*3 + k] - c->block[i*3+k+j*4*3];
00154 score += t*t;
00155 }
00156 }
00157 }
00158 score /= p->quality;
00159 score += 6;
00160 if(score < bestscore){
00161 bestscore = score;
00162 bestmode = MODE_2COL;
00163 }
00164
00165 score = 0;
00166 for(i = 0; i < 4; i++){
00167 ff_init_elbg(c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
00168 ff_do_elbg (c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
00169 }
00170
00171 if(!c->output2[15]){
00172 for(i = 0; i < 3; i++)
00173 FFSWAP(uint8_t, c->codebook2[i+18], c->codebook2[i+21]);
00174 for(i = 12; i < 16; i++)
00175 c->output2[i] ^= 1;
00176 }
00177 for(j = 0; j < 4; j++){
00178 for(i = 0; i < 4; i++){
00179 for(k = 0; k < 3; k++){
00180 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];
00181 score += t*t;
00182 }
00183 }
00184 }
00185 score /= p->quality;
00186 score += 18;
00187 if(score < bestscore){
00188 bestscore = score;
00189 bestmode = MODE_8COL;
00190 }
00191
00192 if(bestmode == MODE_SKIP){
00193 skips++;
00194 no_skips = 0;
00195 }
00196 if((bestmode != MODE_SKIP && skips) || skips == SKIPS_MAX){
00197 bytestream_put_le16(&dst, skips | SKIP_PREFIX);
00198 skips = 0;
00199 }
00200
00201 switch(bestmode){
00202 case MODE_FILL:
00203 bytestream_put_le16(&dst, MKRGB555(c->avg,0) | 0x8000);
00204 for(j = 0; j < 4; j++)
00205 for(i = 0; i < 4; i++)
00206 for(k = 0; k < 3; k++)
00207 prevptr[i*3 + k - j*3*avctx->width] = c->avg[k];
00208 break;
00209 case MODE_2COL:
00210 for(j = 0; j < 4; j++){
00211 for(i = 0; i < 4; i++){
00212 flags |= (c->output[i + j*4]^1) << (i + j*4);
00213 for(k = 0; k < 3; k++)
00214 prevptr[i*3 + k - j*3*avctx->width] = c->codebook[c->output[i + j*4]*3 + k];
00215 }
00216 }
00217 bytestream_put_le16(&dst, flags);
00218 bytestream_put_le16(&dst, MKRGB555(c->codebook, 0));
00219 bytestream_put_le16(&dst, MKRGB555(c->codebook, 3));
00220 break;
00221 case MODE_8COL:
00222 for(j = 0; j < 4; j++){
00223 for(i = 0; i < 4; i++){
00224 flags |= (c->output2[remap[i + j*4]]^1) << (i + j*4);
00225 for(k = 0; k < 3; k++)
00226 prevptr[i*3 + k - j*3*avctx->width] = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3 + k];
00227 }
00228 }
00229 bytestream_put_le16(&dst, flags);
00230 bytestream_put_le16(&dst, MKRGB555(c->codebook2, 0) | 0x8000);
00231 for(i = 3; i < 24; i += 3)
00232 bytestream_put_le16(&dst, MKRGB555(c->codebook2, i));
00233 break;
00234 }
00235 }
00236 src -= p->linesize[0] << 1;
00237 prevptr -= avctx->width * 3 * 4;
00238 }
00239 if(skips)
00240 bytestream_put_le16(&dst, skips | SKIP_PREFIX);
00241
00242 bytestream_put_byte(&dst, 0);
00243 bytestream_put_byte(&dst, 0);
00244
00245 if(no_skips)
00246 keyframe = 1;
00247 if(keyframe)
00248 c->keyint = 0;
00249 else
00250 c->keyint++;
00251 p->pict_type= keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
00252 p->key_frame= keyframe;
00253 if (keyframe) pkt->flags |= AV_PKT_FLAG_KEY;
00254 pkt->size = dst - buf;
00255 *got_packet = 1;
00256
00257 return 0;
00258 }
00259
00260
00264 static av_cold int encode_init(AVCodecContext *avctx)
00265 {
00266 Msvideo1EncContext * const c = avctx->priv_data;
00267
00268 c->avctx = avctx;
00269 if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
00270 return -1;
00271 }
00272 if((avctx->width&3) || (avctx->height&3)){
00273 av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
00274 return -1;
00275 }
00276
00277 avcodec_get_frame_defaults(&c->pic);
00278 avctx->coded_frame = (AVFrame*)&c->pic;
00279 avctx->bits_per_coded_sample = 16;
00280
00281 c->keyint = avctx->keyint_min;
00282 av_lfg_init(&c->rnd, 1);
00283
00284 return 0;
00285 }
00286
00287
00288
00292 static av_cold int encode_end(AVCodecContext *avctx)
00293 {
00294 Msvideo1EncContext * const c = avctx->priv_data;
00295
00296 av_freep(&c->prev);
00297
00298 return 0;
00299 }
00300
00301 AVCodec ff_msvideo1_encoder = {
00302 .name = "msvideo1",
00303 .type = AVMEDIA_TYPE_VIDEO,
00304 .id = AV_CODEC_ID_MSVIDEO1,
00305 .priv_data_size = sizeof(Msvideo1EncContext),
00306 .init = encode_init,
00307 .encode2 = encode_frame,
00308 .close = encode_end,
00309 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB555, PIX_FMT_NONE},
00310 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video-1"),
00311 };