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.

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