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.

2482 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 "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 if (pic->f)
  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. s->picture[i].f = av_frame_alloc();
  1080. if (!s->picture[i].f)
  1081. goto fail;
  1082. }
  1083. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1084. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1085. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1086. memset(&s->new_picture, 0, sizeof(s->new_picture));
  1087. s->next_picture.f = av_frame_alloc();
  1088. if (!s->next_picture.f)
  1089. goto fail;
  1090. s->last_picture.f = av_frame_alloc();
  1091. if (!s->last_picture.f)
  1092. goto fail;
  1093. s->current_picture.f = av_frame_alloc();
  1094. if (!s->current_picture.f)
  1095. goto fail;
  1096. s->new_picture.f = av_frame_alloc();
  1097. if (!s->new_picture.f)
  1098. goto fail;
  1099. if (s->width && s->height) {
  1100. if (init_context_frame(s))
  1101. goto fail;
  1102. s->parse_context.state = -1;
  1103. }
  1104. s->context_initialized = 1;
  1105. s->thread_context[0] = s;
  1106. if (s->width && s->height) {
  1107. if (nb_slices > 1) {
  1108. for (i = 1; i < nb_slices; i++) {
  1109. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1110. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1111. }
  1112. for (i = 0; i < nb_slices; i++) {
  1113. if (init_duplicate_context(s->thread_context[i]) < 0)
  1114. goto fail;
  1115. s->thread_context[i]->start_mb_y =
  1116. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1117. s->thread_context[i]->end_mb_y =
  1118. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1119. }
  1120. } else {
  1121. if (init_duplicate_context(s) < 0)
  1122. goto fail;
  1123. s->start_mb_y = 0;
  1124. s->end_mb_y = s->mb_height;
  1125. }
  1126. s->slice_context_count = nb_slices;
  1127. }
  1128. return 0;
  1129. fail:
  1130. ff_MPV_common_end(s);
  1131. return -1;
  1132. }
  1133. /**
  1134. * Frees and resets MpegEncContext fields depending on the resolution.
  1135. * Is used during resolution changes to avoid a full reinitialization of the
  1136. * codec.
  1137. */
  1138. static int free_context_frame(MpegEncContext *s)
  1139. {
  1140. int i, j, k;
  1141. av_freep(&s->mb_type);
  1142. av_freep(&s->p_mv_table_base);
  1143. av_freep(&s->b_forw_mv_table_base);
  1144. av_freep(&s->b_back_mv_table_base);
  1145. av_freep(&s->b_bidir_forw_mv_table_base);
  1146. av_freep(&s->b_bidir_back_mv_table_base);
  1147. av_freep(&s->b_direct_mv_table_base);
  1148. s->p_mv_table = NULL;
  1149. s->b_forw_mv_table = NULL;
  1150. s->b_back_mv_table = NULL;
  1151. s->b_bidir_forw_mv_table = NULL;
  1152. s->b_bidir_back_mv_table = NULL;
  1153. s->b_direct_mv_table = NULL;
  1154. for (i = 0; i < 2; i++) {
  1155. for (j = 0; j < 2; j++) {
  1156. for (k = 0; k < 2; k++) {
  1157. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1158. s->b_field_mv_table[i][j][k] = NULL;
  1159. }
  1160. av_freep(&s->b_field_select_table[i][j]);
  1161. av_freep(&s->p_field_mv_table_base[i][j]);
  1162. s->p_field_mv_table[i][j] = NULL;
  1163. }
  1164. av_freep(&s->p_field_select_table[i]);
  1165. }
  1166. av_freep(&s->dc_val_base);
  1167. av_freep(&s->coded_block_base);
  1168. av_freep(&s->mbintra_table);
  1169. av_freep(&s->cbp_table);
  1170. av_freep(&s->pred_dir_table);
  1171. av_freep(&s->mbskip_table);
  1172. av_freep(&s->er.error_status_table);
  1173. av_freep(&s->er.er_temp_buffer);
  1174. av_freep(&s->mb_index2xy);
  1175. av_freep(&s->lambda_table);
  1176. av_freep(&s->cplx_tab);
  1177. av_freep(&s->bits_tab);
  1178. s->linesize = s->uvlinesize = 0;
  1179. return 0;
  1180. }
  1181. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1182. {
  1183. int i, err = 0;
  1184. if (s->slice_context_count > 1) {
  1185. for (i = 0; i < s->slice_context_count; i++) {
  1186. free_duplicate_context(s->thread_context[i]);
  1187. }
  1188. for (i = 1; i < s->slice_context_count; i++) {
  1189. av_freep(&s->thread_context[i]);
  1190. }
  1191. } else
  1192. free_duplicate_context(s);
  1193. if ((err = free_context_frame(s)) < 0)
  1194. return err;
  1195. if (s->picture)
  1196. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1197. s->picture[i].needs_realloc = 1;
  1198. }
  1199. s->last_picture_ptr =
  1200. s->next_picture_ptr =
  1201. s->current_picture_ptr = NULL;
  1202. // init
  1203. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1204. s->mb_height = (s->height + 31) / 32 * 2;
  1205. else
  1206. s->mb_height = (s->height + 15) / 16;
  1207. if ((s->width || s->height) &&
  1208. av_image_check_size(s->width, s->height, 0, s->avctx))
  1209. return AVERROR_INVALIDDATA;
  1210. if ((err = init_context_frame(s)))
  1211. goto fail;
  1212. s->thread_context[0] = s;
  1213. if (s->width && s->height) {
  1214. int nb_slices = s->slice_context_count;
  1215. if (nb_slices > 1) {
  1216. for (i = 1; i < nb_slices; i++) {
  1217. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1218. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1219. }
  1220. for (i = 0; i < nb_slices; i++) {
  1221. if (init_duplicate_context(s->thread_context[i]) < 0)
  1222. goto fail;
  1223. s->thread_context[i]->start_mb_y =
  1224. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1225. s->thread_context[i]->end_mb_y =
  1226. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1227. }
  1228. } else {
  1229. if (init_duplicate_context(s) < 0)
  1230. goto fail;
  1231. s->start_mb_y = 0;
  1232. s->end_mb_y = s->mb_height;
  1233. }
  1234. s->slice_context_count = nb_slices;
  1235. }
  1236. return 0;
  1237. fail:
  1238. ff_MPV_common_end(s);
  1239. return err;
  1240. }
  1241. /* init common structure for both encoder and decoder */
  1242. void ff_MPV_common_end(MpegEncContext *s)
  1243. {
  1244. int i;
  1245. if (s->slice_context_count > 1) {
  1246. for (i = 0; i < s->slice_context_count; i++) {
  1247. free_duplicate_context(s->thread_context[i]);
  1248. }
  1249. for (i = 1; i < s->slice_context_count; i++) {
  1250. av_freep(&s->thread_context[i]);
  1251. }
  1252. s->slice_context_count = 1;
  1253. } else free_duplicate_context(s);
  1254. av_freep(&s->parse_context.buffer);
  1255. s->parse_context.buffer_size = 0;
  1256. av_freep(&s->bitstream_buffer);
  1257. s->allocated_bitstream_buffer_size = 0;
  1258. if (s->picture) {
  1259. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1260. ff_free_picture_tables(&s->picture[i]);
  1261. ff_mpeg_unref_picture(s, &s->picture[i]);
  1262. av_frame_free(&s->picture[i].f);
  1263. }
  1264. }
  1265. av_freep(&s->picture);
  1266. ff_free_picture_tables(&s->last_picture);
  1267. ff_mpeg_unref_picture(s, &s->last_picture);
  1268. av_frame_free(&s->last_picture.f);
  1269. ff_free_picture_tables(&s->current_picture);
  1270. ff_mpeg_unref_picture(s, &s->current_picture);
  1271. av_frame_free(&s->current_picture.f);
  1272. ff_free_picture_tables(&s->next_picture);
  1273. ff_mpeg_unref_picture(s, &s->next_picture);
  1274. av_frame_free(&s->next_picture.f);
  1275. ff_free_picture_tables(&s->new_picture);
  1276. ff_mpeg_unref_picture(s, &s->new_picture);
  1277. av_frame_free(&s->new_picture.f);
  1278. free_context_frame(s);
  1279. s->context_initialized = 0;
  1280. s->last_picture_ptr =
  1281. s->next_picture_ptr =
  1282. s->current_picture_ptr = NULL;
  1283. s->linesize = s->uvlinesize = 0;
  1284. }
  1285. av_cold void ff_init_rl(RLTable *rl,
  1286. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1287. {
  1288. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1289. uint8_t index_run[MAX_RUN + 1];
  1290. int last, run, level, start, end, i;
  1291. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1292. if (static_store && rl->max_level[0])
  1293. return;
  1294. /* compute max_level[], max_run[] and index_run[] */
  1295. for (last = 0; last < 2; last++) {
  1296. if (last == 0) {
  1297. start = 0;
  1298. end = rl->last;
  1299. } else {
  1300. start = rl->last;
  1301. end = rl->n;
  1302. }
  1303. memset(max_level, 0, MAX_RUN + 1);
  1304. memset(max_run, 0, MAX_LEVEL + 1);
  1305. memset(index_run, rl->n, MAX_RUN + 1);
  1306. for (i = start; i < end; i++) {
  1307. run = rl->table_run[i];
  1308. level = rl->table_level[i];
  1309. if (index_run[run] == rl->n)
  1310. index_run[run] = i;
  1311. if (level > max_level[run])
  1312. max_level[run] = level;
  1313. if (run > max_run[level])
  1314. max_run[level] = run;
  1315. }
  1316. if (static_store)
  1317. rl->max_level[last] = static_store[last];
  1318. else
  1319. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1320. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1321. if (static_store)
  1322. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1323. else
  1324. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1325. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1326. if (static_store)
  1327. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1328. else
  1329. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1330. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1331. }
  1332. }
  1333. av_cold void ff_init_vlc_rl(RLTable *rl)
  1334. {
  1335. int i, q;
  1336. for (q = 0; q < 32; q++) {
  1337. int qmul = q * 2;
  1338. int qadd = (q - 1) | 1;
  1339. if (q == 0) {
  1340. qmul = 1;
  1341. qadd = 0;
  1342. }
  1343. for (i = 0; i < rl->vlc.table_size; i++) {
  1344. int code = rl->vlc.table[i][0];
  1345. int len = rl->vlc.table[i][1];
  1346. int level, run;
  1347. if (len == 0) { // illegal code
  1348. run = 66;
  1349. level = MAX_LEVEL;
  1350. } else if (len < 0) { // more bits needed
  1351. run = 0;
  1352. level = code;
  1353. } else {
  1354. if (code == rl->n) { // esc
  1355. run = 66;
  1356. level = 0;
  1357. } else {
  1358. run = rl->table_run[code] + 1;
  1359. level = rl->table_level[code] * qmul + qadd;
  1360. if (code >= rl->last) run += 192;
  1361. }
  1362. }
  1363. rl->rl_vlc[q][i].len = len;
  1364. rl->rl_vlc[q][i].level = level;
  1365. rl->rl_vlc[q][i].run = run;
  1366. }
  1367. }
  1368. }
  1369. static void release_unused_pictures(MpegEncContext *s)
  1370. {
  1371. int i;
  1372. /* release non reference frames */
  1373. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1374. if (!s->picture[i].reference)
  1375. ff_mpeg_unref_picture(s, &s->picture[i]);
  1376. }
  1377. }
  1378. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1379. {
  1380. if (pic->f->buf[0] == NULL)
  1381. return 1;
  1382. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1383. return 1;
  1384. return 0;
  1385. }
  1386. static int find_unused_picture(MpegEncContext *s, int shared)
  1387. {
  1388. int i;
  1389. if (shared) {
  1390. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1391. if (s->picture[i].f->buf[0] == NULL)
  1392. return i;
  1393. }
  1394. } else {
  1395. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1396. if (pic_is_unused(s, &s->picture[i]))
  1397. return i;
  1398. }
  1399. }
  1400. return AVERROR_INVALIDDATA;
  1401. }
  1402. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1403. {
  1404. int ret = find_unused_picture(s, shared);
  1405. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1406. if (s->picture[ret].needs_realloc) {
  1407. s->picture[ret].needs_realloc = 0;
  1408. ff_free_picture_tables(&s->picture[ret]);
  1409. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1410. }
  1411. }
  1412. return ret;
  1413. }
  1414. /**
  1415. * generic function called after decoding
  1416. * the header and before a frame is decoded.
  1417. */
  1418. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1419. {
  1420. int i, ret;
  1421. Picture *pic;
  1422. s->mb_skipped = 0;
  1423. /* mark & release old frames */
  1424. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1425. s->last_picture_ptr != s->next_picture_ptr &&
  1426. s->last_picture_ptr->f->buf[0]) {
  1427. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1428. }
  1429. /* release forgotten pictures */
  1430. /* if (mpeg124/h263) */
  1431. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1432. if (&s->picture[i] != s->last_picture_ptr &&
  1433. &s->picture[i] != s->next_picture_ptr &&
  1434. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1435. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1436. av_log(avctx, AV_LOG_ERROR,
  1437. "releasing zombie picture\n");
  1438. ff_mpeg_unref_picture(s, &s->picture[i]);
  1439. }
  1440. }
  1441. ff_mpeg_unref_picture(s, &s->current_picture);
  1442. release_unused_pictures(s);
  1443. if (s->current_picture_ptr &&
  1444. s->current_picture_ptr->f->buf[0] == NULL) {
  1445. // we already have a unused image
  1446. // (maybe it was set before reading the header)
  1447. pic = s->current_picture_ptr;
  1448. } else {
  1449. i = ff_find_unused_picture(s, 0);
  1450. if (i < 0) {
  1451. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1452. return i;
  1453. }
  1454. pic = &s->picture[i];
  1455. }
  1456. pic->reference = 0;
  1457. if (!s->droppable) {
  1458. if (s->pict_type != AV_PICTURE_TYPE_B)
  1459. pic->reference = 3;
  1460. }
  1461. pic->f->coded_picture_number = s->coded_picture_number++;
  1462. if (ff_alloc_picture(s, pic, 0) < 0)
  1463. return -1;
  1464. s->current_picture_ptr = pic;
  1465. // FIXME use only the vars from current_pic
  1466. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1467. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1468. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1469. if (s->picture_structure != PICT_FRAME)
  1470. s->current_picture_ptr->f->top_field_first =
  1471. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1472. }
  1473. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1474. !s->progressive_sequence;
  1475. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1476. s->current_picture_ptr->f->pict_type = s->pict_type;
  1477. // if (s->flags && CODEC_FLAG_QSCALE)
  1478. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1479. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1480. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1481. s->current_picture_ptr)) < 0)
  1482. return ret;
  1483. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1484. s->last_picture_ptr = s->next_picture_ptr;
  1485. if (!s->droppable)
  1486. s->next_picture_ptr = s->current_picture_ptr;
  1487. }
  1488. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1489. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1490. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1491. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1492. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1493. s->pict_type, s->droppable);
  1494. if ((s->last_picture_ptr == NULL ||
  1495. s->last_picture_ptr->f->buf[0] == NULL) &&
  1496. (s->pict_type != AV_PICTURE_TYPE_I ||
  1497. s->picture_structure != PICT_FRAME)) {
  1498. int h_chroma_shift, v_chroma_shift;
  1499. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1500. &h_chroma_shift, &v_chroma_shift);
  1501. if (s->pict_type != AV_PICTURE_TYPE_I)
  1502. av_log(avctx, AV_LOG_ERROR,
  1503. "warning: first frame is no keyframe\n");
  1504. else if (s->picture_structure != PICT_FRAME)
  1505. av_log(avctx, AV_LOG_INFO,
  1506. "allocate dummy last picture for field based first keyframe\n");
  1507. /* Allocate a dummy frame */
  1508. i = ff_find_unused_picture(s, 0);
  1509. if (i < 0) {
  1510. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1511. return i;
  1512. }
  1513. s->last_picture_ptr = &s->picture[i];
  1514. s->last_picture_ptr->reference = 3;
  1515. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1516. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1517. s->last_picture_ptr = NULL;
  1518. return -1;
  1519. }
  1520. memset(s->last_picture_ptr->f->data[0], 0,
  1521. avctx->height * s->last_picture_ptr->f->linesize[0]);
  1522. memset(s->last_picture_ptr->f->data[1], 0x80,
  1523. (avctx->height >> v_chroma_shift) *
  1524. s->last_picture_ptr->f->linesize[1]);
  1525. memset(s->last_picture_ptr->f->data[2], 0x80,
  1526. (avctx->height >> v_chroma_shift) *
  1527. s->last_picture_ptr->f->linesize[2]);
  1528. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1529. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1530. }
  1531. if ((s->next_picture_ptr == NULL ||
  1532. s->next_picture_ptr->f->buf[0] == NULL) &&
  1533. s->pict_type == AV_PICTURE_TYPE_B) {
  1534. /* Allocate a dummy frame */
  1535. i = ff_find_unused_picture(s, 0);
  1536. if (i < 0) {
  1537. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1538. return i;
  1539. }
  1540. s->next_picture_ptr = &s->picture[i];
  1541. s->next_picture_ptr->reference = 3;
  1542. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1543. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1544. s->next_picture_ptr = NULL;
  1545. return -1;
  1546. }
  1547. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1548. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1549. }
  1550. if (s->last_picture_ptr) {
  1551. ff_mpeg_unref_picture(s, &s->last_picture);
  1552. if (s->last_picture_ptr->f->buf[0] &&
  1553. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1554. s->last_picture_ptr)) < 0)
  1555. return ret;
  1556. }
  1557. if (s->next_picture_ptr) {
  1558. ff_mpeg_unref_picture(s, &s->next_picture);
  1559. if (s->next_picture_ptr->f->buf[0] &&
  1560. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1561. s->next_picture_ptr)) < 0)
  1562. return ret;
  1563. }
  1564. if (s->pict_type != AV_PICTURE_TYPE_I &&
  1565. !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
  1566. av_log(s, AV_LOG_ERROR,
  1567. "Non-reference picture received and no reference available\n");
  1568. return AVERROR_INVALIDDATA;
  1569. }
  1570. if (s->picture_structure!= PICT_FRAME) {
  1571. int i;
  1572. for (i = 0; i < 4; i++) {
  1573. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1574. s->current_picture.f->data[i] +=
  1575. s->current_picture.f->linesize[i];
  1576. }
  1577. s->current_picture.f->linesize[i] *= 2;
  1578. s->last_picture.f->linesize[i] *= 2;
  1579. s->next_picture.f->linesize[i] *= 2;
  1580. }
  1581. }
  1582. s->err_recognition = avctx->err_recognition;
  1583. /* set dequantizer, we can't do it during init as
  1584. * it might change for mpeg4 and we can't do it in the header
  1585. * decode as init is not called for mpeg4 there yet */
  1586. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1587. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1588. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1589. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1590. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1591. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1592. } else {
  1593. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1594. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1595. }
  1596. #if FF_API_XVMC
  1597. FF_DISABLE_DEPRECATION_WARNINGS
  1598. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1599. return ff_xvmc_field_start(s, avctx);
  1600. FF_ENABLE_DEPRECATION_WARNINGS
  1601. #endif /* FF_API_XVMC */
  1602. return 0;
  1603. }
  1604. /* called after a frame has been decoded. */
  1605. void ff_MPV_frame_end(MpegEncContext *s)
  1606. {
  1607. #if FF_API_XVMC
  1608. FF_DISABLE_DEPRECATION_WARNINGS
  1609. /* redraw edges for the frame if decoding didn't complete */
  1610. // just to make sure that all data is rendered.
  1611. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1612. ff_xvmc_field_end(s);
  1613. } else
  1614. FF_ENABLE_DEPRECATION_WARNINGS
  1615. #endif /* FF_API_XVMC */
  1616. emms_c();
  1617. if (s->current_picture.reference)
  1618. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1619. }
  1620. /**
  1621. * Print debugging info for the given picture.
  1622. */
  1623. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1624. {
  1625. AVFrame *pict;
  1626. if (s->avctx->hwaccel || !p || !p->mb_type)
  1627. return;
  1628. pict = p->f;
  1629. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1630. int x,y;
  1631. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1632. switch (pict->pict_type) {
  1633. case AV_PICTURE_TYPE_I:
  1634. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1635. break;
  1636. case AV_PICTURE_TYPE_P:
  1637. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1638. break;
  1639. case AV_PICTURE_TYPE_B:
  1640. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1641. break;
  1642. case AV_PICTURE_TYPE_S:
  1643. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1644. break;
  1645. case AV_PICTURE_TYPE_SI:
  1646. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1647. break;
  1648. case AV_PICTURE_TYPE_SP:
  1649. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1650. break;
  1651. }
  1652. for (y = 0; y < s->mb_height; y++) {
  1653. for (x = 0; x < s->mb_width; x++) {
  1654. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1655. int count = s->mbskip_table[x + y * s->mb_stride];
  1656. if (count > 9)
  1657. count = 9;
  1658. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1659. }
  1660. if (s->avctx->debug & FF_DEBUG_QP) {
  1661. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1662. p->qscale_table[x + y * s->mb_stride]);
  1663. }
  1664. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1665. int mb_type = p->mb_type[x + y * s->mb_stride];
  1666. // Type & MV direction
  1667. if (IS_PCM(mb_type))
  1668. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1669. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1670. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1671. else if (IS_INTRA4x4(mb_type))
  1672. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1673. else if (IS_INTRA16x16(mb_type))
  1674. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1675. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1676. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1677. else if (IS_DIRECT(mb_type))
  1678. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1679. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1680. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1681. else if (IS_GMC(mb_type))
  1682. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1683. else if (IS_SKIP(mb_type))
  1684. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1685. else if (!USES_LIST(mb_type, 1))
  1686. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1687. else if (!USES_LIST(mb_type, 0))
  1688. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1689. else {
  1690. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1691. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1692. }
  1693. // segmentation
  1694. if (IS_8X8(mb_type))
  1695. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1696. else if (IS_16X8(mb_type))
  1697. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1698. else if (IS_8X16(mb_type))
  1699. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1700. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1701. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1702. else
  1703. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1704. if (IS_INTERLACED(mb_type))
  1705. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1706. else
  1707. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1708. }
  1709. }
  1710. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1711. }
  1712. }
  1713. }
  1714. /**
  1715. * find the lowest MB row referenced in the MVs
  1716. */
  1717. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1718. {
  1719. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1720. int my, off, i, mvs;
  1721. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1722. goto unhandled;
  1723. switch (s->mv_type) {
  1724. case MV_TYPE_16X16:
  1725. mvs = 1;
  1726. break;
  1727. case MV_TYPE_16X8:
  1728. mvs = 2;
  1729. break;
  1730. case MV_TYPE_8X8:
  1731. mvs = 4;
  1732. break;
  1733. default:
  1734. goto unhandled;
  1735. }
  1736. for (i = 0; i < mvs; i++) {
  1737. my = s->mv[dir][i][1]<<qpel_shift;
  1738. my_max = FFMAX(my_max, my);
  1739. my_min = FFMIN(my_min, my);
  1740. }
  1741. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1742. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1743. unhandled:
  1744. return s->mb_height-1;
  1745. }
  1746. /* put block[] to dest[] */
  1747. static inline void put_dct(MpegEncContext *s,
  1748. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1749. {
  1750. s->dct_unquantize_intra(s, block, i, qscale);
  1751. s->dsp.idct_put (dest, line_size, block);
  1752. }
  1753. /* add block[] to dest[] */
  1754. static inline void add_dct(MpegEncContext *s,
  1755. int16_t *block, int i, uint8_t *dest, int line_size)
  1756. {
  1757. if (s->block_last_index[i] >= 0) {
  1758. s->dsp.idct_add (dest, line_size, block);
  1759. }
  1760. }
  1761. static inline void add_dequant_dct(MpegEncContext *s,
  1762. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1763. {
  1764. if (s->block_last_index[i] >= 0) {
  1765. s->dct_unquantize_inter(s, block, i, qscale);
  1766. s->dsp.idct_add (dest, line_size, block);
  1767. }
  1768. }
  1769. /**
  1770. * Clean dc, ac, coded_block for the current non-intra MB.
  1771. */
  1772. void ff_clean_intra_table_entries(MpegEncContext *s)
  1773. {
  1774. int wrap = s->b8_stride;
  1775. int xy = s->block_index[0];
  1776. s->dc_val[0][xy ] =
  1777. s->dc_val[0][xy + 1 ] =
  1778. s->dc_val[0][xy + wrap] =
  1779. s->dc_val[0][xy + 1 + wrap] = 1024;
  1780. /* ac pred */
  1781. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1782. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1783. if (s->msmpeg4_version>=3) {
  1784. s->coded_block[xy ] =
  1785. s->coded_block[xy + 1 ] =
  1786. s->coded_block[xy + wrap] =
  1787. s->coded_block[xy + 1 + wrap] = 0;
  1788. }
  1789. /* chroma */
  1790. wrap = s->mb_stride;
  1791. xy = s->mb_x + s->mb_y * wrap;
  1792. s->dc_val[1][xy] =
  1793. s->dc_val[2][xy] = 1024;
  1794. /* ac pred */
  1795. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1796. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1797. s->mbintra_table[xy]= 0;
  1798. }
  1799. /* generic function called after a macroblock has been parsed by the
  1800. decoder or after it has been encoded by the encoder.
  1801. Important variables used:
  1802. s->mb_intra : true if intra macroblock
  1803. s->mv_dir : motion vector direction
  1804. s->mv_type : motion vector type
  1805. s->mv : motion vector
  1806. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1807. */
  1808. static av_always_inline
  1809. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1810. int is_mpeg12)
  1811. {
  1812. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1813. #if FF_API_XVMC
  1814. FF_DISABLE_DEPRECATION_WARNINGS
  1815. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1816. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1817. return;
  1818. }
  1819. FF_ENABLE_DEPRECATION_WARNINGS
  1820. #endif /* FF_API_XVMC */
  1821. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1822. /* print DCT coefficients */
  1823. int i,j;
  1824. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1825. for(i=0; i<6; i++){
  1826. for(j=0; j<64; j++){
  1827. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  1828. }
  1829. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1830. }
  1831. }
  1832. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1833. /* update DC predictors for P macroblocks */
  1834. if (!s->mb_intra) {
  1835. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1836. if(s->mbintra_table[mb_xy])
  1837. ff_clean_intra_table_entries(s);
  1838. } else {
  1839. s->last_dc[0] =
  1840. s->last_dc[1] =
  1841. s->last_dc[2] = 128 << s->intra_dc_precision;
  1842. }
  1843. }
  1844. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1845. s->mbintra_table[mb_xy]=1;
  1846. 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
  1847. uint8_t *dest_y, *dest_cb, *dest_cr;
  1848. int dct_linesize, dct_offset;
  1849. op_pixels_func (*op_pix)[4];
  1850. qpel_mc_func (*op_qpix)[16];
  1851. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1852. const int uvlinesize = s->current_picture.f->linesize[1];
  1853. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1854. const int block_size = 8;
  1855. /* avoid copy if macroblock skipped in last frame too */
  1856. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1857. if(!s->encoding){
  1858. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1859. if (s->mb_skipped) {
  1860. s->mb_skipped= 0;
  1861. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1862. *mbskip_ptr = 1;
  1863. } else if(!s->current_picture.reference) {
  1864. *mbskip_ptr = 1;
  1865. } else{
  1866. *mbskip_ptr = 0; /* not skipped */
  1867. }
  1868. }
  1869. dct_linesize = linesize << s->interlaced_dct;
  1870. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1871. if(readable){
  1872. dest_y= s->dest[0];
  1873. dest_cb= s->dest[1];
  1874. dest_cr= s->dest[2];
  1875. }else{
  1876. dest_y = s->b_scratchpad;
  1877. dest_cb= s->b_scratchpad+16*linesize;
  1878. dest_cr= s->b_scratchpad+32*linesize;
  1879. }
  1880. if (!s->mb_intra) {
  1881. /* motion handling */
  1882. /* decoding or more than one mb_type (MC was already done otherwise) */
  1883. if(!s->encoding){
  1884. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1885. if (s->mv_dir & MV_DIR_FORWARD) {
  1886. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1887. ff_MPV_lowest_referenced_row(s, 0),
  1888. 0);
  1889. }
  1890. if (s->mv_dir & MV_DIR_BACKWARD) {
  1891. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1892. ff_MPV_lowest_referenced_row(s, 1),
  1893. 0);
  1894. }
  1895. }
  1896. op_qpix= s->me.qpel_put;
  1897. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1898. op_pix = s->hdsp.put_pixels_tab;
  1899. }else{
  1900. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1901. }
  1902. if (s->mv_dir & MV_DIR_FORWARD) {
  1903. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  1904. op_pix = s->hdsp.avg_pixels_tab;
  1905. op_qpix= s->me.qpel_avg;
  1906. }
  1907. if (s->mv_dir & MV_DIR_BACKWARD) {
  1908. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  1909. }
  1910. }
  1911. /* skip dequant / idct if we are really late ;) */
  1912. if(s->avctx->skip_idct){
  1913. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1914. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1915. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1916. goto skip_idct;
  1917. }
  1918. /* add dct residue */
  1919. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1920. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1921. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1922. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1923. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1924. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1925. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1926. if (s->chroma_y_shift){
  1927. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1928. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1929. }else{
  1930. dct_linesize >>= 1;
  1931. dct_offset >>=1;
  1932. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1933. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1934. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1935. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1936. }
  1937. }
  1938. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1939. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1940. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1941. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1942. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1943. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1944. if(s->chroma_y_shift){//Chroma420
  1945. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1946. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1947. }else{
  1948. //chroma422
  1949. dct_linesize = uvlinesize << s->interlaced_dct;
  1950. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1951. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1952. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1953. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1954. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1955. if(!s->chroma_x_shift){//Chroma444
  1956. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1957. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1958. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1959. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1960. }
  1961. }
  1962. }//fi gray
  1963. }
  1964. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1965. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1966. }
  1967. } else {
  1968. /* dct only in intra block */
  1969. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1970. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1971. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1972. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1973. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1974. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1975. if(s->chroma_y_shift){
  1976. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1977. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1978. }else{
  1979. dct_offset >>=1;
  1980. dct_linesize >>=1;
  1981. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1982. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1983. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1984. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1985. }
  1986. }
  1987. }else{
  1988. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1989. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1990. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1991. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1992. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1993. if(s->chroma_y_shift){
  1994. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1995. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1996. }else{
  1997. dct_linesize = uvlinesize << s->interlaced_dct;
  1998. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1999. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2000. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2001. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2002. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2003. if(!s->chroma_x_shift){//Chroma444
  2004. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  2005. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  2006. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  2007. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  2008. }
  2009. }
  2010. }//gray
  2011. }
  2012. }
  2013. skip_idct:
  2014. if(!readable){
  2015. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2016. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2017. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2018. }
  2019. }
  2020. }
  2021. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2022. #if !CONFIG_SMALL
  2023. if(s->out_format == FMT_MPEG1) {
  2024. MPV_decode_mb_internal(s, block, 1);
  2025. } else
  2026. #endif
  2027. MPV_decode_mb_internal(s, block, 0);
  2028. }
  2029. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2030. {
  2031. ff_draw_horiz_band(s->avctx, s->current_picture.f,
  2032. s->last_picture.f, y, h, s->picture_structure,
  2033. s->first_field, s->low_delay);
  2034. }
  2035. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2036. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2037. const int uvlinesize = s->current_picture.f->linesize[1];
  2038. const int mb_size= 4;
  2039. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2040. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2041. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2042. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2043. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2044. 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;
  2045. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2046. s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
  2047. s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2048. s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2049. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2050. {
  2051. if(s->picture_structure==PICT_FRAME){
  2052. s->dest[0] += s->mb_y * linesize << mb_size;
  2053. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2054. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2055. }else{
  2056. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2057. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2058. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2059. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2060. }
  2061. }
  2062. }
  2063. /**
  2064. * Permute an 8x8 block.
  2065. * @param block the block which will be permuted according to the given permutation vector
  2066. * @param permutation the permutation vector
  2067. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2068. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2069. * (inverse) permutated to scantable order!
  2070. */
  2071. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2072. {
  2073. int i;
  2074. int16_t temp[64];
  2075. if(last<=0) return;
  2076. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2077. for(i=0; i<=last; i++){
  2078. const int j= scantable[i];
  2079. temp[j]= block[j];
  2080. block[j]=0;
  2081. }
  2082. for(i=0; i<=last; i++){
  2083. const int j= scantable[i];
  2084. const int perm_j= permutation[j];
  2085. block[perm_j]= temp[j];
  2086. }
  2087. }
  2088. void ff_mpeg_flush(AVCodecContext *avctx){
  2089. int i;
  2090. MpegEncContext *s = avctx->priv_data;
  2091. if(s==NULL || s->picture==NULL)
  2092. return;
  2093. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2094. ff_mpeg_unref_picture(s, &s->picture[i]);
  2095. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2096. ff_mpeg_unref_picture(s, &s->current_picture);
  2097. ff_mpeg_unref_picture(s, &s->last_picture);
  2098. ff_mpeg_unref_picture(s, &s->next_picture);
  2099. s->mb_x= s->mb_y= 0;
  2100. s->parse_context.state= -1;
  2101. s->parse_context.frame_start_found= 0;
  2102. s->parse_context.overread= 0;
  2103. s->parse_context.overread_index= 0;
  2104. s->parse_context.index= 0;
  2105. s->parse_context.last_index= 0;
  2106. s->bitstream_buffer_size=0;
  2107. s->pp_time=0;
  2108. }
  2109. /**
  2110. * set qscale and update qscale dependent variables.
  2111. */
  2112. void ff_set_qscale(MpegEncContext * s, int qscale)
  2113. {
  2114. if (qscale < 1)
  2115. qscale = 1;
  2116. else if (qscale > 31)
  2117. qscale = 31;
  2118. s->qscale = qscale;
  2119. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2120. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2121. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2122. }
  2123. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2124. {
  2125. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2126. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2127. }
  2128. #if CONFIG_ERROR_RESILIENCE
  2129. void ff_mpeg_set_erpic(ERPicture *dst, Picture *src)
  2130. {
  2131. int i;
  2132. if (!src)
  2133. return;
  2134. dst->f = src->f;
  2135. dst->tf = &src->tf;
  2136. for (i = 0; i < 2; i++) {
  2137. dst->motion_val[i] = src->motion_val[i];
  2138. dst->ref_index[i] = src->ref_index[i];
  2139. }
  2140. dst->mb_type = src->mb_type;
  2141. dst->field_picture = src->field_picture;
  2142. }
  2143. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2144. {
  2145. ERContext *er = &s->er;
  2146. ff_mpeg_set_erpic(&er->cur_pic, s->current_picture_ptr);
  2147. ff_mpeg_set_erpic(&er->next_pic, s->next_picture_ptr);
  2148. ff_mpeg_set_erpic(&er->last_pic, s->last_picture_ptr);
  2149. er->pp_time = s->pp_time;
  2150. er->pb_time = s->pb_time;
  2151. er->quarter_sample = s->quarter_sample;
  2152. er->partitioned_frame = s->partitioned_frame;
  2153. ff_er_frame_start(er);
  2154. }
  2155. #endif /* CONFIG_ERROR_RESILIENCE */