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.

2500 lines
84KB

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