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.

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