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.

2501 lines
85KB

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