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.

2463 lines
83KB

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