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.

2504 lines
85KB

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