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.

1402 lines
50KB

  1. /*
  2. * MPEG-4 encoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/opt.h"
  25. #include "mpegutils.h"
  26. #include "mpegvideo.h"
  27. #include "h263.h"
  28. #include "mpeg4video.h"
  29. /* The uni_DCtab_* tables below contain unified bits+length tables to encode DC
  30. * differences in MPEG-4. Unified in the sense that the specification specifies
  31. * this encoding in several steps. */
  32. static uint8_t uni_DCtab_lum_len[512];
  33. static uint8_t uni_DCtab_chrom_len[512];
  34. static uint16_t uni_DCtab_lum_bits[512];
  35. static uint16_t uni_DCtab_chrom_bits[512];
  36. /* Unified encoding tables for run length encoding of coefficients.
  37. * Unified in the sense that the specification specifies the encoding in several steps. */
  38. static uint32_t uni_mpeg4_intra_rl_bits[64 * 64 * 2 * 2];
  39. static uint8_t uni_mpeg4_intra_rl_len[64 * 64 * 2 * 2];
  40. static uint32_t uni_mpeg4_inter_rl_bits[64 * 64 * 2 * 2];
  41. static uint8_t uni_mpeg4_inter_rl_len[64 * 64 * 2 * 2];
  42. //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 + (run) * 256 + (level))
  43. //#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) + (level) * 64)
  44. #define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) * 128 + (level))
  45. /* MPEG-4
  46. * inter
  47. * max level: 24/6
  48. * max run: 53/63
  49. *
  50. * intra
  51. * max level: 53/16
  52. * max run: 29/41
  53. */
  54. /**
  55. * Return the number of bits that encoding the 8x8 block in block would need.
  56. * @param[in] block_last_index last index in scantable order that refers to a non zero element in block.
  57. */
  58. static inline int get_block_rate(MpegEncContext *s, int16_t block[64],
  59. int block_last_index, uint8_t scantable[64])
  60. {
  61. int last = 0;
  62. int j;
  63. int rate = 0;
  64. for (j = 1; j <= block_last_index; j++) {
  65. const int index = scantable[j];
  66. int level = block[index];
  67. if (level) {
  68. level += 64;
  69. if ((level & (~127)) == 0) {
  70. if (j < block_last_index)
  71. rate += s->intra_ac_vlc_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
  72. else
  73. rate += s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
  74. } else
  75. rate += s->ac_esc_length;
  76. last = j;
  77. }
  78. }
  79. return rate;
  80. }
  81. /**
  82. * Restore the ac coefficients in block that have been changed by decide_ac_pred().
  83. * This function also restores s->block_last_index.
  84. * @param[in,out] block MB coefficients, these will be restored
  85. * @param[in] dir ac prediction direction for each 8x8 block
  86. * @param[out] st scantable for each 8x8 block
  87. * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
  88. */
  89. static inline void restore_ac_coeffs(MpegEncContext *s, int16_t block[6][64],
  90. const int dir[6], uint8_t *st[6],
  91. const int zigzag_last_index[6])
  92. {
  93. int i, n;
  94. memcpy(s->block_last_index, zigzag_last_index, sizeof(int) * 6);
  95. for (n = 0; n < 6; n++) {
  96. int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  97. st[n] = s->intra_scantable.permutated;
  98. if (dir[n]) {
  99. /* top prediction */
  100. for (i = 1; i < 8; i++)
  101. block[n][s->idsp.idct_permutation[i]] = ac_val[i + 8];
  102. } else {
  103. /* left prediction */
  104. for (i = 1; i < 8; i++)
  105. block[n][s->idsp.idct_permutation[i << 3]] = ac_val[i];
  106. }
  107. }
  108. }
  109. /**
  110. * Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
  111. * This function will also update s->block_last_index and s->ac_val.
  112. * @param[in,out] block MB coefficients, these will be updated if 1 is returned
  113. * @param[in] dir ac prediction direction for each 8x8 block
  114. * @param[out] st scantable for each 8x8 block
  115. * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
  116. */
  117. static inline int decide_ac_pred(MpegEncContext *s, int16_t block[6][64],
  118. const int dir[6], uint8_t *st[6],
  119. int zigzag_last_index[6])
  120. {
  121. int score = 0;
  122. int i, n;
  123. int8_t *const qscale_table = s->current_picture.qscale_table;
  124. memcpy(zigzag_last_index, s->block_last_index, sizeof(int) * 6);
  125. for (n = 0; n < 6; n++) {
  126. int16_t *ac_val, *ac_val1;
  127. score -= get_block_rate(s, block[n], s->block_last_index[n],
  128. s->intra_scantable.permutated);
  129. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  130. ac_val1 = ac_val;
  131. if (dir[n]) {
  132. const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
  133. /* top prediction */
  134. ac_val -= s->block_wrap[n] * 16;
  135. if (s->mb_y == 0 || s->qscale == qscale_table[xy] || n == 2 || n == 3) {
  136. /* same qscale */
  137. for (i = 1; i < 8; i++) {
  138. const int level = block[n][s->idsp.idct_permutation[i]];
  139. block[n][s->idsp.idct_permutation[i]] = level - ac_val[i + 8];
  140. ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
  141. ac_val1[i + 8] = level;
  142. }
  143. } else {
  144. /* different qscale, we must rescale */
  145. for (i = 1; i < 8; i++) {
  146. const int level = block[n][s->idsp.idct_permutation[i]];
  147. block[n][s->idsp.idct_permutation[i]] = level - ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
  148. ac_val1[i] = block[n][s->idsp.idct_permutation[i << 3]];
  149. ac_val1[i + 8] = level;
  150. }
  151. }
  152. st[n] = s->intra_h_scantable.permutated;
  153. } else {
  154. const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
  155. /* left prediction */
  156. ac_val -= 16;
  157. if (s->mb_x == 0 || s->qscale == qscale_table[xy] || n == 1 || n == 3) {
  158. /* same qscale */
  159. for (i = 1; i < 8; i++) {
  160. const int level = block[n][s->idsp.idct_permutation[i << 3]];
  161. block[n][s->idsp.idct_permutation[i << 3]] = level - ac_val[i];
  162. ac_val1[i] = level;
  163. ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
  164. }
  165. } else {
  166. /* different qscale, we must rescale */
  167. for (i = 1; i < 8; i++) {
  168. const int level = block[n][s->idsp.idct_permutation[i << 3]];
  169. block[n][s->idsp.idct_permutation[i << 3]] = level - ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
  170. ac_val1[i] = level;
  171. ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
  172. }
  173. }
  174. st[n] = s->intra_v_scantable.permutated;
  175. }
  176. for (i = 63; i > 0; i--) // FIXME optimize
  177. if (block[n][st[n][i]])
  178. break;
  179. s->block_last_index[n] = i;
  180. score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
  181. }
  182. if (score < 0) {
  183. return 1;
  184. } else {
  185. restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
  186. return 0;
  187. }
  188. }
  189. /**
  190. * modify mb_type & qscale so that encoding is actually possible in MPEG-4
  191. */
  192. void ff_clean_mpeg4_qscales(MpegEncContext *s)
  193. {
  194. int i;
  195. int8_t *const qscale_table = s->current_picture.qscale_table;
  196. ff_clean_h263_qscales(s);
  197. if (s->pict_type == AV_PICTURE_TYPE_B) {
  198. int odd = 0;
  199. /* ok, come on, this isn't funny anymore, there's more code for
  200. * handling this MPEG-4 mess than for the actual adaptive quantization */
  201. for (i = 0; i < s->mb_num; i++) {
  202. int mb_xy = s->mb_index2xy[i];
  203. odd += qscale_table[mb_xy] & 1;
  204. }
  205. if (2 * odd > s->mb_num)
  206. odd = 1;
  207. else
  208. odd = 0;
  209. for (i = 0; i < s->mb_num; i++) {
  210. int mb_xy = s->mb_index2xy[i];
  211. if ((qscale_table[mb_xy] & 1) != odd)
  212. qscale_table[mb_xy]++;
  213. if (qscale_table[mb_xy] > 31)
  214. qscale_table[mb_xy] = 31;
  215. }
  216. for (i = 1; i < s->mb_num; i++) {
  217. int mb_xy = s->mb_index2xy[i];
  218. if (qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i - 1]] &&
  219. (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_DIRECT)) {
  220. s->mb_type[mb_xy] |= CANDIDATE_MB_TYPE_BIDIR;
  221. }
  222. }
  223. }
  224. }
  225. /**
  226. * Encode the dc value.
  227. * @param n block index (0-3 are luma, 4-5 are chroma)
  228. */
  229. static inline void mpeg4_encode_dc(PutBitContext *s, int level, int n)
  230. {
  231. /* DC will overflow if level is outside the [-255,255] range. */
  232. level += 256;
  233. if (n < 4) {
  234. /* luminance */
  235. put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
  236. } else {
  237. /* chrominance */
  238. put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
  239. }
  240. }
  241. static inline int mpeg4_get_dc_length(int level, int n)
  242. {
  243. if (n < 4)
  244. return uni_DCtab_lum_len[level + 256];
  245. else
  246. return uni_DCtab_chrom_len[level + 256];
  247. }
  248. /**
  249. * Encode an 8x8 block.
  250. * @param n block index (0-3 are luma, 4-5 are chroma)
  251. */
  252. static inline void mpeg4_encode_block(MpegEncContext *s,
  253. int16_t *block, int n, int intra_dc,
  254. uint8_t *scan_table, PutBitContext *dc_pb,
  255. PutBitContext *ac_pb)
  256. {
  257. int i, last_non_zero;
  258. uint32_t *bits_tab;
  259. uint8_t *len_tab;
  260. const int last_index = s->block_last_index[n];
  261. if (s->mb_intra) { // Note gcc (3.2.1 at least) will optimize this away
  262. /* MPEG-4 based DC predictor */
  263. mpeg4_encode_dc(dc_pb, intra_dc, n);
  264. if (last_index < 1)
  265. return;
  266. i = 1;
  267. bits_tab = uni_mpeg4_intra_rl_bits;
  268. len_tab = uni_mpeg4_intra_rl_len;
  269. } else {
  270. if (last_index < 0)
  271. return;
  272. i = 0;
  273. bits_tab = uni_mpeg4_inter_rl_bits;
  274. len_tab = uni_mpeg4_inter_rl_len;
  275. }
  276. /* AC coefs */
  277. last_non_zero = i - 1;
  278. for (; i < last_index; i++) {
  279. int level = block[scan_table[i]];
  280. if (level) {
  281. int run = i - last_non_zero - 1;
  282. level += 64;
  283. if ((level & (~127)) == 0) {
  284. const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
  285. put_bits(ac_pb, len_tab[index], bits_tab[index]);
  286. } else { // ESC3
  287. put_bits(ac_pb,
  288. 7 + 2 + 1 + 6 + 1 + 12 + 1,
  289. (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) +
  290. (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
  291. }
  292. last_non_zero = i;
  293. }
  294. }
  295. /* if (i <= last_index) */ {
  296. int level = block[scan_table[i]];
  297. int run = i - last_non_zero - 1;
  298. level += 64;
  299. if ((level & (~127)) == 0) {
  300. const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
  301. put_bits(ac_pb, len_tab[index], bits_tab[index]);
  302. } else { // ESC3
  303. put_bits(ac_pb,
  304. 7 + 2 + 1 + 6 + 1 + 12 + 1,
  305. (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) +
  306. (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
  307. }
  308. }
  309. }
  310. static int mpeg4_get_block_length(MpegEncContext *s,
  311. int16_t *block, int n,
  312. int intra_dc, uint8_t *scan_table)
  313. {
  314. int i, last_non_zero;
  315. uint8_t *len_tab;
  316. const int last_index = s->block_last_index[n];
  317. int len = 0;
  318. if (s->mb_intra) { // Note gcc (3.2.1 at least) will optimize this away
  319. /* MPEG-4 based DC predictor */
  320. len += mpeg4_get_dc_length(intra_dc, n);
  321. if (last_index < 1)
  322. return len;
  323. i = 1;
  324. len_tab = uni_mpeg4_intra_rl_len;
  325. } else {
  326. if (last_index < 0)
  327. return 0;
  328. i = 0;
  329. len_tab = uni_mpeg4_inter_rl_len;
  330. }
  331. /* AC coefs */
  332. last_non_zero = i - 1;
  333. for (; i < last_index; i++) {
  334. int level = block[scan_table[i]];
  335. if (level) {
  336. int run = i - last_non_zero - 1;
  337. level += 64;
  338. if ((level & (~127)) == 0) {
  339. const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
  340. len += len_tab[index];
  341. } else { // ESC3
  342. len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
  343. }
  344. last_non_zero = i;
  345. }
  346. }
  347. /* if (i <= last_index) */ {
  348. int level = block[scan_table[i]];
  349. int run = i - last_non_zero - 1;
  350. level += 64;
  351. if ((level & (~127)) == 0) {
  352. const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
  353. len += len_tab[index];
  354. } else { // ESC3
  355. len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
  356. }
  357. }
  358. return len;
  359. }
  360. static inline void mpeg4_encode_blocks(MpegEncContext *s, int16_t block[6][64],
  361. int intra_dc[6], uint8_t **scan_table,
  362. PutBitContext *dc_pb,
  363. PutBitContext *ac_pb)
  364. {
  365. int i;
  366. if (scan_table) {
  367. if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
  368. for (i = 0; i < 6; i++)
  369. skip_put_bits(&s->pb,
  370. mpeg4_get_block_length(s, block[i], i,
  371. intra_dc[i], scan_table[i]));
  372. } else {
  373. /* encode each block */
  374. for (i = 0; i < 6; i++)
  375. mpeg4_encode_block(s, block[i], i,
  376. intra_dc[i], scan_table[i], dc_pb, ac_pb);
  377. }
  378. } else {
  379. if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
  380. for (i = 0; i < 6; i++)
  381. skip_put_bits(&s->pb,
  382. mpeg4_get_block_length(s, block[i], i, 0,
  383. s->intra_scantable.permutated));
  384. } else {
  385. /* encode each block */
  386. for (i = 0; i < 6; i++)
  387. mpeg4_encode_block(s, block[i], i, 0,
  388. s->intra_scantable.permutated, dc_pb, ac_pb);
  389. }
  390. }
  391. }
  392. static inline int get_b_cbp(MpegEncContext *s, int16_t block[6][64],
  393. int motion_x, int motion_y, int mb_type)
  394. {
  395. int cbp = 0, i;
  396. if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
  397. int score = 0;
  398. const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  399. for (i = 0; i < 6; i++) {
  400. if (s->coded_score[i] < 0) {
  401. score += s->coded_score[i];
  402. cbp |= 1 << (5 - i);
  403. }
  404. }
  405. if (cbp) {
  406. int zero_score = -6;
  407. if ((motion_x | motion_y | s->dquant | mb_type) == 0)
  408. zero_score -= 4; // 2 * MV + mb_type + cbp bit
  409. zero_score *= lambda;
  410. if (zero_score <= score)
  411. cbp = 0;
  412. }
  413. for (i = 0; i < 6; i++) {
  414. if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
  415. s->block_last_index[i] = -1;
  416. s->bdsp.clear_block(s->block[i]);
  417. }
  418. }
  419. } else {
  420. for (i = 0; i < 6; i++) {
  421. if (s->block_last_index[i] >= 0)
  422. cbp |= 1 << (5 - i);
  423. }
  424. }
  425. return cbp;
  426. }
  427. // FIXME this is duplicated to h263.c
  428. static const int dquant_code[5] = { 1, 0, 9, 2, 3 };
  429. void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64],
  430. int motion_x, int motion_y)
  431. {
  432. int cbpc, cbpy, pred_x, pred_y;
  433. PutBitContext *const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
  434. PutBitContext *const tex_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
  435. PutBitContext *const dc_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb;
  436. const int interleaved_stats = (s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
  437. if (!s->mb_intra) {
  438. int i, cbp;
  439. if (s->pict_type == AV_PICTURE_TYPE_B) {
  440. /* convert from mv_dir to type */
  441. static const int mb_type_table[8] = { -1, 3, 2, 1, -1, -1, -1, 0 };
  442. int mb_type = mb_type_table[s->mv_dir];
  443. if (s->mb_x == 0) {
  444. for (i = 0; i < 2; i++)
  445. s->last_mv[i][0][0] =
  446. s->last_mv[i][0][1] =
  447. s->last_mv[i][1][0] =
  448. s->last_mv[i][1][1] = 0;
  449. }
  450. av_assert2(s->dquant >= -2 && s->dquant <= 2);
  451. av_assert2((s->dquant & 1) == 0);
  452. av_assert2(mb_type >= 0);
  453. /* nothing to do if this MB was skipped in the next P-frame */
  454. if (s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { // FIXME avoid DCT & ...
  455. s->skip_count++;
  456. s->mv[0][0][0] =
  457. s->mv[0][0][1] =
  458. s->mv[1][0][0] =
  459. s->mv[1][0][1] = 0;
  460. s->mv_dir = MV_DIR_FORWARD; // doesn't matter
  461. s->qscale -= s->dquant;
  462. // s->mb_skipped = 1;
  463. return;
  464. }
  465. cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type);
  466. if ((cbp | motion_x | motion_y | mb_type) == 0) {
  467. /* direct MB with MV={0,0} */
  468. av_assert2(s->dquant == 0);
  469. put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
  470. if (interleaved_stats) {
  471. s->misc_bits++;
  472. s->last_bits++;
  473. }
  474. s->skip_count++;
  475. return;
  476. }
  477. put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
  478. put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ // FIXME merge
  479. put_bits(&s->pb, mb_type + 1, 1); // this table is so simple that we don't need it :)
  480. if (cbp)
  481. put_bits(&s->pb, 6, cbp);
  482. if (cbp && mb_type) {
  483. if (s->dquant)
  484. put_bits(&s->pb, 2, (s->dquant >> 2) + 3);
  485. else
  486. put_bits(&s->pb, 1, 0);
  487. } else
  488. s->qscale -= s->dquant;
  489. if (!s->progressive_sequence) {
  490. if (cbp)
  491. put_bits(&s->pb, 1, s->interlaced_dct);
  492. if (mb_type) // not direct mode
  493. put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
  494. }
  495. if (interleaved_stats)
  496. s->misc_bits += get_bits_diff(s);
  497. if (!mb_type) {
  498. av_assert2(s->mv_dir & MV_DIRECT);
  499. ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
  500. s->b_count++;
  501. s->f_count++;
  502. } else {
  503. av_assert2(mb_type > 0 && mb_type < 4);
  504. if (s->mv_type != MV_TYPE_FIELD) {
  505. if (s->mv_dir & MV_DIR_FORWARD) {
  506. ff_h263_encode_motion_vector(s,
  507. s->mv[0][0][0] - s->last_mv[0][0][0],
  508. s->mv[0][0][1] - s->last_mv[0][0][1],
  509. s->f_code);
  510. s->last_mv[0][0][0] =
  511. s->last_mv[0][1][0] = s->mv[0][0][0];
  512. s->last_mv[0][0][1] =
  513. s->last_mv[0][1][1] = s->mv[0][0][1];
  514. s->f_count++;
  515. }
  516. if (s->mv_dir & MV_DIR_BACKWARD) {
  517. ff_h263_encode_motion_vector(s,
  518. s->mv[1][0][0] - s->last_mv[1][0][0],
  519. s->mv[1][0][1] - s->last_mv[1][0][1],
  520. s->b_code);
  521. s->last_mv[1][0][0] =
  522. s->last_mv[1][1][0] = s->mv[1][0][0];
  523. s->last_mv[1][0][1] =
  524. s->last_mv[1][1][1] = s->mv[1][0][1];
  525. s->b_count++;
  526. }
  527. } else {
  528. if (s->mv_dir & MV_DIR_FORWARD) {
  529. put_bits(&s->pb, 1, s->field_select[0][0]);
  530. put_bits(&s->pb, 1, s->field_select[0][1]);
  531. }
  532. if (s->mv_dir & MV_DIR_BACKWARD) {
  533. put_bits(&s->pb, 1, s->field_select[1][0]);
  534. put_bits(&s->pb, 1, s->field_select[1][1]);
  535. }
  536. if (s->mv_dir & MV_DIR_FORWARD) {
  537. for (i = 0; i < 2; i++) {
  538. ff_h263_encode_motion_vector(s,
  539. s->mv[0][i][0] - s->last_mv[0][i][0],
  540. s->mv[0][i][1] - s->last_mv[0][i][1] / 2,
  541. s->f_code);
  542. s->last_mv[0][i][0] = s->mv[0][i][0];
  543. s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
  544. }
  545. s->f_count++;
  546. }
  547. if (s->mv_dir & MV_DIR_BACKWARD) {
  548. for (i = 0; i < 2; i++) {
  549. ff_h263_encode_motion_vector(s,
  550. s->mv[1][i][0] - s->last_mv[1][i][0],
  551. s->mv[1][i][1] - s->last_mv[1][i][1] / 2,
  552. s->b_code);
  553. s->last_mv[1][i][0] = s->mv[1][i][0];
  554. s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
  555. }
  556. s->b_count++;
  557. }
  558. }
  559. }
  560. if (interleaved_stats)
  561. s->mv_bits += get_bits_diff(s);
  562. mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
  563. if (interleaved_stats)
  564. s->p_tex_bits += get_bits_diff(s);
  565. } else { /* s->pict_type==AV_PICTURE_TYPE_B */
  566. cbp = get_p_cbp(s, block, motion_x, motion_y);
  567. if ((cbp | motion_x | motion_y | s->dquant) == 0 &&
  568. s->mv_type == MV_TYPE_16X16) {
  569. /* Check if the B-frames can skip it too, as we must skip it
  570. * if we skip here why didn't they just compress
  571. * the skip-mb bits instead of reusing them ?! */
  572. if (s->max_b_frames > 0) {
  573. int i;
  574. int x, y, offset;
  575. uint8_t *p_pic;
  576. x = s->mb_x * 16;
  577. y = s->mb_y * 16;
  578. offset = x + y * s->linesize;
  579. p_pic = s->new_picture.f->data[0] + offset;
  580. s->mb_skipped = 1;
  581. for (i = 0; i < s->max_b_frames; i++) {
  582. uint8_t *b_pic;
  583. int diff;
  584. Picture *pic = s->reordered_input_picture[i + 1];
  585. if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
  586. break;
  587. b_pic = pic->f->data[0] + offset;
  588. if (!pic->shared)
  589. b_pic += INPLACE_OFFSET;
  590. if (x + 16 > s->width || y + 16 > s->height) {
  591. int x1, y1;
  592. int xe = FFMIN(16, s->width - x);
  593. int ye = FFMIN(16, s->height - y);
  594. diff = 0;
  595. for (y1 = 0; y1 < ye; y1++) {
  596. for (x1 = 0; x1 < xe; x1++) {
  597. diff += FFABS(p_pic[x1 + y1 * s->linesize] - b_pic[x1 + y1 * s->linesize]);
  598. }
  599. }
  600. diff = diff * 256 / (xe * ye);
  601. } else {
  602. diff = s->mecc.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
  603. }
  604. if (diff > s->qscale * 70) { // FIXME check that 70 is optimal
  605. s->mb_skipped = 0;
  606. break;
  607. }
  608. }
  609. } else
  610. s->mb_skipped = 1;
  611. if (s->mb_skipped == 1) {
  612. /* skip macroblock */
  613. put_bits(&s->pb, 1, 1);
  614. if (interleaved_stats) {
  615. s->misc_bits++;
  616. s->last_bits++;
  617. }
  618. s->skip_count++;
  619. return;
  620. }
  621. }
  622. put_bits(&s->pb, 1, 0); /* mb coded */
  623. cbpc = cbp & 3;
  624. cbpy = cbp >> 2;
  625. cbpy ^= 0xf;
  626. if (s->mv_type == MV_TYPE_16X16) {
  627. if (s->dquant)
  628. cbpc += 8;
  629. put_bits(&s->pb,
  630. ff_h263_inter_MCBPC_bits[cbpc],
  631. ff_h263_inter_MCBPC_code[cbpc]);
  632. put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
  633. if (s->dquant)
  634. put_bits(pb2, 2, dquant_code[s->dquant + 2]);
  635. if (!s->progressive_sequence) {
  636. if (cbp)
  637. put_bits(pb2, 1, s->interlaced_dct);
  638. put_bits(pb2, 1, 0);
  639. }
  640. if (interleaved_stats)
  641. s->misc_bits += get_bits_diff(s);
  642. /* motion vectors: 16x16 mode */
  643. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  644. ff_h263_encode_motion_vector(s,
  645. motion_x - pred_x,
  646. motion_y - pred_y,
  647. s->f_code);
  648. } else if (s->mv_type == MV_TYPE_FIELD) {
  649. if (s->dquant)
  650. cbpc += 8;
  651. put_bits(&s->pb,
  652. ff_h263_inter_MCBPC_bits[cbpc],
  653. ff_h263_inter_MCBPC_code[cbpc]);
  654. put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
  655. if (s->dquant)
  656. put_bits(pb2, 2, dquant_code[s->dquant + 2]);
  657. av_assert2(!s->progressive_sequence);
  658. if (cbp)
  659. put_bits(pb2, 1, s->interlaced_dct);
  660. put_bits(pb2, 1, 1);
  661. if (interleaved_stats)
  662. s->misc_bits += get_bits_diff(s);
  663. /* motion vectors: 16x8 interlaced mode */
  664. ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
  665. pred_y /= 2;
  666. put_bits(&s->pb, 1, s->field_select[0][0]);
  667. put_bits(&s->pb, 1, s->field_select[0][1]);
  668. ff_h263_encode_motion_vector(s,
  669. s->mv[0][0][0] - pred_x,
  670. s->mv[0][0][1] - pred_y,
  671. s->f_code);
  672. ff_h263_encode_motion_vector(s,
  673. s->mv[0][1][0] - pred_x,
  674. s->mv[0][1][1] - pred_y,
  675. s->f_code);
  676. } else {
  677. av_assert2(s->mv_type == MV_TYPE_8X8);
  678. put_bits(&s->pb,
  679. ff_h263_inter_MCBPC_bits[cbpc + 16],
  680. ff_h263_inter_MCBPC_code[cbpc + 16]);
  681. put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
  682. if (!s->progressive_sequence && cbp)
  683. put_bits(pb2, 1, s->interlaced_dct);
  684. if (interleaved_stats)
  685. s->misc_bits += get_bits_diff(s);
  686. for (i = 0; i < 4; i++) {
  687. /* motion vectors: 8x8 mode*/
  688. ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
  689. ff_h263_encode_motion_vector(s,
  690. s->current_picture.motion_val[0][s->block_index[i]][0] - pred_x,
  691. s->current_picture.motion_val[0][s->block_index[i]][1] - pred_y,
  692. s->f_code);
  693. }
  694. }
  695. if (interleaved_stats)
  696. s->mv_bits += get_bits_diff(s);
  697. mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
  698. if (interleaved_stats)
  699. s->p_tex_bits += get_bits_diff(s);
  700. s->f_count++;
  701. }
  702. } else {
  703. int cbp;
  704. int dc_diff[6]; // dc values with the dc prediction subtracted
  705. int dir[6]; // prediction direction
  706. int zigzag_last_index[6];
  707. uint8_t *scan_table[6];
  708. int i;
  709. for (i = 0; i < 6; i++)
  710. dc_diff[i] = ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
  711. if (s->avctx->flags & AV_CODEC_FLAG_AC_PRED) {
  712. s->ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
  713. } else {
  714. for (i = 0; i < 6; i++)
  715. scan_table[i] = s->intra_scantable.permutated;
  716. }
  717. /* compute cbp */
  718. cbp = 0;
  719. for (i = 0; i < 6; i++)
  720. if (s->block_last_index[i] >= 1)
  721. cbp |= 1 << (5 - i);
  722. cbpc = cbp & 3;
  723. if (s->pict_type == AV_PICTURE_TYPE_I) {
  724. if (s->dquant)
  725. cbpc += 4;
  726. put_bits(&s->pb,
  727. ff_h263_intra_MCBPC_bits[cbpc],
  728. ff_h263_intra_MCBPC_code[cbpc]);
  729. } else {
  730. if (s->dquant)
  731. cbpc += 8;
  732. put_bits(&s->pb, 1, 0); /* mb coded */
  733. put_bits(&s->pb,
  734. ff_h263_inter_MCBPC_bits[cbpc + 4],
  735. ff_h263_inter_MCBPC_code[cbpc + 4]);
  736. }
  737. put_bits(pb2, 1, s->ac_pred);
  738. cbpy = cbp >> 2;
  739. put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
  740. if (s->dquant)
  741. put_bits(dc_pb, 2, dquant_code[s->dquant + 2]);
  742. if (!s->progressive_sequence)
  743. put_bits(dc_pb, 1, s->interlaced_dct);
  744. if (interleaved_stats)
  745. s->misc_bits += get_bits_diff(s);
  746. mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
  747. if (interleaved_stats)
  748. s->i_tex_bits += get_bits_diff(s);
  749. s->i_count++;
  750. /* restore ac coeffs & last_index stuff
  751. * if we messed them up with the prediction */
  752. if (s->ac_pred)
  753. restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
  754. }
  755. }
  756. /**
  757. * add MPEG-4 stuffing bits (01...1)
  758. */
  759. void ff_mpeg4_stuffing(PutBitContext *pbc)
  760. {
  761. int length;
  762. put_bits(pbc, 1, 0);
  763. length = (-put_bits_count(pbc)) & 7;
  764. if (length)
  765. put_bits(pbc, length, (1 << length) - 1);
  766. }
  767. /* must be called before writing the header */
  768. void ff_set_mpeg4_time(MpegEncContext *s)
  769. {
  770. if (s->pict_type == AV_PICTURE_TYPE_B) {
  771. ff_mpeg4_init_direct_mv(s);
  772. } else {
  773. s->last_time_base = s->time_base;
  774. s->time_base = FFUDIV(s->time, s->avctx->time_base.den);
  775. }
  776. }
  777. static void mpeg4_encode_gop_header(MpegEncContext *s)
  778. {
  779. int hours, minutes, seconds;
  780. int64_t time;
  781. put_bits(&s->pb, 16, 0);
  782. put_bits(&s->pb, 16, GOP_STARTCODE);
  783. time = s->current_picture_ptr->f->pts;
  784. if (s->reordered_input_picture[1])
  785. time = FFMIN(time, s->reordered_input_picture[1]->f->pts);
  786. time = time * s->avctx->time_base.num;
  787. s->last_time_base = FFUDIV(time, s->avctx->time_base.den);
  788. seconds = FFUDIV(time, s->avctx->time_base.den);
  789. minutes = FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
  790. hours = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
  791. hours = FFUMOD(hours , 24);
  792. put_bits(&s->pb, 5, hours);
  793. put_bits(&s->pb, 6, minutes);
  794. put_bits(&s->pb, 1, 1);
  795. put_bits(&s->pb, 6, seconds);
  796. put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
  797. put_bits(&s->pb, 1, 0); // broken link == NO
  798. ff_mpeg4_stuffing(&s->pb);
  799. }
  800. static void mpeg4_encode_visual_object_header(MpegEncContext *s)
  801. {
  802. int profile_and_level_indication;
  803. int vo_ver_id;
  804. if (s->avctx->profile != FF_PROFILE_UNKNOWN) {
  805. profile_and_level_indication = s->avctx->profile << 4;
  806. } else if (s->max_b_frames || s->quarter_sample) {
  807. profile_and_level_indication = 0xF0; // adv simple
  808. } else {
  809. profile_and_level_indication = 0x00; // simple
  810. }
  811. if (s->avctx->level != FF_LEVEL_UNKNOWN)
  812. profile_and_level_indication |= s->avctx->level;
  813. else
  814. profile_and_level_indication |= 1; // level 1
  815. if (profile_and_level_indication >> 4 == 0xF)
  816. vo_ver_id = 5;
  817. else
  818. vo_ver_id = 1;
  819. // FIXME levels
  820. put_bits(&s->pb, 16, 0);
  821. put_bits(&s->pb, 16, VOS_STARTCODE);
  822. put_bits(&s->pb, 8, profile_and_level_indication);
  823. put_bits(&s->pb, 16, 0);
  824. put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
  825. put_bits(&s->pb, 1, 1);
  826. put_bits(&s->pb, 4, vo_ver_id);
  827. put_bits(&s->pb, 3, 1); // priority
  828. put_bits(&s->pb, 4, 1); // visual obj type== video obj
  829. put_bits(&s->pb, 1, 0); // video signal type == no clue // FIXME
  830. ff_mpeg4_stuffing(&s->pb);
  831. }
  832. static void mpeg4_encode_vol_header(MpegEncContext *s,
  833. int vo_number,
  834. int vol_number)
  835. {
  836. int vo_ver_id;
  837. if (!CONFIG_MPEG4_ENCODER)
  838. return;
  839. if (s->max_b_frames || s->quarter_sample) {
  840. vo_ver_id = 5;
  841. s->vo_type = ADV_SIMPLE_VO_TYPE;
  842. } else {
  843. vo_ver_id = 1;
  844. s->vo_type = SIMPLE_VO_TYPE;
  845. }
  846. put_bits(&s->pb, 16, 0);
  847. put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
  848. put_bits(&s->pb, 16, 0);
  849. put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
  850. put_bits(&s->pb, 1, 0); /* random access vol */
  851. put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
  852. if (s->workaround_bugs & FF_BUG_MS) {
  853. put_bits(&s->pb, 1, 0); /* is obj layer id= no */
  854. } else {
  855. put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
  856. put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
  857. put_bits(&s->pb, 3, 1); /* is obj layer priority */
  858. }
  859. s->aspect_ratio_info = ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
  860. put_bits(&s->pb, 4, s->aspect_ratio_info); /* aspect ratio info */
  861. if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
  862. av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
  863. s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den, 255);
  864. put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
  865. put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
  866. }
  867. if (s->workaround_bugs & FF_BUG_MS) {
  868. put_bits(&s->pb, 1, 0); /* vol control parameters= no @@@ */
  869. } else {
  870. put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
  871. put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
  872. put_bits(&s->pb, 1, s->low_delay);
  873. put_bits(&s->pb, 1, 0); /* vbv parameters= no */
  874. }
  875. put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
  876. put_bits(&s->pb, 1, 1); /* marker bit */
  877. put_bits(&s->pb, 16, s->avctx->time_base.den);
  878. if (s->time_increment_bits < 1)
  879. s->time_increment_bits = 1;
  880. put_bits(&s->pb, 1, 1); /* marker bit */
  881. put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
  882. put_bits(&s->pb, 1, 1); /* marker bit */
  883. put_bits(&s->pb, 13, s->width); /* vol width */
  884. put_bits(&s->pb, 1, 1); /* marker bit */
  885. put_bits(&s->pb, 13, s->height); /* vol height */
  886. put_bits(&s->pb, 1, 1); /* marker bit */
  887. put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
  888. put_bits(&s->pb, 1, 1); /* obmc disable */
  889. if (vo_ver_id == 1)
  890. put_bits(&s->pb, 1, 0); /* sprite enable */
  891. else
  892. put_bits(&s->pb, 2, 0); /* sprite enable */
  893. put_bits(&s->pb, 1, 0); /* not 8 bit == false */
  894. put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */
  895. if (s->mpeg_quant) {
  896. ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
  897. ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
  898. }
  899. if (vo_ver_id != 1)
  900. put_bits(&s->pb, 1, s->quarter_sample);
  901. put_bits(&s->pb, 1, 1); /* complexity estimation disable */
  902. put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */
  903. put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
  904. if (s->data_partitioning)
  905. put_bits(&s->pb, 1, 0); /* no rvlc */
  906. if (vo_ver_id != 1) {
  907. put_bits(&s->pb, 1, 0); /* newpred */
  908. put_bits(&s->pb, 1, 0); /* reduced res vop */
  909. }
  910. put_bits(&s->pb, 1, 0); /* scalability */
  911. ff_mpeg4_stuffing(&s->pb);
  912. /* user data */
  913. if (!(s->avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
  914. put_bits(&s->pb, 16, 0);
  915. put_bits(&s->pb, 16, 0x1B2); /* user_data */
  916. avpriv_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
  917. }
  918. }
  919. /* write MPEG-4 VOP header */
  920. int ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
  921. {
  922. uint64_t time_incr;
  923. int64_t time_div, time_mod;
  924. if (s->pict_type == AV_PICTURE_TYPE_I) {
  925. if (!(s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
  926. if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) // HACK, the reference sw is buggy
  927. mpeg4_encode_visual_object_header(s);
  928. if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number == 0) // HACK, the reference sw is buggy
  929. mpeg4_encode_vol_header(s, 0, 0);
  930. }
  931. if (!(s->workaround_bugs & FF_BUG_MS))
  932. mpeg4_encode_gop_header(s);
  933. }
  934. s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
  935. put_bits(&s->pb, 16, 0); /* vop header */
  936. put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
  937. put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
  938. time_div = FFUDIV(s->time, s->avctx->time_base.den);
  939. time_mod = FFUMOD(s->time, s->avctx->time_base.den);
  940. time_incr = time_div - s->last_time_base;
  941. // This limits the frame duration to max 1 hour
  942. if (time_incr > 3600) {
  943. av_log(s->avctx, AV_LOG_ERROR, "time_incr %"PRIu64" too large\n", time_incr);
  944. return AVERROR(EINVAL);
  945. }
  946. while (time_incr--)
  947. put_bits(&s->pb, 1, 1);
  948. put_bits(&s->pb, 1, 0);
  949. put_bits(&s->pb, 1, 1); /* marker */
  950. put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
  951. put_bits(&s->pb, 1, 1); /* marker */
  952. put_bits(&s->pb, 1, 1); /* vop coded */
  953. if (s->pict_type == AV_PICTURE_TYPE_P) {
  954. put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
  955. }
  956. put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
  957. if (!s->progressive_sequence) {
  958. put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
  959. put_bits(&s->pb, 1, s->alternate_scan);
  960. }
  961. // FIXME sprite stuff
  962. put_bits(&s->pb, 5, s->qscale);
  963. if (s->pict_type != AV_PICTURE_TYPE_I)
  964. put_bits(&s->pb, 3, s->f_code); /* fcode_for */
  965. if (s->pict_type == AV_PICTURE_TYPE_B)
  966. put_bits(&s->pb, 3, s->b_code); /* fcode_back */
  967. return 0;
  968. }
  969. static av_cold void init_uni_dc_tab(void)
  970. {
  971. int level, uni_code, uni_len;
  972. for (level = -256; level < 256; level++) {
  973. int size, v, l;
  974. /* find number of bits */
  975. size = 0;
  976. v = abs(level);
  977. while (v) {
  978. v >>= 1;
  979. size++;
  980. }
  981. if (level < 0)
  982. l = (-level) ^ ((1 << size) - 1);
  983. else
  984. l = level;
  985. /* luminance */
  986. uni_code = ff_mpeg4_DCtab_lum[size][0];
  987. uni_len = ff_mpeg4_DCtab_lum[size][1];
  988. if (size > 0) {
  989. uni_code <<= size;
  990. uni_code |= l;
  991. uni_len += size;
  992. if (size > 8) {
  993. uni_code <<= 1;
  994. uni_code |= 1;
  995. uni_len++;
  996. }
  997. }
  998. uni_DCtab_lum_bits[level + 256] = uni_code;
  999. uni_DCtab_lum_len[level + 256] = uni_len;
  1000. /* chrominance */
  1001. uni_code = ff_mpeg4_DCtab_chrom[size][0];
  1002. uni_len = ff_mpeg4_DCtab_chrom[size][1];
  1003. if (size > 0) {
  1004. uni_code <<= size;
  1005. uni_code |= l;
  1006. uni_len += size;
  1007. if (size > 8) {
  1008. uni_code <<= 1;
  1009. uni_code |= 1;
  1010. uni_len++;
  1011. }
  1012. }
  1013. uni_DCtab_chrom_bits[level + 256] = uni_code;
  1014. uni_DCtab_chrom_len[level + 256] = uni_len;
  1015. }
  1016. }
  1017. static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
  1018. uint8_t *len_tab)
  1019. {
  1020. int slevel, run, last;
  1021. av_assert0(MAX_LEVEL >= 64);
  1022. av_assert0(MAX_RUN >= 63);
  1023. for (slevel = -64; slevel < 64; slevel++) {
  1024. if (slevel == 0)
  1025. continue;
  1026. for (run = 0; run < 64; run++) {
  1027. for (last = 0; last <= 1; last++) {
  1028. const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64);
  1029. int level = slevel < 0 ? -slevel : slevel;
  1030. int sign = slevel < 0 ? 1 : 0;
  1031. int bits, len, code;
  1032. int level1, run1;
  1033. len_tab[index] = 100;
  1034. /* ESC0 */
  1035. code = get_rl_index(rl, last, run, level);
  1036. bits = rl->table_vlc[code][0];
  1037. len = rl->table_vlc[code][1];
  1038. bits = bits * 2 + sign;
  1039. len++;
  1040. if (code != rl->n && len < len_tab[index]) {
  1041. bits_tab[index] = bits;
  1042. len_tab[index] = len;
  1043. }
  1044. /* ESC1 */
  1045. bits = rl->table_vlc[rl->n][0];
  1046. len = rl->table_vlc[rl->n][1];
  1047. bits = bits * 2;
  1048. len++; // esc1
  1049. level1 = level - rl->max_level[last][run];
  1050. if (level1 > 0) {
  1051. code = get_rl_index(rl, last, run, level1);
  1052. bits <<= rl->table_vlc[code][1];
  1053. len += rl->table_vlc[code][1];
  1054. bits += rl->table_vlc[code][0];
  1055. bits = bits * 2 + sign;
  1056. len++;
  1057. if (code != rl->n && len < len_tab[index]) {
  1058. bits_tab[index] = bits;
  1059. len_tab[index] = len;
  1060. }
  1061. }
  1062. /* ESC2 */
  1063. bits = rl->table_vlc[rl->n][0];
  1064. len = rl->table_vlc[rl->n][1];
  1065. bits = bits * 4 + 2;
  1066. len += 2; // esc2
  1067. run1 = run - rl->max_run[last][level] - 1;
  1068. if (run1 >= 0) {
  1069. code = get_rl_index(rl, last, run1, level);
  1070. bits <<= rl->table_vlc[code][1];
  1071. len += rl->table_vlc[code][1];
  1072. bits += rl->table_vlc[code][0];
  1073. bits = bits * 2 + sign;
  1074. len++;
  1075. if (code != rl->n && len < len_tab[index]) {
  1076. bits_tab[index] = bits;
  1077. len_tab[index] = len;
  1078. }
  1079. }
  1080. /* ESC3 */
  1081. bits = rl->table_vlc[rl->n][0];
  1082. len = rl->table_vlc[rl->n][1];
  1083. bits = bits * 4 + 3;
  1084. len += 2; // esc3
  1085. bits = bits * 2 + last;
  1086. len++;
  1087. bits = bits * 64 + run;
  1088. len += 6;
  1089. bits = bits * 2 + 1;
  1090. len++; // marker
  1091. bits = bits * 4096 + (slevel & 0xfff);
  1092. len += 12;
  1093. bits = bits * 2 + 1;
  1094. len++; // marker
  1095. if (len < len_tab[index]) {
  1096. bits_tab[index] = bits;
  1097. len_tab[index] = len;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. }
  1103. static av_cold int encode_init(AVCodecContext *avctx)
  1104. {
  1105. MpegEncContext *s = avctx->priv_data;
  1106. int ret;
  1107. static int done = 0;
  1108. if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
  1109. av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
  1110. return AVERROR(EINVAL);
  1111. }
  1112. if ((ret = ff_mpv_encode_init(avctx)) < 0)
  1113. return ret;
  1114. if (!done) {
  1115. done = 1;
  1116. init_uni_dc_tab();
  1117. ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
  1118. init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
  1119. init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
  1120. }
  1121. s->min_qcoeff = -2048;
  1122. s->max_qcoeff = 2047;
  1123. s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
  1124. s->intra_ac_vlc_last_length = uni_mpeg4_intra_rl_len + 128 * 64;
  1125. s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
  1126. s->inter_ac_vlc_last_length = uni_mpeg4_inter_rl_len + 128 * 64;
  1127. s->luma_dc_vlc_length = uni_DCtab_lum_len;
  1128. s->ac_esc_length = 7 + 2 + 1 + 6 + 1 + 12 + 1;
  1129. s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
  1130. s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
  1131. if (s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  1132. s->avctx->extradata = av_malloc(1024);
  1133. init_put_bits(&s->pb, s->avctx->extradata, 1024);
  1134. if (!(s->workaround_bugs & FF_BUG_MS))
  1135. mpeg4_encode_visual_object_header(s);
  1136. mpeg4_encode_vol_header(s, 0, 0);
  1137. // ff_mpeg4_stuffing(&s->pb); ?
  1138. flush_put_bits(&s->pb);
  1139. s->avctx->extradata_size = (put_bits_count(&s->pb) + 7) >> 3;
  1140. }
  1141. return 0;
  1142. }
  1143. void ff_mpeg4_init_partitions(MpegEncContext *s)
  1144. {
  1145. uint8_t *start = put_bits_ptr(&s->pb);
  1146. uint8_t *end = s->pb.buf_end;
  1147. int size = end - start;
  1148. int pb_size = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start;
  1149. int tex_size = (size - 2 * pb_size) & (~3);
  1150. set_put_bits_buffer_size(&s->pb, pb_size);
  1151. init_put_bits(&s->tex_pb, start + pb_size, tex_size);
  1152. init_put_bits(&s->pb2, start + pb_size + tex_size, pb_size);
  1153. }
  1154. void ff_mpeg4_merge_partitions(MpegEncContext *s)
  1155. {
  1156. const int pb2_len = put_bits_count(&s->pb2);
  1157. const int tex_pb_len = put_bits_count(&s->tex_pb);
  1158. const int bits = put_bits_count(&s->pb);
  1159. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1160. put_bits(&s->pb, 19, DC_MARKER);
  1161. s->misc_bits += 19 + pb2_len + bits - s->last_bits;
  1162. s->i_tex_bits += tex_pb_len;
  1163. } else {
  1164. put_bits(&s->pb, 17, MOTION_MARKER);
  1165. s->misc_bits += 17 + pb2_len;
  1166. s->mv_bits += bits - s->last_bits;
  1167. s->p_tex_bits += tex_pb_len;
  1168. }
  1169. flush_put_bits(&s->pb2);
  1170. flush_put_bits(&s->tex_pb);
  1171. set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
  1172. avpriv_copy_bits(&s->pb, s->pb2.buf, pb2_len);
  1173. avpriv_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
  1174. s->last_bits = put_bits_count(&s->pb);
  1175. }
  1176. void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
  1177. {
  1178. int mb_num_bits = av_log2(s->mb_num - 1) + 1;
  1179. put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
  1180. put_bits(&s->pb, 1, 1);
  1181. put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y * s->mb_width);
  1182. put_bits(&s->pb, s->quant_precision, s->qscale);
  1183. put_bits(&s->pb, 1, 0); /* no HEC */
  1184. }
  1185. #define OFFSET(x) offsetof(MpegEncContext, x)
  1186. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1187. static const AVOption options[] = {
  1188. { "data_partitioning", "Use data partitioning.", OFFSET(data_partitioning), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  1189. { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  1190. FF_MPV_COMMON_OPTS
  1191. { NULL },
  1192. };
  1193. static const AVClass mpeg4enc_class = {
  1194. .class_name = "MPEG4 encoder",
  1195. .item_name = av_default_item_name,
  1196. .option = options,
  1197. .version = LIBAVUTIL_VERSION_INT,
  1198. };
  1199. AVCodec ff_mpeg4_encoder = {
  1200. .name = "mpeg4",
  1201. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
  1202. .type = AVMEDIA_TYPE_VIDEO,
  1203. .id = AV_CODEC_ID_MPEG4,
  1204. .priv_data_size = sizeof(MpegEncContext),
  1205. .init = encode_init,
  1206. .encode2 = ff_mpv_encode_picture,
  1207. .close = ff_mpv_encode_end,
  1208. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  1209. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
  1210. .priv_class = &mpeg4enc_class,
  1211. };