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.

2472 lines
84KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/timer.h"
  33. #include "avcodec.h"
  34. #include "blockdsp.h"
  35. #include "idctdsp.h"
  36. #include "internal.h"
  37. #include "mathops.h"
  38. #include "mpegutils.h"
  39. #include "mpegvideo.h"
  40. #include "mjpegenc.h"
  41. #include "msmpeg4.h"
  42. #include "qpeldsp.h"
  43. #include "xvmc_internal.h"
  44. #include "thread.h"
  45. #include <limits.h>
  46. static const uint8_t ff_default_chroma_qscale_table[32] = {
  47. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  48. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  49. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  50. };
  51. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  52. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  53. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  54. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  55. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  56. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  57. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  58. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  59. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  60. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  61. };
  62. static const uint8_t mpeg2_dc_scale_table1[128] = {
  63. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  64. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  67. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  68. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  69. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  70. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  71. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  72. };
  73. static const uint8_t mpeg2_dc_scale_table2[128] = {
  74. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  76. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  77. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  78. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  79. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  82. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  83. };
  84. static const uint8_t mpeg2_dc_scale_table3[128] = {
  85. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  86. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  92. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  93. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  94. };
  95. const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
  96. ff_mpeg1_dc_scale_table,
  97. mpeg2_dc_scale_table1,
  98. mpeg2_dc_scale_table2,
  99. mpeg2_dc_scale_table3,
  100. };
  101. const uint8_t ff_alternate_horizontal_scan[64] = {
  102. 0, 1, 2, 3, 8, 9, 16, 17,
  103. 10, 11, 4, 5, 6, 7, 15, 14,
  104. 13, 12, 19, 18, 24, 25, 32, 33,
  105. 26, 27, 20, 21, 22, 23, 28, 29,
  106. 30, 31, 34, 35, 40, 41, 48, 49,
  107. 42, 43, 36, 37, 38, 39, 44, 45,
  108. 46, 47, 50, 51, 56, 57, 58, 59,
  109. 52, 53, 54, 55, 60, 61, 62, 63,
  110. };
  111. const uint8_t ff_alternate_vertical_scan[64] = {
  112. 0, 8, 16, 24, 1, 9, 2, 10,
  113. 17, 25, 32, 40, 48, 56, 57, 49,
  114. 41, 33, 26, 18, 3, 11, 4, 12,
  115. 19, 27, 34, 42, 50, 58, 35, 43,
  116. 51, 59, 20, 28, 5, 13, 6, 14,
  117. 21, 29, 36, 44, 52, 60, 37, 45,
  118. 53, 61, 22, 30, 7, 15, 23, 31,
  119. 38, 46, 54, 62, 39, 47, 55, 63,
  120. };
  121. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  122. int16_t *block, int n, int qscale)
  123. {
  124. int i, level, nCoeffs;
  125. const uint16_t *quant_matrix;
  126. nCoeffs= s->block_last_index[n];
  127. if (n < 4)
  128. block[0] = block[0] * s->y_dc_scale;
  129. else
  130. block[0] = block[0] * s->c_dc_scale;
  131. /* XXX: only mpeg1 */
  132. quant_matrix = s->intra_matrix;
  133. for(i=1;i<=nCoeffs;i++) {
  134. int j= s->intra_scantable.permutated[i];
  135. level = block[j];
  136. if (level) {
  137. if (level < 0) {
  138. level = -level;
  139. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  140. level = (level - 1) | 1;
  141. level = -level;
  142. } else {
  143. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  144. level = (level - 1) | 1;
  145. }
  146. block[j] = level;
  147. }
  148. }
  149. }
  150. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  151. int16_t *block, int n, int qscale)
  152. {
  153. int i, level, nCoeffs;
  154. const uint16_t *quant_matrix;
  155. nCoeffs= s->block_last_index[n];
  156. quant_matrix = s->inter_matrix;
  157. for(i=0; i<=nCoeffs; i++) {
  158. int j= s->intra_scantable.permutated[i];
  159. level = block[j];
  160. if (level) {
  161. if (level < 0) {
  162. level = -level;
  163. level = (((level << 1) + 1) * qscale *
  164. ((int) (quant_matrix[j]))) >> 4;
  165. level = (level - 1) | 1;
  166. level = -level;
  167. } else {
  168. level = (((level << 1) + 1) * qscale *
  169. ((int) (quant_matrix[j]))) >> 4;
  170. level = (level - 1) | 1;
  171. }
  172. block[j] = level;
  173. }
  174. }
  175. }
  176. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  177. int16_t *block, int n, int qscale)
  178. {
  179. int i, level, nCoeffs;
  180. const uint16_t *quant_matrix;
  181. if(s->alternate_scan) nCoeffs= 63;
  182. else nCoeffs= s->block_last_index[n];
  183. if (n < 4)
  184. block[0] = block[0] * s->y_dc_scale;
  185. else
  186. block[0] = block[0] * s->c_dc_scale;
  187. quant_matrix = s->intra_matrix;
  188. for(i=1;i<=nCoeffs;i++) {
  189. int j= s->intra_scantable.permutated[i];
  190. level = block[j];
  191. if (level) {
  192. if (level < 0) {
  193. level = -level;
  194. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  195. level = -level;
  196. } else {
  197. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  198. }
  199. block[j] = level;
  200. }
  201. }
  202. }
  203. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  204. int16_t *block, int n, int qscale)
  205. {
  206. int i, level, nCoeffs;
  207. const uint16_t *quant_matrix;
  208. int sum=-1;
  209. if(s->alternate_scan) nCoeffs= 63;
  210. else nCoeffs= s->block_last_index[n];
  211. if (n < 4)
  212. block[0] = block[0] * s->y_dc_scale;
  213. else
  214. block[0] = block[0] * s->c_dc_scale;
  215. quant_matrix = s->intra_matrix;
  216. for(i=1;i<=nCoeffs;i++) {
  217. int j= s->intra_scantable.permutated[i];
  218. level = block[j];
  219. if (level) {
  220. if (level < 0) {
  221. level = -level;
  222. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  223. level = -level;
  224. } else {
  225. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  226. }
  227. block[j] = level;
  228. sum+=level;
  229. }
  230. }
  231. block[63]^=sum&1;
  232. }
  233. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  234. int16_t *block, int n, int qscale)
  235. {
  236. int i, level, nCoeffs;
  237. const uint16_t *quant_matrix;
  238. int sum=-1;
  239. if(s->alternate_scan) nCoeffs= 63;
  240. else nCoeffs= s->block_last_index[n];
  241. quant_matrix = s->inter_matrix;
  242. for(i=0; i<=nCoeffs; i++) {
  243. int j= s->intra_scantable.permutated[i];
  244. level = block[j];
  245. if (level) {
  246. if (level < 0) {
  247. level = -level;
  248. level = (((level << 1) + 1) * qscale *
  249. ((int) (quant_matrix[j]))) >> 4;
  250. level = -level;
  251. } else {
  252. level = (((level << 1) + 1) * qscale *
  253. ((int) (quant_matrix[j]))) >> 4;
  254. }
  255. block[j] = level;
  256. sum+=level;
  257. }
  258. }
  259. block[63]^=sum&1;
  260. }
  261. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  262. int16_t *block, int n, int qscale)
  263. {
  264. int i, level, qmul, qadd;
  265. int nCoeffs;
  266. assert(s->block_last_index[n]>=0);
  267. qmul = qscale << 1;
  268. if (!s->h263_aic) {
  269. if (n < 4)
  270. block[0] = block[0] * s->y_dc_scale;
  271. else
  272. block[0] = block[0] * s->c_dc_scale;
  273. qadd = (qscale - 1) | 1;
  274. }else{
  275. qadd = 0;
  276. }
  277. if(s->ac_pred)
  278. nCoeffs=63;
  279. else
  280. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  281. for(i=1; i<=nCoeffs; i++) {
  282. level = block[i];
  283. if (level) {
  284. if (level < 0) {
  285. level = level * qmul - qadd;
  286. } else {
  287. level = level * qmul + qadd;
  288. }
  289. block[i] = level;
  290. }
  291. }
  292. }
  293. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  294. int16_t *block, int n, int qscale)
  295. {
  296. int i, level, qmul, qadd;
  297. int nCoeffs;
  298. assert(s->block_last_index[n]>=0);
  299. qadd = (qscale - 1) | 1;
  300. qmul = qscale << 1;
  301. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  302. for(i=0; i<=nCoeffs; i++) {
  303. level = block[i];
  304. if (level) {
  305. if (level < 0) {
  306. level = level * qmul - qadd;
  307. } else {
  308. level = level * qmul + qadd;
  309. }
  310. block[i] = level;
  311. }
  312. }
  313. }
  314. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  315. int (*mv)[2][4][2],
  316. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  317. {
  318. MpegEncContext *s = opaque;
  319. s->mv_dir = mv_dir;
  320. s->mv_type = mv_type;
  321. s->mb_intra = mb_intra;
  322. s->mb_skipped = mb_skipped;
  323. s->mb_x = mb_x;
  324. s->mb_y = mb_y;
  325. memcpy(s->mv, mv, sizeof(*mv));
  326. ff_init_block_index(s);
  327. ff_update_block_index(s);
  328. s->bdsp.clear_blocks(s->block[0]);
  329. s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  330. s->dest[1] = s->current_picture.f->data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  331. s->dest[2] = s->current_picture.f->data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  332. assert(ref == 0);
  333. ff_MPV_decode_mb(s, s->block);
  334. }
  335. /* init common dct for both encoder and decoder */
  336. av_cold int ff_dct_common_init(MpegEncContext *s)
  337. {
  338. ff_blockdsp_init(&s->bdsp, s->avctx);
  339. ff_dsputil_init(&s->dsp, s->avctx);
  340. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  341. ff_idctdsp_init(&s->idsp, s->avctx);
  342. ff_mpegvideodsp_init(&s->mdsp);
  343. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  344. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  345. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  346. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  347. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  348. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  349. if (s->flags & CODEC_FLAG_BITEXACT)
  350. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  351. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  352. if (ARCH_ARM)
  353. ff_MPV_common_init_arm(s);
  354. if (ARCH_PPC)
  355. ff_MPV_common_init_ppc(s);
  356. if (ARCH_X86)
  357. ff_MPV_common_init_x86(s);
  358. /* load & permutate scantables
  359. * note: only wmv uses different ones
  360. */
  361. if (s->alternate_scan) {
  362. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  363. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  364. } else {
  365. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  366. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  367. }
  368. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  369. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  370. return 0;
  371. }
  372. static int frame_size_alloc(MpegEncContext *s, int linesize)
  373. {
  374. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  375. // edge emu needs blocksize + filter length - 1
  376. // (= 17x17 for halfpel / 21x21 for h264)
  377. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  378. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  379. // linesize * interlaced * MBsize
  380. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
  381. fail);
  382. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
  383. fail)
  384. s->me.temp = s->me.scratchpad;
  385. s->rd_scratchpad = s->me.scratchpad;
  386. s->b_scratchpad = s->me.scratchpad;
  387. s->obmc_scratchpad = s->me.scratchpad + 16;
  388. return 0;
  389. fail:
  390. av_freep(&s->edge_emu_buffer);
  391. return AVERROR(ENOMEM);
  392. }
  393. /**
  394. * Allocate a frame buffer
  395. */
  396. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  397. {
  398. int edges_needed = av_codec_is_encoder(s->avctx->codec);
  399. int r, ret;
  400. pic->tf.f = pic->f;
  401. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  402. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  403. s->codec_id != AV_CODEC_ID_MSS2) {
  404. if (edges_needed) {
  405. pic->f->width = s->avctx->width + 2 * EDGE_WIDTH;
  406. pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
  407. }
  408. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  409. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  410. } else {
  411. pic->f->width = s->avctx->width;
  412. pic->f->height = s->avctx->height;
  413. pic->f->format = s->avctx->pix_fmt;
  414. r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
  415. }
  416. if (r < 0 || !pic->f->buf[0]) {
  417. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  418. r, pic->f->data[0]);
  419. return -1;
  420. }
  421. if (edges_needed) {
  422. int i;
  423. for (i = 0; pic->f->data[i]; i++) {
  424. int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
  425. pic->f->linesize[i] +
  426. (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
  427. pic->f->data[i] += offset;
  428. }
  429. pic->f->width = s->avctx->width;
  430. pic->f->height = s->avctx->height;
  431. }
  432. if (s->avctx->hwaccel) {
  433. assert(!pic->hwaccel_picture_private);
  434. if (s->avctx->hwaccel->frame_priv_data_size) {
  435. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->frame_priv_data_size);
  436. if (!pic->hwaccel_priv_buf) {
  437. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  438. return -1;
  439. }
  440. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  441. }
  442. }
  443. if (s->linesize && (s->linesize != pic->f->linesize[0] ||
  444. s->uvlinesize != pic->f->linesize[1])) {
  445. av_log(s->avctx, AV_LOG_ERROR,
  446. "get_buffer() failed (stride changed)\n");
  447. ff_mpeg_unref_picture(s, pic);
  448. return -1;
  449. }
  450. if (pic->f->linesize[1] != pic->f->linesize[2]) {
  451. av_log(s->avctx, AV_LOG_ERROR,
  452. "get_buffer() failed (uv stride mismatch)\n");
  453. ff_mpeg_unref_picture(s, pic);
  454. return -1;
  455. }
  456. if (!s->edge_emu_buffer &&
  457. (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
  458. av_log(s->avctx, AV_LOG_ERROR,
  459. "get_buffer() failed to allocate context scratch buffers.\n");
  460. ff_mpeg_unref_picture(s, pic);
  461. return ret;
  462. }
  463. return 0;
  464. }
  465. void ff_free_picture_tables(Picture *pic)
  466. {
  467. int i;
  468. av_buffer_unref(&pic->mb_var_buf);
  469. av_buffer_unref(&pic->mc_mb_var_buf);
  470. av_buffer_unref(&pic->mb_mean_buf);
  471. av_buffer_unref(&pic->mbskip_table_buf);
  472. av_buffer_unref(&pic->qscale_table_buf);
  473. av_buffer_unref(&pic->mb_type_buf);
  474. for (i = 0; i < 2; i++) {
  475. av_buffer_unref(&pic->motion_val_buf[i]);
  476. av_buffer_unref(&pic->ref_index_buf[i]);
  477. }
  478. }
  479. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  480. {
  481. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  482. const int mb_array_size = s->mb_stride * s->mb_height;
  483. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  484. int i;
  485. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  486. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  487. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  488. sizeof(uint32_t));
  489. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  490. return AVERROR(ENOMEM);
  491. if (s->encoding) {
  492. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  493. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  494. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  495. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  496. return AVERROR(ENOMEM);
  497. }
  498. if (s->out_format == FMT_H263 || s->encoding) {
  499. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  500. int ref_index_size = 4 * mb_array_size;
  501. for (i = 0; mv_size && i < 2; i++) {
  502. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  503. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  504. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  505. return AVERROR(ENOMEM);
  506. }
  507. }
  508. return 0;
  509. }
  510. static int make_tables_writable(Picture *pic)
  511. {
  512. int ret, i;
  513. #define MAKE_WRITABLE(table) \
  514. do {\
  515. if (pic->table &&\
  516. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  517. return ret;\
  518. } while (0)
  519. MAKE_WRITABLE(mb_var_buf);
  520. MAKE_WRITABLE(mc_mb_var_buf);
  521. MAKE_WRITABLE(mb_mean_buf);
  522. MAKE_WRITABLE(mbskip_table_buf);
  523. MAKE_WRITABLE(qscale_table_buf);
  524. MAKE_WRITABLE(mb_type_buf);
  525. for (i = 0; i < 2; i++) {
  526. MAKE_WRITABLE(motion_val_buf[i]);
  527. MAKE_WRITABLE(ref_index_buf[i]);
  528. }
  529. return 0;
  530. }
  531. /**
  532. * Allocate a Picture.
  533. * The pixels are allocated/set by calling get_buffer() if shared = 0
  534. */
  535. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  536. {
  537. int i, ret;
  538. if (shared) {
  539. assert(pic->f->data[0]);
  540. pic->shared = 1;
  541. } else {
  542. assert(!pic->f->buf[0]);
  543. if (alloc_frame_buffer(s, pic) < 0)
  544. return -1;
  545. s->linesize = pic->f->linesize[0];
  546. s->uvlinesize = pic->f->linesize[1];
  547. }
  548. if (!pic->qscale_table_buf)
  549. ret = alloc_picture_tables(s, pic);
  550. else
  551. ret = make_tables_writable(pic);
  552. if (ret < 0)
  553. goto fail;
  554. if (s->encoding) {
  555. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  556. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  557. pic->mb_mean = pic->mb_mean_buf->data;
  558. }
  559. pic->mbskip_table = pic->mbskip_table_buf->data;
  560. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  561. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  562. if (pic->motion_val_buf[0]) {
  563. for (i = 0; i < 2; i++) {
  564. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  565. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  566. }
  567. }
  568. return 0;
  569. fail:
  570. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  571. ff_mpeg_unref_picture(s, pic);
  572. ff_free_picture_tables(pic);
  573. return AVERROR(ENOMEM);
  574. }
  575. /**
  576. * Deallocate a picture.
  577. */
  578. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  579. {
  580. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  581. pic->tf.f = pic->f;
  582. /* WM Image / Screen codecs allocate internal buffers with different
  583. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  584. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  585. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  586. s->codec_id != AV_CODEC_ID_MSS2)
  587. ff_thread_release_buffer(s->avctx, &pic->tf);
  588. else if (pic->f)
  589. av_frame_unref(pic->f);
  590. av_buffer_unref(&pic->hwaccel_priv_buf);
  591. if (pic->needs_realloc)
  592. ff_free_picture_tables(pic);
  593. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  594. }
  595. static int update_picture_tables(Picture *dst, Picture *src)
  596. {
  597. int i;
  598. #define UPDATE_TABLE(table)\
  599. do {\
  600. if (src->table &&\
  601. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  602. av_buffer_unref(&dst->table);\
  603. dst->table = av_buffer_ref(src->table);\
  604. if (!dst->table) {\
  605. ff_free_picture_tables(dst);\
  606. return AVERROR(ENOMEM);\
  607. }\
  608. }\
  609. } while (0)
  610. UPDATE_TABLE(mb_var_buf);
  611. UPDATE_TABLE(mc_mb_var_buf);
  612. UPDATE_TABLE(mb_mean_buf);
  613. UPDATE_TABLE(mbskip_table_buf);
  614. UPDATE_TABLE(qscale_table_buf);
  615. UPDATE_TABLE(mb_type_buf);
  616. for (i = 0; i < 2; i++) {
  617. UPDATE_TABLE(motion_val_buf[i]);
  618. UPDATE_TABLE(ref_index_buf[i]);
  619. }
  620. dst->mb_var = src->mb_var;
  621. dst->mc_mb_var = src->mc_mb_var;
  622. dst->mb_mean = src->mb_mean;
  623. dst->mbskip_table = src->mbskip_table;
  624. dst->qscale_table = src->qscale_table;
  625. dst->mb_type = src->mb_type;
  626. for (i = 0; i < 2; i++) {
  627. dst->motion_val[i] = src->motion_val[i];
  628. dst->ref_index[i] = src->ref_index[i];
  629. }
  630. return 0;
  631. }
  632. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  633. {
  634. int ret;
  635. av_assert0(!dst->f->buf[0]);
  636. av_assert0(src->f->buf[0]);
  637. src->tf.f = src->f;
  638. dst->tf.f = dst->f;
  639. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  640. if (ret < 0)
  641. goto fail;
  642. ret = update_picture_tables(dst, src);
  643. if (ret < 0)
  644. goto fail;
  645. if (src->hwaccel_picture_private) {
  646. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  647. if (!dst->hwaccel_priv_buf)
  648. goto fail;
  649. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  650. }
  651. dst->field_picture = src->field_picture;
  652. dst->mb_var_sum = src->mb_var_sum;
  653. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  654. dst->b_frame_score = src->b_frame_score;
  655. dst->needs_realloc = src->needs_realloc;
  656. dst->reference = src->reference;
  657. dst->shared = src->shared;
  658. return 0;
  659. fail:
  660. ff_mpeg_unref_picture(s, dst);
  661. return ret;
  662. }
  663. static void exchange_uv(MpegEncContext *s)
  664. {
  665. int16_t (*tmp)[64];
  666. tmp = s->pblocks[4];
  667. s->pblocks[4] = s->pblocks[5];
  668. s->pblocks[5] = tmp;
  669. }
  670. static int init_duplicate_context(MpegEncContext *s)
  671. {
  672. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  673. int c_size = s->mb_stride * (s->mb_height + 1);
  674. int yc_size = y_size + 2 * c_size;
  675. int i;
  676. s->edge_emu_buffer =
  677. s->me.scratchpad =
  678. s->me.temp =
  679. s->rd_scratchpad =
  680. s->b_scratchpad =
  681. s->obmc_scratchpad = NULL;
  682. if (s->encoding) {
  683. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  684. ME_MAP_SIZE * sizeof(uint32_t), fail)
  685. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  686. ME_MAP_SIZE * sizeof(uint32_t), fail)
  687. if (s->avctx->noise_reduction) {
  688. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  689. 2 * 64 * sizeof(int), fail)
  690. }
  691. }
  692. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  693. s->block = s->blocks[0];
  694. for (i = 0; i < 12; i++) {
  695. s->pblocks[i] = &s->block[i];
  696. }
  697. if (s->avctx->codec_tag == AV_RL32("VCR2"))
  698. exchange_uv(s);
  699. if (s->out_format == FMT_H263) {
  700. /* ac values */
  701. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  702. yc_size * sizeof(int16_t) * 16, fail);
  703. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  704. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  705. s->ac_val[2] = s->ac_val[1] + c_size;
  706. }
  707. return 0;
  708. fail:
  709. return -1; // free() through ff_MPV_common_end()
  710. }
  711. static void free_duplicate_context(MpegEncContext *s)
  712. {
  713. if (s == NULL)
  714. return;
  715. av_freep(&s->edge_emu_buffer);
  716. av_freep(&s->me.scratchpad);
  717. s->me.temp =
  718. s->rd_scratchpad =
  719. s->b_scratchpad =
  720. s->obmc_scratchpad = NULL;
  721. av_freep(&s->dct_error_sum);
  722. av_freep(&s->me.map);
  723. av_freep(&s->me.score_map);
  724. av_freep(&s->blocks);
  725. av_freep(&s->ac_val_base);
  726. s->block = NULL;
  727. }
  728. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  729. {
  730. #define COPY(a) bak->a = src->a
  731. COPY(edge_emu_buffer);
  732. COPY(me.scratchpad);
  733. COPY(me.temp);
  734. COPY(rd_scratchpad);
  735. COPY(b_scratchpad);
  736. COPY(obmc_scratchpad);
  737. COPY(me.map);
  738. COPY(me.score_map);
  739. COPY(blocks);
  740. COPY(block);
  741. COPY(start_mb_y);
  742. COPY(end_mb_y);
  743. COPY(me.map_generation);
  744. COPY(pb);
  745. COPY(dct_error_sum);
  746. COPY(dct_count[0]);
  747. COPY(dct_count[1]);
  748. COPY(ac_val_base);
  749. COPY(ac_val[0]);
  750. COPY(ac_val[1]);
  751. COPY(ac_val[2]);
  752. #undef COPY
  753. }
  754. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  755. {
  756. MpegEncContext bak;
  757. int i, ret;
  758. // FIXME copy only needed parts
  759. // START_TIMER
  760. backup_duplicate_context(&bak, dst);
  761. memcpy(dst, src, sizeof(MpegEncContext));
  762. backup_duplicate_context(dst, &bak);
  763. for (i = 0; i < 12; i++) {
  764. dst->pblocks[i] = &dst->block[i];
  765. }
  766. if (dst->avctx->codec_tag == AV_RL32("VCR2"))
  767. exchange_uv(dst);
  768. if (!dst->edge_emu_buffer &&
  769. (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
  770. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  771. "scratch buffers.\n");
  772. return ret;
  773. }
  774. // STOP_TIMER("update_duplicate_context")
  775. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  776. return 0;
  777. }
  778. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  779. const AVCodecContext *src)
  780. {
  781. int i, ret;
  782. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  783. if (dst == src || !s1->context_initialized)
  784. return 0;
  785. // FIXME can parameters change on I-frames?
  786. // in that case dst may need a reinit
  787. if (!s->context_initialized) {
  788. memcpy(s, s1, sizeof(MpegEncContext));
  789. s->avctx = dst;
  790. s->bitstream_buffer = NULL;
  791. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  792. ff_MPV_common_init(s);
  793. }
  794. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  795. int err;
  796. s->context_reinit = 0;
  797. s->height = s1->height;
  798. s->width = s1->width;
  799. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  800. return err;
  801. }
  802. s->avctx->coded_height = s1->avctx->coded_height;
  803. s->avctx->coded_width = s1->avctx->coded_width;
  804. s->avctx->width = s1->avctx->width;
  805. s->avctx->height = s1->avctx->height;
  806. s->coded_picture_number = s1->coded_picture_number;
  807. s->picture_number = s1->picture_number;
  808. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  809. ff_mpeg_unref_picture(s, &s->picture[i]);
  810. if (s1->picture[i].f->buf[0] &&
  811. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  812. return ret;
  813. }
  814. #define UPDATE_PICTURE(pic)\
  815. do {\
  816. ff_mpeg_unref_picture(s, &s->pic);\
  817. if (s1->pic.f->buf[0])\
  818. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  819. else\
  820. ret = update_picture_tables(&s->pic, &s1->pic);\
  821. if (ret < 0)\
  822. return ret;\
  823. } while (0)
  824. UPDATE_PICTURE(current_picture);
  825. UPDATE_PICTURE(last_picture);
  826. UPDATE_PICTURE(next_picture);
  827. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  828. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  829. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  830. // Error/bug resilience
  831. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  832. s->workaround_bugs = s1->workaround_bugs;
  833. // MPEG4 timing info
  834. memcpy(&s->last_time_base, &s1->last_time_base,
  835. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  836. (char *) &s1->last_time_base);
  837. // B-frame info
  838. s->max_b_frames = s1->max_b_frames;
  839. s->low_delay = s1->low_delay;
  840. s->droppable = s1->droppable;
  841. // DivX handling (doesn't work)
  842. s->divx_packed = s1->divx_packed;
  843. if (s1->bitstream_buffer) {
  844. if (s1->bitstream_buffer_size +
  845. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  846. av_fast_malloc(&s->bitstream_buffer,
  847. &s->allocated_bitstream_buffer_size,
  848. s1->allocated_bitstream_buffer_size);
  849. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  850. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  851. s1->bitstream_buffer_size);
  852. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  853. FF_INPUT_BUFFER_PADDING_SIZE);
  854. }
  855. // linesize dependend scratch buffer allocation
  856. if (!s->edge_emu_buffer)
  857. if (s1->linesize) {
  858. if (frame_size_alloc(s, s1->linesize) < 0) {
  859. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  860. "scratch buffers.\n");
  861. return AVERROR(ENOMEM);
  862. }
  863. } else {
  864. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  865. "be allocated due to unknown size.\n");
  866. return AVERROR_BUG;
  867. }
  868. // MPEG2/interlacing info
  869. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  870. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  871. if (!s1->first_field) {
  872. s->last_pict_type = s1->pict_type;
  873. if (s1->current_picture_ptr)
  874. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  875. }
  876. return 0;
  877. }
  878. /**
  879. * Set the given MpegEncContext to common defaults
  880. * (same for encoding and decoding).
  881. * The changed fields will not depend upon the
  882. * prior state of the MpegEncContext.
  883. */
  884. void ff_MPV_common_defaults(MpegEncContext *s)
  885. {
  886. s->y_dc_scale_table =
  887. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  888. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  889. s->progressive_frame = 1;
  890. s->progressive_sequence = 1;
  891. s->picture_structure = PICT_FRAME;
  892. s->coded_picture_number = 0;
  893. s->picture_number = 0;
  894. s->f_code = 1;
  895. s->b_code = 1;
  896. s->slice_context_count = 1;
  897. }
  898. /**
  899. * Set the given MpegEncContext to defaults for decoding.
  900. * the changed fields will not depend upon
  901. * the prior state of the MpegEncContext.
  902. */
  903. void ff_MPV_decode_defaults(MpegEncContext *s)
  904. {
  905. ff_MPV_common_defaults(s);
  906. }
  907. static int init_er(MpegEncContext *s)
  908. {
  909. ERContext *er = &s->er;
  910. int mb_array_size = s->mb_height * s->mb_stride;
  911. int i;
  912. er->avctx = s->avctx;
  913. er->dsp = &s->dsp;
  914. er->mb_index2xy = s->mb_index2xy;
  915. er->mb_num = s->mb_num;
  916. er->mb_width = s->mb_width;
  917. er->mb_height = s->mb_height;
  918. er->mb_stride = s->mb_stride;
  919. er->b8_stride = s->b8_stride;
  920. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  921. er->error_status_table = av_mallocz(mb_array_size);
  922. if (!er->er_temp_buffer || !er->error_status_table)
  923. goto fail;
  924. er->mbskip_table = s->mbskip_table;
  925. er->mbintra_table = s->mbintra_table;
  926. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  927. er->dc_val[i] = s->dc_val[i];
  928. er->decode_mb = mpeg_er_decode_mb;
  929. er->opaque = s;
  930. return 0;
  931. fail:
  932. av_freep(&er->er_temp_buffer);
  933. av_freep(&er->error_status_table);
  934. return AVERROR(ENOMEM);
  935. }
  936. /**
  937. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  938. */
  939. static int init_context_frame(MpegEncContext *s)
  940. {
  941. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  942. s->mb_width = (s->width + 15) / 16;
  943. s->mb_stride = s->mb_width + 1;
  944. s->b8_stride = s->mb_width * 2 + 1;
  945. mb_array_size = s->mb_height * s->mb_stride;
  946. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  947. /* set default edge pos, will be overriden
  948. * in decode_header if needed */
  949. s->h_edge_pos = s->mb_width * 16;
  950. s->v_edge_pos = s->mb_height * 16;
  951. s->mb_num = s->mb_width * s->mb_height;
  952. s->block_wrap[0] =
  953. s->block_wrap[1] =
  954. s->block_wrap[2] =
  955. s->block_wrap[3] = s->b8_stride;
  956. s->block_wrap[4] =
  957. s->block_wrap[5] = s->mb_stride;
  958. y_size = s->b8_stride * (2 * s->mb_height + 1);
  959. c_size = s->mb_stride * (s->mb_height + 1);
  960. yc_size = y_size + 2 * c_size;
  961. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  962. fail); // error ressilience code looks cleaner with this
  963. for (y = 0; y < s->mb_height; y++)
  964. for (x = 0; x < s->mb_width; x++)
  965. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  966. s->mb_index2xy[s->mb_height * s->mb_width] =
  967. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  968. if (s->encoding) {
  969. /* Allocate MV tables */
  970. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  971. mv_table_size * 2 * sizeof(int16_t), fail);
  972. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  973. mv_table_size * 2 * sizeof(int16_t), fail);
  974. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  975. mv_table_size * 2 * sizeof(int16_t), fail);
  976. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  977. mv_table_size * 2 * sizeof(int16_t), fail);
  978. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  979. mv_table_size * 2 * sizeof(int16_t), fail);
  980. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  981. mv_table_size * 2 * sizeof(int16_t), fail);
  982. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  983. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  984. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  985. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  986. s->mb_stride + 1;
  987. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  988. s->mb_stride + 1;
  989. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  990. /* Allocate MB type table */
  991. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  992. sizeof(uint16_t), fail); // needed for encoding
  993. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  994. sizeof(int), fail);
  995. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  996. mb_array_size * sizeof(float), fail);
  997. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  998. mb_array_size * sizeof(float), fail);
  999. }
  1000. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  1001. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  1002. /* interlaced direct mode decoding tables */
  1003. for (i = 0; i < 2; i++) {
  1004. int j, k;
  1005. for (j = 0; j < 2; j++) {
  1006. for (k = 0; k < 2; k++) {
  1007. FF_ALLOCZ_OR_GOTO(s->avctx,
  1008. s->b_field_mv_table_base[i][j][k],
  1009. mv_table_size * 2 * sizeof(int16_t),
  1010. fail);
  1011. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  1012. s->mb_stride + 1;
  1013. }
  1014. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  1015. mb_array_size * 2 * sizeof(uint8_t), fail);
  1016. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  1017. mv_table_size * 2 * sizeof(int16_t), fail);
  1018. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  1019. + s->mb_stride + 1;
  1020. }
  1021. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  1022. mb_array_size * 2 * sizeof(uint8_t), fail);
  1023. }
  1024. }
  1025. if (s->out_format == FMT_H263) {
  1026. /* cbp values */
  1027. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  1028. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  1029. /* cbp, ac_pred, pred_dir */
  1030. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  1031. mb_array_size * sizeof(uint8_t), fail);
  1032. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  1033. mb_array_size * sizeof(uint8_t), fail);
  1034. }
  1035. if (s->h263_pred || s->h263_plus || !s->encoding) {
  1036. /* dc values */
  1037. // MN: we need these for error resilience of intra-frames
  1038. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  1039. yc_size * sizeof(int16_t), fail);
  1040. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  1041. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  1042. s->dc_val[2] = s->dc_val[1] + c_size;
  1043. for (i = 0; i < yc_size; i++)
  1044. s->dc_val_base[i] = 1024;
  1045. }
  1046. /* which mb is a intra block */
  1047. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  1048. memset(s->mbintra_table, 1, mb_array_size);
  1049. /* init macroblock skip table */
  1050. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  1051. // Note the + 1 is for a quicker mpeg4 slice_end detection
  1052. return init_er(s);
  1053. fail:
  1054. return AVERROR(ENOMEM);
  1055. }
  1056. /**
  1057. * init common structure for both encoder and decoder.
  1058. * this assumes that some variables like width/height are already set
  1059. */
  1060. av_cold int ff_MPV_common_init(MpegEncContext *s)
  1061. {
  1062. int i;
  1063. int nb_slices = (HAVE_THREADS &&
  1064. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  1065. s->avctx->thread_count : 1;
  1066. if (s->encoding && s->avctx->slices)
  1067. nb_slices = s->avctx->slices;
  1068. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1069. s->mb_height = (s->height + 31) / 32 * 2;
  1070. else
  1071. s->mb_height = (s->height + 15) / 16;
  1072. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  1073. av_log(s->avctx, AV_LOG_ERROR,
  1074. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  1075. return -1;
  1076. }
  1077. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  1078. int max_slices;
  1079. if (s->mb_height)
  1080. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  1081. else
  1082. max_slices = MAX_THREADS;
  1083. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  1084. " reducing to %d\n", nb_slices, max_slices);
  1085. nb_slices = max_slices;
  1086. }
  1087. if ((s->width || s->height) &&
  1088. av_image_check_size(s->width, s->height, 0, s->avctx))
  1089. return -1;
  1090. ff_dct_common_init(s);
  1091. s->flags = s->avctx->flags;
  1092. s->flags2 = s->avctx->flags2;
  1093. /* set chroma shifts */
  1094. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1095. &s->chroma_x_shift,
  1096. &s->chroma_y_shift);
  1097. /* convert fourcc to upper case */
  1098. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  1099. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  1100. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  1101. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  1102. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1103. s->picture[i].f = av_frame_alloc();
  1104. if (!s->picture[i].f)
  1105. goto fail;
  1106. }
  1107. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1108. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1109. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1110. memset(&s->new_picture, 0, sizeof(s->new_picture));
  1111. s->next_picture.f = av_frame_alloc();
  1112. if (!s->next_picture.f)
  1113. goto fail;
  1114. s->last_picture.f = av_frame_alloc();
  1115. if (!s->last_picture.f)
  1116. goto fail;
  1117. s->current_picture.f = av_frame_alloc();
  1118. if (!s->current_picture.f)
  1119. goto fail;
  1120. s->new_picture.f = av_frame_alloc();
  1121. if (!s->new_picture.f)
  1122. goto fail;
  1123. if (s->width && s->height) {
  1124. if (init_context_frame(s))
  1125. goto fail;
  1126. s->parse_context.state = -1;
  1127. }
  1128. s->context_initialized = 1;
  1129. s->thread_context[0] = s;
  1130. if (s->width && s->height) {
  1131. if (nb_slices > 1) {
  1132. for (i = 1; i < nb_slices; i++) {
  1133. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1134. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1135. }
  1136. for (i = 0; i < nb_slices; i++) {
  1137. if (init_duplicate_context(s->thread_context[i]) < 0)
  1138. goto fail;
  1139. s->thread_context[i]->start_mb_y =
  1140. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1141. s->thread_context[i]->end_mb_y =
  1142. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1143. }
  1144. } else {
  1145. if (init_duplicate_context(s) < 0)
  1146. goto fail;
  1147. s->start_mb_y = 0;
  1148. s->end_mb_y = s->mb_height;
  1149. }
  1150. s->slice_context_count = nb_slices;
  1151. }
  1152. return 0;
  1153. fail:
  1154. ff_MPV_common_end(s);
  1155. return -1;
  1156. }
  1157. /**
  1158. * Frees and resets MpegEncContext fields depending on the resolution.
  1159. * Is used during resolution changes to avoid a full reinitialization of the
  1160. * codec.
  1161. */
  1162. static int free_context_frame(MpegEncContext *s)
  1163. {
  1164. int i, j, k;
  1165. av_freep(&s->mb_type);
  1166. av_freep(&s->p_mv_table_base);
  1167. av_freep(&s->b_forw_mv_table_base);
  1168. av_freep(&s->b_back_mv_table_base);
  1169. av_freep(&s->b_bidir_forw_mv_table_base);
  1170. av_freep(&s->b_bidir_back_mv_table_base);
  1171. av_freep(&s->b_direct_mv_table_base);
  1172. s->p_mv_table = NULL;
  1173. s->b_forw_mv_table = NULL;
  1174. s->b_back_mv_table = NULL;
  1175. s->b_bidir_forw_mv_table = NULL;
  1176. s->b_bidir_back_mv_table = NULL;
  1177. s->b_direct_mv_table = NULL;
  1178. for (i = 0; i < 2; i++) {
  1179. for (j = 0; j < 2; j++) {
  1180. for (k = 0; k < 2; k++) {
  1181. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1182. s->b_field_mv_table[i][j][k] = NULL;
  1183. }
  1184. av_freep(&s->b_field_select_table[i][j]);
  1185. av_freep(&s->p_field_mv_table_base[i][j]);
  1186. s->p_field_mv_table[i][j] = NULL;
  1187. }
  1188. av_freep(&s->p_field_select_table[i]);
  1189. }
  1190. av_freep(&s->dc_val_base);
  1191. av_freep(&s->coded_block_base);
  1192. av_freep(&s->mbintra_table);
  1193. av_freep(&s->cbp_table);
  1194. av_freep(&s->pred_dir_table);
  1195. av_freep(&s->mbskip_table);
  1196. av_freep(&s->er.error_status_table);
  1197. av_freep(&s->er.er_temp_buffer);
  1198. av_freep(&s->mb_index2xy);
  1199. av_freep(&s->lambda_table);
  1200. av_freep(&s->cplx_tab);
  1201. av_freep(&s->bits_tab);
  1202. s->linesize = s->uvlinesize = 0;
  1203. return 0;
  1204. }
  1205. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1206. {
  1207. int i, err = 0;
  1208. if (s->slice_context_count > 1) {
  1209. for (i = 0; i < s->slice_context_count; i++) {
  1210. free_duplicate_context(s->thread_context[i]);
  1211. }
  1212. for (i = 1; i < s->slice_context_count; i++) {
  1213. av_freep(&s->thread_context[i]);
  1214. }
  1215. } else
  1216. free_duplicate_context(s);
  1217. if ((err = free_context_frame(s)) < 0)
  1218. return err;
  1219. if (s->picture)
  1220. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1221. s->picture[i].needs_realloc = 1;
  1222. }
  1223. s->last_picture_ptr =
  1224. s->next_picture_ptr =
  1225. s->current_picture_ptr = NULL;
  1226. // init
  1227. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1228. s->mb_height = (s->height + 31) / 32 * 2;
  1229. else
  1230. s->mb_height = (s->height + 15) / 16;
  1231. if ((s->width || s->height) &&
  1232. av_image_check_size(s->width, s->height, 0, s->avctx))
  1233. return AVERROR_INVALIDDATA;
  1234. if ((err = init_context_frame(s)))
  1235. goto fail;
  1236. s->thread_context[0] = s;
  1237. if (s->width && s->height) {
  1238. int nb_slices = s->slice_context_count;
  1239. if (nb_slices > 1) {
  1240. for (i = 1; i < nb_slices; i++) {
  1241. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1242. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1243. }
  1244. for (i = 0; i < nb_slices; i++) {
  1245. if (init_duplicate_context(s->thread_context[i]) < 0)
  1246. goto fail;
  1247. s->thread_context[i]->start_mb_y =
  1248. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1249. s->thread_context[i]->end_mb_y =
  1250. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1251. }
  1252. } else {
  1253. if (init_duplicate_context(s) < 0)
  1254. goto fail;
  1255. s->start_mb_y = 0;
  1256. s->end_mb_y = s->mb_height;
  1257. }
  1258. s->slice_context_count = nb_slices;
  1259. }
  1260. return 0;
  1261. fail:
  1262. ff_MPV_common_end(s);
  1263. return err;
  1264. }
  1265. /* init common structure for both encoder and decoder */
  1266. void ff_MPV_common_end(MpegEncContext *s)
  1267. {
  1268. int i;
  1269. if (s->slice_context_count > 1) {
  1270. for (i = 0; i < s->slice_context_count; i++) {
  1271. free_duplicate_context(s->thread_context[i]);
  1272. }
  1273. for (i = 1; i < s->slice_context_count; i++) {
  1274. av_freep(&s->thread_context[i]);
  1275. }
  1276. s->slice_context_count = 1;
  1277. } else free_duplicate_context(s);
  1278. av_freep(&s->parse_context.buffer);
  1279. s->parse_context.buffer_size = 0;
  1280. av_freep(&s->bitstream_buffer);
  1281. s->allocated_bitstream_buffer_size = 0;
  1282. if (s->picture) {
  1283. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1284. ff_free_picture_tables(&s->picture[i]);
  1285. ff_mpeg_unref_picture(s, &s->picture[i]);
  1286. av_frame_free(&s->picture[i].f);
  1287. }
  1288. }
  1289. av_freep(&s->picture);
  1290. ff_free_picture_tables(&s->last_picture);
  1291. ff_mpeg_unref_picture(s, &s->last_picture);
  1292. av_frame_free(&s->last_picture.f);
  1293. ff_free_picture_tables(&s->current_picture);
  1294. ff_mpeg_unref_picture(s, &s->current_picture);
  1295. av_frame_free(&s->current_picture.f);
  1296. ff_free_picture_tables(&s->next_picture);
  1297. ff_mpeg_unref_picture(s, &s->next_picture);
  1298. av_frame_free(&s->next_picture.f);
  1299. ff_free_picture_tables(&s->new_picture);
  1300. ff_mpeg_unref_picture(s, &s->new_picture);
  1301. av_frame_free(&s->new_picture.f);
  1302. free_context_frame(s);
  1303. s->context_initialized = 0;
  1304. s->last_picture_ptr =
  1305. s->next_picture_ptr =
  1306. s->current_picture_ptr = NULL;
  1307. s->linesize = s->uvlinesize = 0;
  1308. }
  1309. av_cold void ff_init_rl(RLTable *rl,
  1310. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1311. {
  1312. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1313. uint8_t index_run[MAX_RUN + 1];
  1314. int last, run, level, start, end, i;
  1315. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1316. if (static_store && rl->max_level[0])
  1317. return;
  1318. /* compute max_level[], max_run[] and index_run[] */
  1319. for (last = 0; last < 2; last++) {
  1320. if (last == 0) {
  1321. start = 0;
  1322. end = rl->last;
  1323. } else {
  1324. start = rl->last;
  1325. end = rl->n;
  1326. }
  1327. memset(max_level, 0, MAX_RUN + 1);
  1328. memset(max_run, 0, MAX_LEVEL + 1);
  1329. memset(index_run, rl->n, MAX_RUN + 1);
  1330. for (i = start; i < end; i++) {
  1331. run = rl->table_run[i];
  1332. level = rl->table_level[i];
  1333. if (index_run[run] == rl->n)
  1334. index_run[run] = i;
  1335. if (level > max_level[run])
  1336. max_level[run] = level;
  1337. if (run > max_run[level])
  1338. max_run[level] = run;
  1339. }
  1340. if (static_store)
  1341. rl->max_level[last] = static_store[last];
  1342. else
  1343. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1344. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1345. if (static_store)
  1346. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1347. else
  1348. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1349. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1350. if (static_store)
  1351. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1352. else
  1353. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1354. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1355. }
  1356. }
  1357. av_cold void ff_init_vlc_rl(RLTable *rl)
  1358. {
  1359. int i, q;
  1360. for (q = 0; q < 32; q++) {
  1361. int qmul = q * 2;
  1362. int qadd = (q - 1) | 1;
  1363. if (q == 0) {
  1364. qmul = 1;
  1365. qadd = 0;
  1366. }
  1367. for (i = 0; i < rl->vlc.table_size; i++) {
  1368. int code = rl->vlc.table[i][0];
  1369. int len = rl->vlc.table[i][1];
  1370. int level, run;
  1371. if (len == 0) { // illegal code
  1372. run = 66;
  1373. level = MAX_LEVEL;
  1374. } else if (len < 0) { // more bits needed
  1375. run = 0;
  1376. level = code;
  1377. } else {
  1378. if (code == rl->n) { // esc
  1379. run = 66;
  1380. level = 0;
  1381. } else {
  1382. run = rl->table_run[code] + 1;
  1383. level = rl->table_level[code] * qmul + qadd;
  1384. if (code >= rl->last) run += 192;
  1385. }
  1386. }
  1387. rl->rl_vlc[q][i].len = len;
  1388. rl->rl_vlc[q][i].level = level;
  1389. rl->rl_vlc[q][i].run = run;
  1390. }
  1391. }
  1392. }
  1393. static void release_unused_pictures(MpegEncContext *s)
  1394. {
  1395. int i;
  1396. /* release non reference frames */
  1397. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1398. if (!s->picture[i].reference)
  1399. ff_mpeg_unref_picture(s, &s->picture[i]);
  1400. }
  1401. }
  1402. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1403. {
  1404. if (pic->f->buf[0] == NULL)
  1405. return 1;
  1406. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1407. return 1;
  1408. return 0;
  1409. }
  1410. static int find_unused_picture(MpegEncContext *s, int shared)
  1411. {
  1412. int i;
  1413. if (shared) {
  1414. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1415. if (s->picture[i].f->buf[0] == NULL)
  1416. return i;
  1417. }
  1418. } else {
  1419. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1420. if (pic_is_unused(s, &s->picture[i]))
  1421. return i;
  1422. }
  1423. }
  1424. return AVERROR_INVALIDDATA;
  1425. }
  1426. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1427. {
  1428. int ret = find_unused_picture(s, shared);
  1429. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1430. if (s->picture[ret].needs_realloc) {
  1431. s->picture[ret].needs_realloc = 0;
  1432. ff_free_picture_tables(&s->picture[ret]);
  1433. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1434. }
  1435. }
  1436. return ret;
  1437. }
  1438. /**
  1439. * generic function called after decoding
  1440. * the header and before a frame is decoded.
  1441. */
  1442. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1443. {
  1444. int i, ret;
  1445. Picture *pic;
  1446. s->mb_skipped = 0;
  1447. /* mark & release old frames */
  1448. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1449. s->last_picture_ptr != s->next_picture_ptr &&
  1450. s->last_picture_ptr->f->buf[0]) {
  1451. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1452. }
  1453. /* release forgotten pictures */
  1454. /* if (mpeg124/h263) */
  1455. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1456. if (&s->picture[i] != s->last_picture_ptr &&
  1457. &s->picture[i] != s->next_picture_ptr &&
  1458. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1459. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1460. av_log(avctx, AV_LOG_ERROR,
  1461. "releasing zombie picture\n");
  1462. ff_mpeg_unref_picture(s, &s->picture[i]);
  1463. }
  1464. }
  1465. ff_mpeg_unref_picture(s, &s->current_picture);
  1466. release_unused_pictures(s);
  1467. if (s->current_picture_ptr &&
  1468. s->current_picture_ptr->f->buf[0] == NULL) {
  1469. // we already have a unused image
  1470. // (maybe it was set before reading the header)
  1471. pic = s->current_picture_ptr;
  1472. } else {
  1473. i = ff_find_unused_picture(s, 0);
  1474. if (i < 0) {
  1475. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1476. return i;
  1477. }
  1478. pic = &s->picture[i];
  1479. }
  1480. pic->reference = 0;
  1481. if (!s->droppable) {
  1482. if (s->pict_type != AV_PICTURE_TYPE_B)
  1483. pic->reference = 3;
  1484. }
  1485. pic->f->coded_picture_number = s->coded_picture_number++;
  1486. if (ff_alloc_picture(s, pic, 0) < 0)
  1487. return -1;
  1488. s->current_picture_ptr = pic;
  1489. // FIXME use only the vars from current_pic
  1490. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1491. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1492. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1493. if (s->picture_structure != PICT_FRAME)
  1494. s->current_picture_ptr->f->top_field_first =
  1495. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1496. }
  1497. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1498. !s->progressive_sequence;
  1499. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1500. s->current_picture_ptr->f->pict_type = s->pict_type;
  1501. // if (s->flags && CODEC_FLAG_QSCALE)
  1502. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1503. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1504. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1505. s->current_picture_ptr)) < 0)
  1506. return ret;
  1507. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1508. s->last_picture_ptr = s->next_picture_ptr;
  1509. if (!s->droppable)
  1510. s->next_picture_ptr = s->current_picture_ptr;
  1511. }
  1512. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1513. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1514. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1515. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1516. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1517. s->pict_type, s->droppable);
  1518. if ((s->last_picture_ptr == NULL ||
  1519. s->last_picture_ptr->f->buf[0] == NULL) &&
  1520. (s->pict_type != AV_PICTURE_TYPE_I ||
  1521. s->picture_structure != PICT_FRAME)) {
  1522. int h_chroma_shift, v_chroma_shift;
  1523. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1524. &h_chroma_shift, &v_chroma_shift);
  1525. if (s->pict_type != AV_PICTURE_TYPE_I)
  1526. av_log(avctx, AV_LOG_ERROR,
  1527. "warning: first frame is no keyframe\n");
  1528. else if (s->picture_structure != PICT_FRAME)
  1529. av_log(avctx, AV_LOG_INFO,
  1530. "allocate dummy last picture for field based first keyframe\n");
  1531. /* Allocate a dummy frame */
  1532. i = ff_find_unused_picture(s, 0);
  1533. if (i < 0) {
  1534. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1535. return i;
  1536. }
  1537. s->last_picture_ptr = &s->picture[i];
  1538. s->last_picture_ptr->reference = 3;
  1539. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1540. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1541. s->last_picture_ptr = NULL;
  1542. return -1;
  1543. }
  1544. memset(s->last_picture_ptr->f->data[0], 0,
  1545. avctx->height * s->last_picture_ptr->f->linesize[0]);
  1546. memset(s->last_picture_ptr->f->data[1], 0x80,
  1547. (avctx->height >> v_chroma_shift) *
  1548. s->last_picture_ptr->f->linesize[1]);
  1549. memset(s->last_picture_ptr->f->data[2], 0x80,
  1550. (avctx->height >> v_chroma_shift) *
  1551. s->last_picture_ptr->f->linesize[2]);
  1552. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1553. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1554. }
  1555. if ((s->next_picture_ptr == NULL ||
  1556. s->next_picture_ptr->f->buf[0] == NULL) &&
  1557. s->pict_type == AV_PICTURE_TYPE_B) {
  1558. /* Allocate a dummy frame */
  1559. i = ff_find_unused_picture(s, 0);
  1560. if (i < 0) {
  1561. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1562. return i;
  1563. }
  1564. s->next_picture_ptr = &s->picture[i];
  1565. s->next_picture_ptr->reference = 3;
  1566. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1567. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1568. s->next_picture_ptr = NULL;
  1569. return -1;
  1570. }
  1571. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1572. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1573. }
  1574. if (s->last_picture_ptr) {
  1575. ff_mpeg_unref_picture(s, &s->last_picture);
  1576. if (s->last_picture_ptr->f->buf[0] &&
  1577. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1578. s->last_picture_ptr)) < 0)
  1579. return ret;
  1580. }
  1581. if (s->next_picture_ptr) {
  1582. ff_mpeg_unref_picture(s, &s->next_picture);
  1583. if (s->next_picture_ptr->f->buf[0] &&
  1584. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1585. s->next_picture_ptr)) < 0)
  1586. return ret;
  1587. }
  1588. if (s->pict_type != AV_PICTURE_TYPE_I &&
  1589. !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
  1590. av_log(s, AV_LOG_ERROR,
  1591. "Non-reference picture received and no reference available\n");
  1592. return AVERROR_INVALIDDATA;
  1593. }
  1594. if (s->picture_structure!= PICT_FRAME) {
  1595. int i;
  1596. for (i = 0; i < 4; i++) {
  1597. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1598. s->current_picture.f->data[i] +=
  1599. s->current_picture.f->linesize[i];
  1600. }
  1601. s->current_picture.f->linesize[i] *= 2;
  1602. s->last_picture.f->linesize[i] *= 2;
  1603. s->next_picture.f->linesize[i] *= 2;
  1604. }
  1605. }
  1606. s->err_recognition = avctx->err_recognition;
  1607. /* set dequantizer, we can't do it during init as
  1608. * it might change for mpeg4 and we can't do it in the header
  1609. * decode as init is not called for mpeg4 there yet */
  1610. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1611. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1612. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1613. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1614. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1615. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1616. } else {
  1617. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1618. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1619. }
  1620. #if FF_API_XVMC
  1621. FF_DISABLE_DEPRECATION_WARNINGS
  1622. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1623. return ff_xvmc_field_start(s, avctx);
  1624. FF_ENABLE_DEPRECATION_WARNINGS
  1625. #endif /* FF_API_XVMC */
  1626. return 0;
  1627. }
  1628. /* called after a frame has been decoded. */
  1629. void ff_MPV_frame_end(MpegEncContext *s)
  1630. {
  1631. #if FF_API_XVMC
  1632. FF_DISABLE_DEPRECATION_WARNINGS
  1633. /* redraw edges for the frame if decoding didn't complete */
  1634. // just to make sure that all data is rendered.
  1635. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1636. ff_xvmc_field_end(s);
  1637. } else
  1638. FF_ENABLE_DEPRECATION_WARNINGS
  1639. #endif /* FF_API_XVMC */
  1640. emms_c();
  1641. if (s->current_picture.reference)
  1642. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1643. }
  1644. /**
  1645. * Print debugging info for the given picture.
  1646. */
  1647. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1648. {
  1649. AVFrame *pict;
  1650. if (s->avctx->hwaccel || !p || !p->mb_type)
  1651. return;
  1652. pict = p->f;
  1653. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1654. int x,y;
  1655. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1656. switch (pict->pict_type) {
  1657. case AV_PICTURE_TYPE_I:
  1658. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1659. break;
  1660. case AV_PICTURE_TYPE_P:
  1661. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1662. break;
  1663. case AV_PICTURE_TYPE_B:
  1664. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1665. break;
  1666. case AV_PICTURE_TYPE_S:
  1667. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1668. break;
  1669. case AV_PICTURE_TYPE_SI:
  1670. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1671. break;
  1672. case AV_PICTURE_TYPE_SP:
  1673. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1674. break;
  1675. }
  1676. for (y = 0; y < s->mb_height; y++) {
  1677. for (x = 0; x < s->mb_width; x++) {
  1678. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1679. int count = s->mbskip_table[x + y * s->mb_stride];
  1680. if (count > 9)
  1681. count = 9;
  1682. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1683. }
  1684. if (s->avctx->debug & FF_DEBUG_QP) {
  1685. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1686. p->qscale_table[x + y * s->mb_stride]);
  1687. }
  1688. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1689. int mb_type = p->mb_type[x + y * s->mb_stride];
  1690. // Type & MV direction
  1691. if (IS_PCM(mb_type))
  1692. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1693. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1694. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1695. else if (IS_INTRA4x4(mb_type))
  1696. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1697. else if (IS_INTRA16x16(mb_type))
  1698. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1699. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1700. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1701. else if (IS_DIRECT(mb_type))
  1702. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1703. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1704. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1705. else if (IS_GMC(mb_type))
  1706. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1707. else if (IS_SKIP(mb_type))
  1708. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1709. else if (!USES_LIST(mb_type, 1))
  1710. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1711. else if (!USES_LIST(mb_type, 0))
  1712. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1713. else {
  1714. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1715. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1716. }
  1717. // segmentation
  1718. if (IS_8X8(mb_type))
  1719. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1720. else if (IS_16X8(mb_type))
  1721. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1722. else if (IS_8X16(mb_type))
  1723. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1724. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1725. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1726. else
  1727. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1728. if (IS_INTERLACED(mb_type))
  1729. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1730. else
  1731. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1732. }
  1733. }
  1734. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1735. }
  1736. }
  1737. }
  1738. /**
  1739. * find the lowest MB row referenced in the MVs
  1740. */
  1741. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1742. {
  1743. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1744. int my, off, i, mvs;
  1745. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1746. goto unhandled;
  1747. switch (s->mv_type) {
  1748. case MV_TYPE_16X16:
  1749. mvs = 1;
  1750. break;
  1751. case MV_TYPE_16X8:
  1752. mvs = 2;
  1753. break;
  1754. case MV_TYPE_8X8:
  1755. mvs = 4;
  1756. break;
  1757. default:
  1758. goto unhandled;
  1759. }
  1760. for (i = 0; i < mvs; i++) {
  1761. my = s->mv[dir][i][1]<<qpel_shift;
  1762. my_max = FFMAX(my_max, my);
  1763. my_min = FFMIN(my_min, my);
  1764. }
  1765. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1766. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1767. unhandled:
  1768. return s->mb_height-1;
  1769. }
  1770. /* put block[] to dest[] */
  1771. static inline void put_dct(MpegEncContext *s,
  1772. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1773. {
  1774. s->dct_unquantize_intra(s, block, i, qscale);
  1775. s->idsp.idct_put(dest, line_size, block);
  1776. }
  1777. /* add block[] to dest[] */
  1778. static inline void add_dct(MpegEncContext *s,
  1779. int16_t *block, int i, uint8_t *dest, int line_size)
  1780. {
  1781. if (s->block_last_index[i] >= 0) {
  1782. s->idsp.idct_add(dest, line_size, block);
  1783. }
  1784. }
  1785. static inline void add_dequant_dct(MpegEncContext *s,
  1786. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1787. {
  1788. if (s->block_last_index[i] >= 0) {
  1789. s->dct_unquantize_inter(s, block, i, qscale);
  1790. s->idsp.idct_add(dest, line_size, block);
  1791. }
  1792. }
  1793. /**
  1794. * Clean dc, ac, coded_block for the current non-intra MB.
  1795. */
  1796. void ff_clean_intra_table_entries(MpegEncContext *s)
  1797. {
  1798. int wrap = s->b8_stride;
  1799. int xy = s->block_index[0];
  1800. s->dc_val[0][xy ] =
  1801. s->dc_val[0][xy + 1 ] =
  1802. s->dc_val[0][xy + wrap] =
  1803. s->dc_val[0][xy + 1 + wrap] = 1024;
  1804. /* ac pred */
  1805. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1806. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1807. if (s->msmpeg4_version>=3) {
  1808. s->coded_block[xy ] =
  1809. s->coded_block[xy + 1 ] =
  1810. s->coded_block[xy + wrap] =
  1811. s->coded_block[xy + 1 + wrap] = 0;
  1812. }
  1813. /* chroma */
  1814. wrap = s->mb_stride;
  1815. xy = s->mb_x + s->mb_y * wrap;
  1816. s->dc_val[1][xy] =
  1817. s->dc_val[2][xy] = 1024;
  1818. /* ac pred */
  1819. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1820. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1821. s->mbintra_table[xy]= 0;
  1822. }
  1823. /* generic function called after a macroblock has been parsed by the
  1824. decoder or after it has been encoded by the encoder.
  1825. Important variables used:
  1826. s->mb_intra : true if intra macroblock
  1827. s->mv_dir : motion vector direction
  1828. s->mv_type : motion vector type
  1829. s->mv : motion vector
  1830. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1831. */
  1832. static av_always_inline
  1833. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1834. int is_mpeg12)
  1835. {
  1836. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1837. #if FF_API_XVMC
  1838. FF_DISABLE_DEPRECATION_WARNINGS
  1839. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1840. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1841. return;
  1842. }
  1843. FF_ENABLE_DEPRECATION_WARNINGS
  1844. #endif /* FF_API_XVMC */
  1845. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1846. /* print DCT coefficients */
  1847. int i,j;
  1848. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1849. for(i=0; i<6; i++){
  1850. for(j=0; j<64; j++){
  1851. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  1852. block[i][s->idsp.idct_permutation[j]]);
  1853. }
  1854. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1855. }
  1856. }
  1857. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1858. /* update DC predictors for P macroblocks */
  1859. if (!s->mb_intra) {
  1860. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1861. if(s->mbintra_table[mb_xy])
  1862. ff_clean_intra_table_entries(s);
  1863. } else {
  1864. s->last_dc[0] =
  1865. s->last_dc[1] =
  1866. s->last_dc[2] = 128 << s->intra_dc_precision;
  1867. }
  1868. }
  1869. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1870. s->mbintra_table[mb_xy]=1;
  1871. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1872. uint8_t *dest_y, *dest_cb, *dest_cr;
  1873. int dct_linesize, dct_offset;
  1874. op_pixels_func (*op_pix)[4];
  1875. qpel_mc_func (*op_qpix)[16];
  1876. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1877. const int uvlinesize = s->current_picture.f->linesize[1];
  1878. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1879. const int block_size = 8;
  1880. /* avoid copy if macroblock skipped in last frame too */
  1881. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1882. if(!s->encoding){
  1883. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1884. if (s->mb_skipped) {
  1885. s->mb_skipped= 0;
  1886. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1887. *mbskip_ptr = 1;
  1888. } else if(!s->current_picture.reference) {
  1889. *mbskip_ptr = 1;
  1890. } else{
  1891. *mbskip_ptr = 0; /* not skipped */
  1892. }
  1893. }
  1894. dct_linesize = linesize << s->interlaced_dct;
  1895. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1896. if(readable){
  1897. dest_y= s->dest[0];
  1898. dest_cb= s->dest[1];
  1899. dest_cr= s->dest[2];
  1900. }else{
  1901. dest_y = s->b_scratchpad;
  1902. dest_cb= s->b_scratchpad+16*linesize;
  1903. dest_cr= s->b_scratchpad+32*linesize;
  1904. }
  1905. if (!s->mb_intra) {
  1906. /* motion handling */
  1907. /* decoding or more than one mb_type (MC was already done otherwise) */
  1908. if(!s->encoding){
  1909. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1910. if (s->mv_dir & MV_DIR_FORWARD) {
  1911. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1912. ff_MPV_lowest_referenced_row(s, 0),
  1913. 0);
  1914. }
  1915. if (s->mv_dir & MV_DIR_BACKWARD) {
  1916. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1917. ff_MPV_lowest_referenced_row(s, 1),
  1918. 0);
  1919. }
  1920. }
  1921. op_qpix= s->me.qpel_put;
  1922. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1923. op_pix = s->hdsp.put_pixels_tab;
  1924. }else{
  1925. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1926. }
  1927. if (s->mv_dir & MV_DIR_FORWARD) {
  1928. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  1929. op_pix = s->hdsp.avg_pixels_tab;
  1930. op_qpix= s->me.qpel_avg;
  1931. }
  1932. if (s->mv_dir & MV_DIR_BACKWARD) {
  1933. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  1934. }
  1935. }
  1936. /* skip dequant / idct if we are really late ;) */
  1937. if(s->avctx->skip_idct){
  1938. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1939. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1940. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1941. goto skip_idct;
  1942. }
  1943. /* add dct residue */
  1944. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1945. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1946. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1947. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1948. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1949. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1950. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1951. if (s->chroma_y_shift){
  1952. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1953. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1954. }else{
  1955. dct_linesize >>= 1;
  1956. dct_offset >>=1;
  1957. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1958. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1959. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1960. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1961. }
  1962. }
  1963. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1964. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1965. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1966. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1967. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1968. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1969. if(s->chroma_y_shift){//Chroma420
  1970. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1971. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1972. }else{
  1973. //chroma422
  1974. dct_linesize = uvlinesize << s->interlaced_dct;
  1975. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1976. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1977. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1978. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1979. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1980. if(!s->chroma_x_shift){//Chroma444
  1981. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1982. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1983. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1984. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1985. }
  1986. }
  1987. }//fi gray
  1988. }
  1989. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1990. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1991. }
  1992. } else {
  1993. /* dct only in intra block */
  1994. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1995. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1996. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1997. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1998. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1999. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2000. if(s->chroma_y_shift){
  2001. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2002. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2003. }else{
  2004. dct_offset >>=1;
  2005. dct_linesize >>=1;
  2006. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2007. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2008. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2009. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2010. }
  2011. }
  2012. }else{
  2013. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  2014. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2015. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  2016. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2017. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2018. if(s->chroma_y_shift){
  2019. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  2020. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  2021. }else{
  2022. dct_linesize = uvlinesize << s->interlaced_dct;
  2023. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  2024. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  2025. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  2026. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2027. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2028. if(!s->chroma_x_shift){//Chroma444
  2029. s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  2030. s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  2031. s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  2032. s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  2033. }
  2034. }
  2035. }//gray
  2036. }
  2037. }
  2038. skip_idct:
  2039. if(!readable){
  2040. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2041. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2042. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2043. }
  2044. }
  2045. }
  2046. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2047. #if !CONFIG_SMALL
  2048. if(s->out_format == FMT_MPEG1) {
  2049. MPV_decode_mb_internal(s, block, 1);
  2050. } else
  2051. #endif
  2052. MPV_decode_mb_internal(s, block, 0);
  2053. }
  2054. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2055. {
  2056. ff_draw_horiz_band(s->avctx, s->current_picture.f,
  2057. s->last_picture.f, y, h, s->picture_structure,
  2058. s->first_field, s->low_delay);
  2059. }
  2060. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2061. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2062. const int uvlinesize = s->current_picture.f->linesize[1];
  2063. const int mb_size= 4;
  2064. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2065. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2066. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2067. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2068. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2069. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2070. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2071. s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
  2072. s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2073. s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2074. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2075. {
  2076. if(s->picture_structure==PICT_FRAME){
  2077. s->dest[0] += s->mb_y * linesize << mb_size;
  2078. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2079. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2080. }else{
  2081. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2082. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2083. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2084. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2085. }
  2086. }
  2087. }
  2088. /**
  2089. * Permute an 8x8 block.
  2090. * @param block the block which will be permuted according to the given permutation vector
  2091. * @param permutation the permutation vector
  2092. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2093. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2094. * (inverse) permutated to scantable order!
  2095. */
  2096. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2097. {
  2098. int i;
  2099. int16_t temp[64];
  2100. if(last<=0) return;
  2101. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2102. for(i=0; i<=last; i++){
  2103. const int j= scantable[i];
  2104. temp[j]= block[j];
  2105. block[j]=0;
  2106. }
  2107. for(i=0; i<=last; i++){
  2108. const int j= scantable[i];
  2109. const int perm_j= permutation[j];
  2110. block[perm_j]= temp[j];
  2111. }
  2112. }
  2113. void ff_mpeg_flush(AVCodecContext *avctx){
  2114. int i;
  2115. MpegEncContext *s = avctx->priv_data;
  2116. if(s==NULL || s->picture==NULL)
  2117. return;
  2118. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2119. ff_mpeg_unref_picture(s, &s->picture[i]);
  2120. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2121. ff_mpeg_unref_picture(s, &s->current_picture);
  2122. ff_mpeg_unref_picture(s, &s->last_picture);
  2123. ff_mpeg_unref_picture(s, &s->next_picture);
  2124. s->mb_x= s->mb_y= 0;
  2125. s->parse_context.state= -1;
  2126. s->parse_context.frame_start_found= 0;
  2127. s->parse_context.overread= 0;
  2128. s->parse_context.overread_index= 0;
  2129. s->parse_context.index= 0;
  2130. s->parse_context.last_index= 0;
  2131. s->bitstream_buffer_size=0;
  2132. s->pp_time=0;
  2133. }
  2134. /**
  2135. * set qscale and update qscale dependent variables.
  2136. */
  2137. void ff_set_qscale(MpegEncContext * s, int qscale)
  2138. {
  2139. if (qscale < 1)
  2140. qscale = 1;
  2141. else if (qscale > 31)
  2142. qscale = 31;
  2143. s->qscale = qscale;
  2144. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2145. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2146. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2147. }
  2148. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2149. {
  2150. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2151. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2152. }