FFmpeg  4.4.9
msvideo1enc.c
Go to the documentation of this file.
1 /*
2  * Microsoft Video-1 Encoder
3  * Copyright (c) 2009 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Microsoft Video-1 encoder
25  */
26 
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "bytestream.h"
30 #include "libavutil/lfg.h"
31 #include "elbg.h"
32 #include "libavutil/imgutils.h"
33 /**
34  * Encoder context
35  */
36 typedef struct Msvideo1EncContext {
40 
41  int block[16*3];
42  int block2[16*3];
43  int codebook[8*3];
44  int codebook2[8*3];
45  int output[16*3];
46  int output2[16*3];
47  int avg[3];
48  int bestpos;
49  int keyint;
51 
52 enum MSV1Mode{
53  MODE_SKIP = 0,
57 };
58 
59 #define SKIP_PREFIX 0x8400
60 #define SKIPS_MAX 0x03FF
61 #define MKRGB555(in, off) (((in)[off] << 10) | ((in)[(off) + 1] << 5) | ((in)[(off) + 2]))
62 
63 static const int remap[16] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 };
64 
66  const AVFrame *pict, int *got_packet)
67 {
68  Msvideo1EncContext * const c = avctx->priv_data;
69  const AVFrame *p = pict;
70  uint16_t *src;
71  uint8_t *prevptr;
72  uint8_t *dst, *buf;
73  int keyframe = 0;
74  int no_skips = 1;
75  int i, j, k, x, y, ret;
76  int skips = 0;
77  int quality = 24;
78 
79  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
80  return ret;
81  dst= buf= pkt->data;
82 
83  if(!c->prev)
84  c->prev = av_malloc(avctx->width * 3 * (avctx->height + 3));
85  if (!c->prev)
86  return AVERROR(ENOMEM);
87  prevptr = c->prev + avctx->width * 3 * (FFALIGN(avctx->height, 4) - 1);
88  src = (uint16_t*)(p->data[0] + p->linesize[0]*(FFALIGN(avctx->height, 4) - 1));
89  if(c->keyint >= avctx->keyint_min)
90  keyframe = 1;
91 
92 
93  for(y = 0; y < avctx->height; y += 4){
94  for(x = 0; x < avctx->width; x += 4){
95  int bestmode = MODE_SKIP;
96  int bestscore = INT_MAX;
97  int flags = 0;
98  int score;
99 
100  for(j = 0; j < 4; j++){
101  for(i = 0; i < 4; i++){
102  uint16_t val = src[x + i - j*p->linesize[0]/2];
103  for(k = 0; k < 3; k++){
104  c->block[(i + j*4)*3 + k] =
105  c->block2[remap[i + j*4]*3 + k] = (val >> (10-k*5)) & 0x1F;
106  }
107  }
108  }
109  if(!keyframe){
110  bestscore = 0;
111  for(j = 0; j < 4; j++){
112  for(i = 0; i < 4*3; i++){
113  int t = prevptr[x*3 + i - j*3*avctx->width] - c->block[i + j*4*3];
114  bestscore += t*t;
115  }
116  }
117  bestscore /= quality;
118  }
119  // try to find optimal value to fill whole 4x4 block
120  score = 0;
121  avpriv_init_elbg(c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
122  avpriv_do_elbg (c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
123  if(c->avg[0] == 1) // red component = 1 will be written as skip code
124  c->avg[0] = 0;
125  for(j = 0; j < 4; j++){
126  for(i = 0; i < 4; i++){
127  for(k = 0; k < 3; k++){
128  int t = c->avg[k] - c->block[(i+j*4)*3+k];
129  score += t*t;
130  }
131  }
132  }
133  score /= quality;
134  score += 2;
135  if(score < bestscore){
136  bestscore = score;
137  bestmode = MODE_FILL;
138  }
139  // search for optimal filling of 2-color block
140  score = 0;
141  avpriv_init_elbg(c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
142  avpriv_do_elbg (c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
143  // last output value should be always 1, swap codebooks if needed
144  if(!c->output[15]){
145  for(i = 0; i < 3; i++)
146  FFSWAP(uint8_t, c->codebook[i], c->codebook[i+3]);
147  for(i = 0; i < 16; i++)
148  c->output[i] ^= 1;
149  }
150  for(j = 0; j < 4; j++){
151  for(i = 0; i < 4; i++){
152  for(k = 0; k < 3; k++){
153  int t = c->codebook[c->output[i+j*4]*3 + k] - c->block[i*3+k+j*4*3];
154  score += t*t;
155  }
156  }
157  }
158  score /= quality;
159  score += 6;
160  if(score < bestscore){
161  bestscore = score;
162  bestmode = MODE_2COL;
163  }
164  // search for optimal filling of 2-color 2x2 subblocks
165  score = 0;
166  for(i = 0; i < 4; i++){
167  avpriv_init_elbg(c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
168  avpriv_do_elbg (c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
169  }
170  // last value should be always 1, swap codebooks if needed
171  if(!c->output2[15]){
172  for(i = 0; i < 3; i++)
173  FFSWAP(uint8_t, c->codebook2[i+18], c->codebook2[i+21]);
174  for(i = 12; i < 16; i++)
175  c->output2[i] ^= 1;
176  }
177  for(j = 0; j < 4; j++){
178  for(i = 0; i < 4; i++){
179  for(k = 0; k < 3; k++){
180  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];
181  score += t*t;
182  }
183  }
184  }
185  score /= quality;
186  score += 18;
187  if(score < bestscore){
188  bestscore = score;
189  bestmode = MODE_8COL;
190  }
191 
192  if(bestmode == MODE_SKIP){
193  skips++;
194  no_skips = 0;
195  }
196  if((bestmode != MODE_SKIP && skips) || skips == SKIPS_MAX){
197  bytestream_put_le16(&dst, skips | SKIP_PREFIX);
198  skips = 0;
199  }
200 
201  switch(bestmode){
202  case MODE_FILL:
203  bytestream_put_le16(&dst, MKRGB555(c->avg,0) | 0x8000);
204  for(j = 0; j < 4; j++)
205  for(i = 0; i < 4; i++)
206  for(k = 0; k < 3; k++)
207  prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->avg[k];
208  break;
209  case MODE_2COL:
210  for(j = 0; j < 4; j++){
211  for(i = 0; i < 4; i++){
212  flags |= (c->output[i + j*4]^1) << (i + j*4);
213  for(k = 0; k < 3; k++)
214  prevptr[x*3 + i*3 + k - j*3*avctx->width] = c->codebook[c->output[i + j*4]*3 + k];
215  }
216  }
217  bytestream_put_le16(&dst, flags);
218  bytestream_put_le16(&dst, MKRGB555(c->codebook, 0));
219  bytestream_put_le16(&dst, MKRGB555(c->codebook, 3));
220  break;
221  case MODE_8COL:
222  for(j = 0; j < 4; j++){
223  for(i = 0; i < 4; i++){
224  flags |= (c->output2[remap[i + j*4]]^1) << (i + j*4);
225  for(k = 0; k < 3; k++)
226  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];
227  }
228  }
229  bytestream_put_le16(&dst, flags);
230  bytestream_put_le16(&dst, MKRGB555(c->codebook2, 0) | 0x8000);
231  for(i = 3; i < 24; i += 3)
232  bytestream_put_le16(&dst, MKRGB555(c->codebook2, i));
233  break;
234  }
235  }
236  src -= p->linesize[0] << 1;
237  prevptr -= avctx->width * 3 * 4;
238  }
239  if(skips)
240  bytestream_put_le16(&dst, skips | SKIP_PREFIX);
241  //EOF
242  bytestream_put_byte(&dst, 0);
243  bytestream_put_byte(&dst, 0);
244 
245  if(no_skips)
246  keyframe = 1;
247  if(keyframe)
248  c->keyint = 0;
249  else
250  c->keyint++;
251  if (keyframe) pkt->flags |= AV_PKT_FLAG_KEY;
252  pkt->size = dst - buf;
253  *got_packet = 1;
254 
255  return 0;
256 }
257 
258 
259 /**
260  * init encoder
261  */
263 {
264  Msvideo1EncContext * const c = avctx->priv_data;
265 
266  c->avctx = avctx;
267  if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
268  return -1;
269  }
270  if((avctx->width&3) || (avctx->height&3)){
271  av_log(avctx, AV_LOG_ERROR, "width and height must be multiples of 4\n");
272  return -1;
273  }
274 
275  avctx->bits_per_coded_sample = 16;
276 
277  c->keyint = avctx->keyint_min;
278  av_lfg_init(&c->rnd, 1);
279 
280  return 0;
281 }
282 
283 
284 
285 /**
286  * Uninit encoder
287  */
289 {
290  Msvideo1EncContext * const c = avctx->priv_data;
291 
292  av_freep(&c->prev);
293 
294  return 0;
295 }
296 
298  .name = "msvideo1",
299  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video-1"),
300  .type = AVMEDIA_TYPE_VIDEO,
301  .id = AV_CODEC_ID_MSVIDEO1,
302  .priv_data_size = sizeof(Msvideo1EncContext),
303  .init = encode_init,
304  .encode2 = encode_frame,
305  .close = encode_end,
307 };
static double val(void *priv, double ch)
Definition: aeval.c:76
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define FFSWAP(type, a, b)
Definition: common.h:108
int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Initialize the **codebook vector for the elbg algorithm.
Definition: elbg.c:337
int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Implementation of the Enhanced LBG Algorithm Based on the paper "Neural Networks 14:1219-1237" that c...
Definition: elbg.c:371
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:34
@ AV_CODEC_ID_MSVIDEO1
Definition: codec_id.h:95
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:222
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:320
misc image utilities
int i
Definition: input.c:407
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define FFALIGN(x, a)
Definition: macros.h:48
AVCodec ff_msvideo1_encoder
Definition: msvideo1enc.c:297
static const int remap[16]
Definition: msvideo1enc.c:63
static av_cold int encode_init(AVCodecContext *avctx)
init encoder
Definition: msvideo1enc.c:262
#define MKRGB555(in, off)
Definition: msvideo1enc.c:61
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: msvideo1enc.c:65
MSV1Mode
Definition: msvideo1enc.c:52
@ MODE_8COL
Definition: msvideo1enc.c:56
@ MODE_FILL
Definition: msvideo1enc.c:54
@ MODE_2COL
Definition: msvideo1enc.c:55
@ MODE_SKIP
Definition: msvideo1enc.c:53
static av_cold int encode_end(AVCodecContext *avctx)
Uninit encoder.
Definition: msvideo1enc.c:288
#define SKIPS_MAX
Definition: msvideo1enc.c:60
#define SKIP_PREFIX
Definition: msvideo1enc.c:59
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:387
main external API structure.
Definition: avcodec.h:536
int width
picture width / height.
Definition: avcodec.h:709
int keyint_min
minimum GOP size
Definition: avcodec.h:1117
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1744
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Encoder context.
Definition: msvideo1enc.c:36
int block[16 *3]
Definition: msvideo1enc.c:41
int block2[16 *3]
Definition: msvideo1enc.c:42
AVCodecContext * avctx
Definition: msvideo1enc.c:37
int codebook[8 *3]
Definition: msvideo1enc.c:43
int output[16 *3]
Definition: msvideo1enc.c:45
int codebook2[8 *3]
Definition: msvideo1enc.c:44
int output2[16 *3]
Definition: msvideo1enc.c:46
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
static double c[64]