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.

2481 lines
84KB

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