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.

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