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.

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