FFmpeg
msmpeg4enc.c
Go to the documentation of this file.
1 /*
2  * MSMPEG4 encoder backend
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 /**
26  * @file
27  * MSMPEG4 encoder backend
28  */
29 
30 #include <stdint.h>
31 #include <string.h>
32 
33 #define NO_SLICE_THREADING_HERE
34 
35 #include "libavutil/attributes.h"
36 #include "libavutil/avutil.h"
37 #include "libavutil/thread.h"
38 #include "codec_internal.h"
39 #include "mpegvideo.h"
40 #include "mpegvideoenc.h"
41 #include "h263.h"
42 #include "h263data.h"
43 #include "mpeg4video.h"
44 #include "msmpeg4.h"
45 #include "msmpeg4data.h"
46 #include "msmpeg4_vc1_data.h"
47 #include "msmpeg4enc.h"
48 #include "put_bits.h"
49 #include "rl.h"
50 
51 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
52 
53 // The lowest 8 bits of each entry are length, the other bits are the code.
54 // The index of the (mx, my) entry is (mx * 64) + my.
55 static uint32_t mv_vector_tables[2][4096];
56 
57 /* build the table which associate a (x,y) motion vector to a vlc */
58 static av_cold void init_mv_table(const uint16_t mv_table[], const uint8_t mv_table_lens[],
59  uint32_t mv_vector_table[4096],
60  unsigned escape_code, int escape_length)
61 {
62  for (int i = 0; i < 4096; i++) {
63  // Initialize to the table to "escaped". This code is equivalent to
64  // the following double loop (with x and y ranging over 0..63):
65  // tab[x * 64 + y] = (esc_code << 20) | (x << 14) | (y << 8) | esc_length
66  mv_vector_table[i] = (escape_code << 20) | (i << 8) | escape_length;
67  }
68 
69  for (uint32_t i = 0, code = 0; i < MSMPEG4_MV_TABLES_NB_ELEMS; i++) {
70  int sym = mv_table[i];
71  int len = mv_table_lens[i];
72  int x = sym >> 8;
73  int y = sym & 0xFF;
74  // We ignore the escape value here and restore it after the loop.
75  mv_vector_table[(x << 6) | y] = (code >> (24 - len)) | len;
76  code += 1U << (32 - len);
77  }
78  mv_vector_table[0] = (escape_code << 20) | escape_length;
79 }
80 
82 {
83  if (n == 0) {
84  put_bits(pb, 1, 0);
85  } else {
86  put_bits(pb, 2, 2 | (n >= 2));
87  }
88 }
89 
90 static int get_size_of_code(const RLTable *rl, int last, int run,
91  int level, int intra)
92 {
93  int size=0;
94  int code;
95  int run_diff= intra ? 0 : 1;
96 
97  code = get_rl_index(rl, last, run, level);
98  size+= rl->table_vlc[code][1];
99  if (code == rl->n) {
100  int level1, run1;
101 
102  level1 = level - rl->max_level[last][run];
103  if (level1 < 1)
104  goto esc2;
105  code = get_rl_index(rl, last, run, level1);
106  if (code == rl->n) {
107  esc2:
108  size++;
109  if (level > MAX_LEVEL)
110  goto esc3;
111  run1 = run - rl->max_run[last][level] - run_diff;
112  if (run1 < 0)
113  goto esc3;
114  code = get_rl_index(rl, last, run1, level);
115  if (code == rl->n) {
116  esc3:
117  /* third escape */
118  size+=1+1+6+8;
119  } else {
120  /* second escape */
121  size+= 1+1+ rl->table_vlc[code][1];
122  }
123  } else {
124  /* first escape */
125  size+= 1+1+ rl->table_vlc[code][1];
126  }
127  } else {
128  size++;
129  }
130  return size;
131 }
132 
134 {
136  mv_vector_tables[0], 0x0000, 8 + 12);
138  mv_vector_tables[1], 0x000b, 4 + 12);
139 
140  for (int i = 0; i < NB_RL_TABLES; i++) {
141  for (int level = 1; level <= MAX_LEVEL; level++) {
142  for (int run = 0; run <= MAX_RUN; run++) {
143  for (int last = 0; last < 2; last++) {
144  rl_length[i][level][run][last] = get_size_of_code(&ff_rl_table[i], last, run, level, 0);
145  }
146  }
147  }
148  }
149 }
150 
152 {
153  MPVEncContext *const s = &ms->m.s;
154  int i;
155  int best = 0, best_size = INT_MAX;
156  int chroma_best = 0, best_chroma_size = INT_MAX;
157 
158  for(i=0; i<3; i++){
159  int level;
160  int chroma_size=0;
161  int size=0;
162 
163  if(i>0){// ;)
164  size++;
165  chroma_size++;
166  }
167  for(level=0; level<=MAX_LEVEL; level++){
168  int run;
169  for(run=0; run<=MAX_RUN; run++){
170  int last;
171  const int last_size= size + chroma_size;
172  for(last=0; last<2; last++){
173  int inter_count = ms->ac_stats[0][0][level][run][last] + ms->ac_stats[0][1][level][run][last];
174  int intra_luma_count = ms->ac_stats[1][0][level][run][last];
175  int intra_chroma_count= ms->ac_stats[1][1][level][run][last];
176 
177  if (s->c.pict_type == AV_PICTURE_TYPE_I) {
178  size += intra_luma_count *rl_length[i ][level][run][last];
179  chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
180  }else{
181  size+= intra_luma_count *rl_length[i ][level][run][last]
182  +intra_chroma_count*rl_length[i+3][level][run][last]
183  +inter_count *rl_length[i+3][level][run][last];
184  }
185  }
186  if(last_size == size+chroma_size) break;
187  }
188  }
189  if(size<best_size){
190  best_size= size;
191  best= i;
192  }
193  if(chroma_size<best_chroma_size){
194  best_chroma_size= chroma_size;
195  chroma_best= i;
196  }
197  }
198 
199  if (s->c.pict_type == AV_PICTURE_TYPE_P) chroma_best = best;
200 
201  memset(ms->ac_stats, 0, sizeof(ms->ac_stats));
202 
203  ms->rl_table_index = best;
204  ms->rl_chroma_table_index = chroma_best;
205 
206  if (s->c.pict_type != ms->m.last_non_b_pict_type) {
207  ms->rl_table_index= 2;
208  if (s->c.pict_type == AV_PICTURE_TYPE_I)
209  ms->rl_chroma_table_index = 1;
210  else
211  ms->rl_chroma_table_index = 2;
212  }
213 
214 }
215 
216 /* write MSMPEG4 compatible frame header */
218 {
219  MSMPEG4EncContext *const ms = (MSMPEG4EncContext*)m;
220  MPVEncContext *const s = &m->s;
221 
222  find_best_tables(ms);
223 
224  align_put_bits(&s->pb);
225  put_bits(&s->pb, 2, s->c.pict_type - 1);
226 
227  put_bits(&s->pb, 5, s->c.qscale);
228  if (s->c.msmpeg4_version <= MSMP4_V2) {
229  ms->rl_table_index = 2;
230  ms->rl_chroma_table_index = 2;
231  }
232 
233  ms->dc_table_index = 1;
234  ms->mv_table_index = 1; /* only if P-frame */
235  ms->use_skip_mb_code = 1; /* only if P-frame */
236  ms->per_mb_rl_table = 0;
237  if (s->c.msmpeg4_version == MSMP4_WMV1)
238  s->c.inter_intra_pred = s->c.width * s->c.height < 320*240 &&
239  m->bit_rate <= II_BITRATE &&
240  s->c.pict_type == AV_PICTURE_TYPE_P;
241  ff_dlog(s->c.avctx, "%d %"PRId64" %d %d %d\n", s->c.pict_type, m->bit_rate,
242  s->c.inter_intra_pred, s->c.width, s->c.height);
243 
244  if (s->c.pict_type == AV_PICTURE_TYPE_I) {
245  s->c.slice_height = s->c.mb_height/1;
246  put_bits(&s->pb, 5, 0x16 + s->c.mb_height/s->c.slice_height);
247 
248  if (s->c.msmpeg4_version == MSMP4_WMV1) {
250  if (m->bit_rate > MBAC_BITRATE)
251  put_bits(&s->pb, 1, ms->per_mb_rl_table);
252  }
253 
254  if (s->c.msmpeg4_version > MSMP4_V2) {
255  if (!ms->per_mb_rl_table){
258  }
259 
260  put_bits(&s->pb, 1, ms->dc_table_index);
261  }
262  } else {
263  put_bits(&s->pb, 1, ms->use_skip_mb_code);
264 
265  if (s->c.msmpeg4_version == MSMP4_WMV1 && m->bit_rate > MBAC_BITRATE)
266  put_bits(&s->pb, 1, ms->per_mb_rl_table);
267 
268  if (s->c.msmpeg4_version > MSMP4_V2) {
269  if (!ms->per_mb_rl_table)
271 
272  put_bits(&s->pb, 1, ms->dc_table_index);
273 
274  put_bits(&s->pb, 1, ms->mv_table_index);
275  }
276  }
277 
278  s->esc3_level_length = 0;
279  ms->esc3_run_length = 0;
280 
281  return 0;
282 }
283 
285 {
286  const MPVMainEncContext *const m = slice_to_mainenc(s);
287  unsigned fps;
288 
289  if (s->c.avctx->framerate.num > 0 && s->c.avctx->framerate.den > 0)
290  fps = s->c.avctx->framerate.num / s->c.avctx->framerate.den;
291  else {
292  fps = s->c.avctx->time_base.den / s->c.avctx->time_base.num;
293  }
294 
295  put_bits(&s->pb, 5, FFMIN(fps, 31)); //yes 29.97 -> 29
296 
297  put_bits(&s->pb, 11, FFMIN(m->bit_rate / 1024, 2047));
298 
299  if (s->c.msmpeg4_version >= MSMP4_V3)
300  put_bits(&s->pb, 1, s->c.flipflop_rounding);
301  else
302  av_assert0(!s->c.flipflop_rounding);
303 }
304 
306  int mx, int my)
307 {
308  MPVEncContext *const s = &ms->m.s;
309  const uint32_t *const mv_vector_table = mv_vector_tables[ms->mv_table_index];
310  uint32_t code;
311 
312  /* modulo encoding */
313  /* WARNING : you cannot reach all the MVs even with the modulo
314  encoding. This is a somewhat strange compromise they took !!! */
315  if (mx <= -64)
316  mx += 64;
317  else if (mx >= 64)
318  mx -= 64;
319  if (my <= -64)
320  my += 64;
321  else if (my >= 64)
322  my -= 64;
323 
324  mx += 32;
325  my += 32;
326 
327  code = mv_vector_table[(mx << 6) | my];
328  put_bits(&s->pb, code & 0xff, code >> 8);
329 }
330 
332 {
333  if (s->c.mb_x == 0) {
334  if (s->c.slice_height && (s->c.mb_y % s->c.slice_height) == 0) {
335  if (s->c.msmpeg4_version < MSMP4_WMV1)
337  s->c.first_slice_line = 1;
338  } else {
339  s->c.first_slice_line = 0;
340  }
341  }
342 }
343 
344 static void msmpeg4v2_encode_motion(MPVEncContext *const s, int val)
345 {
346  int range, bit_size, sign, code, bits;
347 
348  if (val == 0) {
349  /* zero vector; corresponds to ff_mvtab[0] */
350  put_bits(&s->pb, 1, 0x1);
351  } else {
352  bit_size = s->c.f_code - 1;
353  range = 1 << bit_size;
354  if (val <= -64)
355  val += 64;
356  else if (val >= 64)
357  val -= 64;
358 
359  if (val >= 0) {
360  sign = 0;
361  } else {
362  val = -val;
363  sign = 1;
364  }
365  val--;
366  code = (val >> bit_size) + 1;
367  bits = val & (range - 1);
368 
369  put_bits(&s->pb, ff_mvtab[code][1] + 1, (ff_mvtab[code][0] << 1) | sign);
370  if (bit_size > 0) {
371  put_bits(&s->pb, bit_size, bits);
372  }
373  }
374 }
375 
376 static void msmpeg4_encode_mb(MPVEncContext *const s,
377  int16_t block[][64],
378  int motion_x, int motion_y)
379 {
380  MSMPEG4EncContext *const ms = mpv_to_msmpeg4(s);
381  int cbp, coded_cbp, i;
382  int pred_x, pred_y;
383 
385 
386  if (!s->c.mb_intra) {
387  /* compute cbp */
388  cbp = 0;
389  for (i = 0; i < 6; i++) {
390  if (s->c.block_last_index[i] >= 0)
391  cbp |= 1 << (5 - i);
392  }
393  if (ms->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
394  /* skip macroblock */
395  put_bits(&s->pb, 1, 1);
396  s->last_bits++;
397  s->misc_bits++;
398 
399  return;
400  }
401  if (ms->use_skip_mb_code)
402  put_bits(&s->pb, 1, 0); /* mb coded */
403 
404  if (s->c.msmpeg4_version <= MSMP4_V2) {
405  put_bits(&s->pb,
406  ff_v2_mb_type[cbp&3][1],
407  ff_v2_mb_type[cbp&3][0]);
408  if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
409  else coded_cbp= cbp;
410 
411  put_bits(&s->pb,
412  ff_h263_cbpy_tab[coded_cbp>>2][1],
413  ff_h263_cbpy_tab[coded_cbp>>2][0]);
414 
415  s->misc_bits += get_bits_diff(s);
416 
417  ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
418  msmpeg4v2_encode_motion(s, motion_x - pred_x);
419  msmpeg4v2_encode_motion(s, motion_y - pred_y);
420  }else{
421  put_bits(&s->pb,
422  ff_table_mb_non_intra[cbp + 64][1],
423  ff_table_mb_non_intra[cbp + 64][0]);
424 
425  s->misc_bits += get_bits_diff(s);
426 
427  /* motion vector */
428  ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
429  ff_msmpeg4_encode_motion(ms, motion_x - pred_x,
430  motion_y - pred_y);
431  }
432 
433  s->mv_bits += get_bits_diff(s);
434 
435  for (i = 0; i < 6; i++) {
437  }
438  s->p_tex_bits += get_bits_diff(s);
439  } else {
440  /* compute cbp */
441  cbp = 0;
442  for (int i = 0; i < 6; i++) {
443  int val = (s->c.block_last_index[i] >= 1);
444  cbp |= val << (5 - i);
445  }
446  if (s->c.msmpeg4_version <= MSMP4_V2) {
447  if (s->c.pict_type == AV_PICTURE_TYPE_I) {
448  put_bits(&s->pb,
449  ff_v2_intra_cbpc[cbp&3][1], ff_v2_intra_cbpc[cbp&3][0]);
450  } else {
451  if (ms->use_skip_mb_code)
452  put_bits(&s->pb, 1, 0); /* mb coded */
453  put_bits(&s->pb,
454  ff_v2_mb_type[(cbp&3) + 4][1],
455  ff_v2_mb_type[(cbp&3) + 4][0]);
456  }
457  put_bits(&s->pb, 1, 0); /* no AC prediction yet */
458  put_bits(&s->pb,
459  ff_h263_cbpy_tab[cbp>>2][1],
460  ff_h263_cbpy_tab[cbp>>2][0]);
461  }else{
462  if (s->c.pict_type == AV_PICTURE_TYPE_I) {
463  /* compute coded_cbp; the 0x3 corresponds to chroma cbp;
464  * luma coded_cbp are set in the loop below */
465  coded_cbp = cbp & 0x3;
466  for (int i = 0; i < 4; i++) {
467  uint8_t *coded_block;
468  int pred = ff_msmpeg4_coded_block_pred(&s->c, i, &coded_block);
469  int val = (s->c.block_last_index[i] >= 1);
470  *coded_block = val;
471  val ^= pred;
472  coded_cbp |= val << (5 - i);
473  }
474 
475  put_bits(&s->pb,
476  ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
477  } else {
478  if (ms->use_skip_mb_code)
479  put_bits(&s->pb, 1, 0); /* mb coded */
480  put_bits(&s->pb,
481  ff_table_mb_non_intra[cbp][1],
482  ff_table_mb_non_intra[cbp][0]);
483  }
484  put_bits(&s->pb, 1, 0); /* no AC prediction yet */
485  if (s->c.inter_intra_pred) {
486  s->c.h263_aic_dir = 0;
487  put_bits(&s->pb, ff_table_inter_intra[s->c.h263_aic_dir][1],
488  ff_table_inter_intra[s->c.h263_aic_dir][0]);
489  }
490  }
491  s->misc_bits += get_bits_diff(s);
492 
493  for (i = 0; i < 6; i++) {
495  }
496  s->i_tex_bits += get_bits_diff(s);
497  s->i_count++;
498  }
499 }
500 
501 static void msmpeg4_encode_dc(MSMPEG4EncContext *const ms, int level, int n, int *dir_ptr)
502 {
503  MPVEncContext *const s = &ms->m.s;
504  int sign, code;
505  int pred;
506 
507  int16_t *dc_val;
508  pred = ff_msmpeg4_pred_dc(&s->c, n, &dc_val, dir_ptr);
509 
510  /* update predictor */
511  if (n < 4) {
512  *dc_val = level * s->c.y_dc_scale;
513  } else {
514  *dc_val = level * s->c.c_dc_scale;
515  }
516 
517  /* do the prediction */
518  level -= pred;
519 
520  if (s->c.msmpeg4_version <= MSMP4_V2) {
521  if (n < 4) {
522  put_bits(&s->pb,
523  ff_v2_dc_lum_table[level + 256][1],
524  ff_v2_dc_lum_table[level + 256][0]);
525  }else{
526  put_bits(&s->pb,
527  ff_v2_dc_chroma_table[level + 256][1],
528  ff_v2_dc_chroma_table[level + 256][0]);
529  }
530  }else{
531  sign = 0;
532  if (level < 0) {
533  level = -level;
534  sign = 1;
535  }
536  code = level;
537  if (code > DC_MAX)
538  code = DC_MAX;
539 
540  put_bits(&s->pb, ff_msmp4_dc_tables[ms->dc_table_index][n >= 4][code][1],
541  ff_msmp4_dc_tables[ms->dc_table_index][n >= 4][code][0]);
542 
543  if (code == DC_MAX)
544  put_bits(&s->pb, 8, level);
545 
546  if (level != 0) {
547  put_bits(&s->pb, 1, sign);
548  }
549  }
550 }
551 
552 /* Encoding of a block; very similar to MPEG-4 except for a different
553  * escape coding (same as H.263) and more VLC tables. */
554 void ff_msmpeg4_encode_block(MPVEncContext *const s, int16_t * block, int n)
555 {
556  MSMPEG4EncContext *const ms = (MSMPEG4EncContext*)s;
557  int level, run, last, i, j, last_index;
558  int last_non_zero, sign, slevel;
559  int code, run_diff, dc_pred_dir;
560  const RLTable *rl;
561  const uint8_t *scantable;
562 
563  if (s->c.mb_intra) {
564  msmpeg4_encode_dc(ms, block[0], n, &dc_pred_dir);
565  i = 1;
566  if (n < 4) {
567  rl = &ff_rl_table[ms->rl_table_index];
568  } else {
569  rl = &ff_rl_table[3 + ms->rl_chroma_table_index];
570  }
571  run_diff = s->c.msmpeg4_version >= MSMP4_WMV1;
572  scantable = s->c.intra_scantable.permutated;
573  } else {
574  i = 0;
575  rl = &ff_rl_table[3 + ms->rl_table_index];
576  run_diff = s->c.msmpeg4_version > MSMP4_V2;
577  scantable = s->c.inter_scantable.permutated;
578  }
579 
580  /* recalculate block_last_index for M$ wmv1 */
581  if (s->c.msmpeg4_version >= MSMP4_WMV1 && s->c.block_last_index[n] > 0) {
582  for(last_index=63; last_index>=0; last_index--){
583  if(block[scantable[last_index]]) break;
584  }
585  s->c.block_last_index[n] = last_index;
586  }else
587  last_index = s->c.block_last_index[n];
588  /* AC coefs */
589  last_non_zero = i - 1;
590  for (; i <= last_index; i++) {
591  j = scantable[i];
592  level = block[j];
593  if (level) {
594  run = i - last_non_zero - 1;
595  last = (i == last_index);
596  sign = 0;
597  slevel = level;
598  if (level < 0) {
599  sign = 1;
600  level = -level;
601  }
602 
603  if(level<=MAX_LEVEL && run<=MAX_RUN){
604  ms->ac_stats[s->c.mb_intra][n>3][level][run][last]++;
605  }
606 
607  ms->ac_stats[s->c.mb_intra][n > 3][40][63][0]++; //esc3 like
608 
609  code = get_rl_index(rl, last, run, level);
610  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
611  if (code == rl->n) {
612  int level1, run1;
613 
614  level1 = level - rl->max_level[last][run];
615  if (level1 < 1)
616  goto esc2;
617  code = get_rl_index(rl, last, run, level1);
618  if (code == rl->n) {
619  esc2:
620  put_bits(&s->pb, 1, 0);
621  if (level > MAX_LEVEL)
622  goto esc3;
623  run1 = run - rl->max_run[last][level] - run_diff;
624  if (run1 < 0)
625  goto esc3;
626  code = get_rl_index(rl, last, run1+1, level);
627  if (s->c.msmpeg4_version == MSMP4_WMV1 && code == rl->n)
628  goto esc3;
629  code = get_rl_index(rl, last, run1, level);
630  if (code == rl->n) {
631  esc3:
632  /* third escape */
633  put_bits(&s->pb, 1, 0);
634  put_bits(&s->pb, 1, last);
635  if (s->c.msmpeg4_version >= MSMP4_WMV1) {
636  if (s->esc3_level_length == 0) {
637  s->esc3_level_length = 8;
638  ms->esc3_run_length = 6;
639  //ESCLVLSZ + ESCRUNSZ
640  if (s->c.qscale < 8)
641  put_bits(&s->pb, 6, 3);
642  else
643  put_bits(&s->pb, 8, 3);
644  }
645  put_bits(&s->pb, ms->esc3_run_length, run);
646  put_bits(&s->pb, 1, sign);
647  put_bits(&s->pb, s->esc3_level_length, level);
648  }else{
649  put_bits(&s->pb, 6, run);
650  put_sbits(&s->pb, 8, slevel);
651  }
652  } else {
653  /* second escape */
654  put_bits(&s->pb, 1, 1);
655  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
656  put_bits(&s->pb, 1, sign);
657  }
658  } else {
659  /* first escape */
660  put_bits(&s->pb, 1, 1);
661  put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
662  put_bits(&s->pb, 1, sign);
663  }
664  } else {
665  put_bits(&s->pb, 1, sign);
666  }
667  last_non_zero = i;
668  }
669  }
670 }
671 
673 {
674  MPVEncContext *const s = &m->s;
675  static AVOnce init_static_once = AV_ONCE_INIT;
676 
678 
679  if (s->c.msmpeg4_version <= MSMP4_WMV1) {
681  s->encode_mb = msmpeg4_encode_mb;
682  }
683 
684  if (s->c.msmpeg4_version >= MSMP4_WMV1) {
685  s->min_qcoeff = -255;
686  s->max_qcoeff = 255;
687  }
688 
689  /* init various encoding tables */
690  ff_thread_once(&init_static_once, msmpeg4_encode_init_static);
691 }
692 
694  .p.name = "msmpeg4v2",
695  CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 2"),
696  .p.type = AVMEDIA_TYPE_VIDEO,
697  .p.id = AV_CODEC_ID_MSMPEG4V2,
699  .color_ranges = AVCOL_RANGE_MPEG,
700  .p.priv_class = &ff_mpv_enc_class,
702  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
703  .priv_data_size = sizeof(MSMPEG4EncContext),
706  .close = ff_mpv_encode_end,
707 };
708 
710  .p.name = "msmpeg4",
711  CODEC_LONG_NAME("MPEG-4 part 2 Microsoft variant version 3"),
712  .p.type = AVMEDIA_TYPE_VIDEO,
713  .p.id = AV_CODEC_ID_MSMPEG4V3,
715  .color_ranges = AVCOL_RANGE_MPEG,
716  .p.priv_class = &ff_mpv_enc_class,
718  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
719  .priv_data_size = sizeof(MSMPEG4EncContext),
722  .close = ff_mpv_encode_end,
723 };
724 
726  .p.name = "wmv1",
727  CODEC_LONG_NAME("Windows Media Video 7"),
728  .p.type = AVMEDIA_TYPE_VIDEO,
729  .p.id = AV_CODEC_ID_WMV1,
731  .color_ranges = AVCOL_RANGE_MPEG,
732  .p.priv_class = &ff_mpv_enc_class,
734  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
735  .priv_data_size = sizeof(MSMPEG4EncContext),
738  .close = ff_mpv_encode_end,
739 };
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:386
ff_msmp4_dc_tables
const uint32_t ff_msmp4_dc_tables[2][2][120][2]
Definition: msmpeg4_vc1_data.c:83
MPVMainEncContext::bit_rate
int64_t bit_rate
Definition: mpegvideoenc.h:224
h263data.h
ff_mpv_enc_class
const AVClass ff_mpv_enc_class
Definition: mpegvideo_enc.c:104
level
uint8_t level
Definition: svq3.c:205
MPVEncContext
Definition: mpegvideoenc.h:45
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
align_put_bits
static void align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition: put_bits.h:435
ff_msmpeg4v3_encoder
const FFCodec ff_msmpeg4v3_encoder
Definition: msmpeg4enc.c:709
thread.h
ff_msmpeg4_encode_init
av_cold void ff_msmpeg4_encode_init(MPVMainEncContext *const m)
Definition: msmpeg4enc.c:672
mpegvideoenc.h
ff_msmpeg4_handle_slices
void ff_msmpeg4_handle_slices(MPVEncContext *const s)
Definition: msmpeg4enc.c:331
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
MSMPEG4EncContext::use_skip_mb_code
int use_skip_mb_code
Definition: msmpeg4enc.h:36
msmpeg4_encode_mb
static void msmpeg4_encode_mb(MPVEncContext *const s, int16_t block[][64], int motion_x, int motion_y)
Definition: msmpeg4enc.c:376
MBAC_BITRATE
#define MBAC_BITRATE
Definition: msmpeg4.h:30
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
ff_msmpeg4_common_init
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
Definition: msmpeg4.c:117
FFCodec
Definition: codec_internal.h:127
mpegvideo.h
ff_rl_table
RLTable ff_rl_table[NB_RL_TABLES]
Definition: msmpeg4data.c:441
MPVMainEncContext::encode_picture_header
int(* encode_picture_header)(struct MPVMainEncContext *m)
Definition: mpegvideoenc.h:221
ff_mpv_encode_picture
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
Definition: mpegvideo_enc.c:1926
mx
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition: dsp.h:53
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ff_h263_pred_motion
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition: h263.c:179
RLTable
RLTable.
Definition: rl.h:39
ff_wmv1_encoder
const FFCodec ff_wmv1_encoder
Definition: msmpeg4enc.c:725
val
static double val(void *priv, double ch)
Definition: aeval.c:77
AV_CODEC_ID_MSMPEG4V2
@ AV_CODEC_ID_MSMPEG4V2
Definition: codec_id.h:67
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:353
MSMPEG4EncContext::ac_stats
unsigned ac_stats[2][2][MAX_LEVEL+1][MAX_RUN+1][2]
[mb_intra][isChroma][level][run][last]
Definition: msmpeg4enc.h:41
ff_msmp4_mv_table1_lens
const uint8_t ff_msmp4_mv_table1_lens[MSMPEG4_MV_TABLES_NB_ELEMS]
Definition: msmpeg4data.c:928
NB_RL_TABLES
#define NB_RL_TABLES
Definition: msmpeg4data.h:40
RLTable::n
int n
number of entries of table_vlc minus 1
Definition: rl.h:40
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
av_cold
#define av_cold
Definition: attributes.h:90
msmpeg4data.h
RLTable::max_level
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:46
s
#define s(width, name)
Definition: cbs_vp9.c:198
msmpeg4v2_encode_motion
static void msmpeg4v2_encode_motion(MPVEncContext *const s, int val)
Definition: msmpeg4enc.c:344
get_rl_index
static int get_rl_index(const RLTable *rl, int last, int run, int level)
Definition: rl.h:101
MSMPEG4EncContext::esc3_run_length
int esc3_run_length
Definition: msmpeg4enc.h:38
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
bits
uint8_t bits
Definition: vp3data.h:128
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
find_best_tables
static void find_best_tables(MSMPEG4EncContext *ms)
Definition: msmpeg4enc.c:151
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
mpv_to_msmpeg4
static MSMP4DecContext * mpv_to_msmpeg4(MpegEncContext *s)
Definition: msmpeg4dec.h:44
PutBitContext
Definition: put_bits.h:50
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition: dsp.h:53
MSMPEG4EncContext::per_mb_rl_table
int per_mb_rl_table
Definition: msmpeg4enc.h:37
ff_msmp4_mv_table0_lens
const uint8_t ff_msmp4_mv_table0_lens[MSMPEG4_MV_TABLES_NB_ELEMS]
Definition: msmpeg4data.c:674
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
run
uint8_t run
Definition: svq3.c:204
RLTable::table_vlc
const uint16_t(* table_vlc)[2]
Definition: rl.h:42
ff_table_mb_non_intra
const uint32_t ff_table_mb_non_intra[128][2]
Definition: msmpeg4data.c:39
MSMPEG4EncContext::dc_table_index
int dc_table_index
Definition: msmpeg4enc.h:35
ff_v2_mb_type
const uint8_t ff_v2_mb_type[8][2]
Definition: msmpeg4data.c:993
init_mv_table
static av_cold void init_mv_table(const uint16_t mv_table[], const uint8_t mv_table_lens[], uint32_t mv_vector_table[4096], unsigned escape_code, int escape_length)
Definition: msmpeg4enc.c:58
ff_v2_dc_lum_table
uint32_t ff_v2_dc_lum_table[512][2]
Definition: msmpeg4data.c:35
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
msmpeg4_encode_picture_header
static int msmpeg4_encode_picture_header(MPVMainEncContext *const m)
Definition: msmpeg4enc.c:217
ff_msmp4_mb_i_table
const uint16_t ff_msmp4_mb_i_table[64][2]
Definition: msmpeg4_vc1_data.c:63
ff_v2_dc_chroma_table
uint32_t ff_v2_dc_chroma_table[512][2]
Definition: msmpeg4data.c:36
ff_mpv_encode_end
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
Definition: mpegvideo_enc.c:1104
DC_MAX
#define DC_MAX
Definition: msmpeg4.h:32
AV_CODEC_ID_WMV1
@ AV_CODEC_ID_WMV1
Definition: codec_id.h:69
MPVMainEncContext
Definition: mpegvideoenc.h:172
ff_msmpeg4v2_encoder
const FFCodec ff_msmpeg4v2_encoder
Definition: msmpeg4enc.c:693
AVOnce
#define AVOnce
Definition: thread.h:202
ff_msmp4_mv_table0
const uint16_t ff_msmp4_mv_table0[MSMPEG4_MV_TABLES_NB_ELEMS]
The entries are of the form (8 << mvx) | mvy. Escape value is zero.
Definition: msmpeg4data.c:487
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
MSMPEG4EncContext
Definition: msmpeg4enc.h:29
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ff_mpeg4_clean_buffers
void ff_mpeg4_clean_buffers(MpegEncContext *s)
Definition: mpeg4video.c:57
codec_internal.h
ff_h263_cbpy_tab
const uint8_t ff_h263_cbpy_tab[16][2]
Definition: h263data.c:82
size
int size
Definition: twinvq_data.h:10344
rl_length
static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2]
Definition: msmpeg4enc.c:51
II_BITRATE
#define II_BITRATE
Definition: msmpeg4.h:29
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
ff_msmp4_mv_table1
const uint16_t ff_msmp4_mv_table1[MSMPEG4_MV_TABLES_NB_ELEMS]
Definition: msmpeg4data.c:741
ff_msmpeg4_pred_dc
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n, int16_t **dc_val_ptr, int *dir_ptr)
Definition: msmpeg4.c:196
ff_v2_intra_cbpc
const uint8_t ff_v2_intra_cbpc[4][2]
Definition: msmpeg4data.c:998
attributes.h
ff_msmpeg4_encode_motion
void ff_msmpeg4_encode_motion(MSMPEG4EncContext *const ms, int mx, int my)
Definition: msmpeg4enc.c:305
get_bits_diff
static int get_bits_diff(MPVEncContext *s)
Definition: mpegvideoenc.h:382
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
get_size_of_code
static int get_size_of_code(const RLTable *rl, int last, int run, int level, int intra)
Definition: msmpeg4enc.c:90
RLTable::max_run
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:47
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
ff_msmpeg4_encode_ext_header
void ff_msmpeg4_encode_ext_header(MPVEncContext *const s)
Definition: msmpeg4enc.c:284
len
int len
Definition: vorbis_enc_data.h:426
MPVMainEncContext::last_non_b_pict_type
int last_non_b_pict_type
used for MPEG-4 gmc B-frames & ratecontrol
Definition: mpegvideoenc.h:233
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:733
MSMPEG4EncContext::rl_table_index
int rl_table_index
Definition: msmpeg4enc.h:33
mv_vector_tables
static uint32_t mv_vector_tables[2][4096]
Definition: msmpeg4enc.c:55
msmpeg4.h
pred
static const float pred[4]
Definition: siprdata.h:259
ff_mvtab
const uint8_t ff_mvtab[33][2]
Definition: h263data.c:88
U
#define U(x)
Definition: vpx_arith.h:37
msmpeg4_encode_init_static
static av_cold void msmpeg4_encode_init_static(void)
Definition: msmpeg4enc.c:133
ff_msmpeg4_code012
void ff_msmpeg4_code012(PutBitContext *pb, int n)
Definition: msmpeg4enc.c:81
mpeg4video.h
ff_table_inter_intra
const uint8_t ff_table_inter_intra[4][2]
Definition: msmpeg4data.c:1017
MSMPEG4EncContext::mv_table_index
int mv_table_index
Definition: msmpeg4enc.h:32
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avutil.h
msmpeg4enc.h
msmpeg4_vc1_data.h
ff_mpv_encode_init
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
Definition: mpegvideo_enc.c:544
ff_msmpeg4_encode_block
void ff_msmpeg4_encode_block(MPVEncContext *const s, int16_t *block, int n)
Definition: msmpeg4enc.c:554
slice_to_mainenc
static const MPVMainEncContext * slice_to_mainenc(const MPVEncContext *s)
Definition: mpegvideoenc.h:247
rl.h
MSMPEG4_MV_TABLES_NB_ELEMS
#define MSMPEG4_MV_TABLES_NB_ELEMS
Definition: msmpeg4data.h:51
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
AV_CODEC_ID_MSMPEG4V3
@ AV_CODEC_ID_MSMPEG4V3
Definition: codec_id.h:68
msmpeg4_encode_dc
static void msmpeg4_encode_dc(MSMPEG4EncContext *const ms, int level, int n, int *dir_ptr)
Definition: msmpeg4enc.c:501
put_bits.h
MPVMainEncContext::s
MPVEncContext s
The main slicecontext.
Definition: mpegvideoenc.h:173
MSMPEG4EncContext::rl_chroma_table_index
int rl_chroma_table_index
Definition: msmpeg4enc.h:34
ff_msmpeg4_coded_block_pred
int ff_msmpeg4_coded_block_pred(MpegEncContext *s, int n, uint8_t **coded_block_ptr)
Definition: msmpeg4.c:156
MSMPEG4EncContext::m
MPVMainEncContext m
Definition: msmpeg4enc.h:30
h263.h