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.

3280 lines
118KB

  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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "h264chroma.h"
  36. #include "idctdsp.h"
  37. #include "internal.h"
  38. #include "mathops.h"
  39. #include "mpegutils.h"
  40. #include "mpegvideo.h"
  41. #include "mjpegenc.h"
  42. #include "msmpeg4.h"
  43. #include "qpeldsp.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. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  128. /* XXX: only mpeg1 */
  129. quant_matrix = s->intra_matrix;
  130. for(i=1;i<=nCoeffs;i++) {
  131. int j= s->intra_scantable.permutated[i];
  132. level = block[j];
  133. if (level) {
  134. if (level < 0) {
  135. level = -level;
  136. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  137. level = (level - 1) | 1;
  138. level = -level;
  139. } else {
  140. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  141. level = (level - 1) | 1;
  142. }
  143. block[j] = level;
  144. }
  145. }
  146. }
  147. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  148. int16_t *block, int n, int qscale)
  149. {
  150. int i, level, nCoeffs;
  151. const uint16_t *quant_matrix;
  152. nCoeffs= s->block_last_index[n];
  153. quant_matrix = s->inter_matrix;
  154. for(i=0; i<=nCoeffs; i++) {
  155. int j= s->intra_scantable.permutated[i];
  156. level = block[j];
  157. if (level) {
  158. if (level < 0) {
  159. level = -level;
  160. level = (((level << 1) + 1) * qscale *
  161. ((int) (quant_matrix[j]))) >> 4;
  162. level = (level - 1) | 1;
  163. level = -level;
  164. } else {
  165. level = (((level << 1) + 1) * qscale *
  166. ((int) (quant_matrix[j]))) >> 4;
  167. level = (level - 1) | 1;
  168. }
  169. block[j] = level;
  170. }
  171. }
  172. }
  173. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  174. int16_t *block, int n, int qscale)
  175. {
  176. int i, level, nCoeffs;
  177. const uint16_t *quant_matrix;
  178. if(s->alternate_scan) nCoeffs= 63;
  179. else nCoeffs= s->block_last_index[n];
  180. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  181. quant_matrix = s->intra_matrix;
  182. for(i=1;i<=nCoeffs;i++) {
  183. int j= s->intra_scantable.permutated[i];
  184. level = block[j];
  185. if (level) {
  186. if (level < 0) {
  187. level = -level;
  188. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  189. level = -level;
  190. } else {
  191. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  192. }
  193. block[j] = level;
  194. }
  195. }
  196. }
  197. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  198. int16_t *block, int n, int qscale)
  199. {
  200. int i, level, nCoeffs;
  201. const uint16_t *quant_matrix;
  202. int sum=-1;
  203. if(s->alternate_scan) nCoeffs= 63;
  204. else nCoeffs= s->block_last_index[n];
  205. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  206. sum += block[0];
  207. quant_matrix = s->intra_matrix;
  208. for(i=1;i<=nCoeffs;i++) {
  209. int j= s->intra_scantable.permutated[i];
  210. level = block[j];
  211. if (level) {
  212. if (level < 0) {
  213. level = -level;
  214. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  215. level = -level;
  216. } else {
  217. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  218. }
  219. block[j] = level;
  220. sum+=level;
  221. }
  222. }
  223. block[63]^=sum&1;
  224. }
  225. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  226. int16_t *block, int n, int qscale)
  227. {
  228. int i, level, nCoeffs;
  229. const uint16_t *quant_matrix;
  230. int sum=-1;
  231. if(s->alternate_scan) nCoeffs= 63;
  232. else nCoeffs= s->block_last_index[n];
  233. quant_matrix = s->inter_matrix;
  234. for(i=0; i<=nCoeffs; i++) {
  235. int j= s->intra_scantable.permutated[i];
  236. level = block[j];
  237. if (level) {
  238. if (level < 0) {
  239. level = -level;
  240. level = (((level << 1) + 1) * qscale *
  241. ((int) (quant_matrix[j]))) >> 4;
  242. level = -level;
  243. } else {
  244. level = (((level << 1) + 1) * qscale *
  245. ((int) (quant_matrix[j]))) >> 4;
  246. }
  247. block[j] = level;
  248. sum+=level;
  249. }
  250. }
  251. block[63]^=sum&1;
  252. }
  253. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  254. int16_t *block, int n, int qscale)
  255. {
  256. int i, level, qmul, qadd;
  257. int nCoeffs;
  258. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  259. qmul = qscale << 1;
  260. if (!s->h263_aic) {
  261. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  262. qadd = (qscale - 1) | 1;
  263. }else{
  264. qadd = 0;
  265. }
  266. if(s->ac_pred)
  267. nCoeffs=63;
  268. else
  269. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  270. for(i=1; i<=nCoeffs; i++) {
  271. level = block[i];
  272. if (level) {
  273. if (level < 0) {
  274. level = level * qmul - qadd;
  275. } else {
  276. level = level * qmul + qadd;
  277. }
  278. block[i] = level;
  279. }
  280. }
  281. }
  282. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  283. int16_t *block, int n, int qscale)
  284. {
  285. int i, level, qmul, qadd;
  286. int nCoeffs;
  287. av_assert2(s->block_last_index[n]>=0);
  288. qadd = (qscale - 1) | 1;
  289. qmul = qscale << 1;
  290. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  291. for(i=0; i<=nCoeffs; i++) {
  292. level = block[i];
  293. if (level) {
  294. if (level < 0) {
  295. level = level * qmul - qadd;
  296. } else {
  297. level = level * qmul + qadd;
  298. }
  299. block[i] = level;
  300. }
  301. }
  302. }
  303. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  304. int (*mv)[2][4][2],
  305. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  306. {
  307. MpegEncContext *s = opaque;
  308. s->mv_dir = mv_dir;
  309. s->mv_type = mv_type;
  310. s->mb_intra = mb_intra;
  311. s->mb_skipped = mb_skipped;
  312. s->mb_x = mb_x;
  313. s->mb_y = mb_y;
  314. memcpy(s->mv, mv, sizeof(*mv));
  315. ff_init_block_index(s);
  316. ff_update_block_index(s);
  317. s->bdsp.clear_blocks(s->block[0]);
  318. s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  319. 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);
  320. 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);
  321. if (ref)
  322. av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
  323. ff_MPV_decode_mb(s, s->block);
  324. }
  325. static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
  326. {
  327. while(h--)
  328. memset(dst + h*linesize, 128, 16);
  329. }
  330. static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
  331. {
  332. while(h--)
  333. memset(dst + h*linesize, 128, 8);
  334. }
  335. /* init common dct for both encoder and decoder */
  336. av_cold int ff_dct_common_init(MpegEncContext *s)
  337. {
  338. ff_blockdsp_init(&s->bdsp, s->avctx);
  339. ff_dsputil_init(&s->dsp, s->avctx);
  340. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  341. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  342. ff_idctdsp_init(&s->idsp, s->avctx);
  343. ff_mpegvideodsp_init(&s->mdsp);
  344. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  345. if (s->avctx->debug & FF_DEBUG_NOMC) {
  346. int i;
  347. for (i=0; i<4; i++) {
  348. s->hdsp.avg_pixels_tab[0][i] = gray16;
  349. s->hdsp.put_pixels_tab[0][i] = gray16;
  350. s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
  351. s->hdsp.avg_pixels_tab[1][i] = gray8;
  352. s->hdsp.put_pixels_tab[1][i] = gray8;
  353. s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
  354. }
  355. }
  356. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  357. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  358. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  359. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  360. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  361. if (s->flags & CODEC_FLAG_BITEXACT)
  362. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  363. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  364. if (ARCH_ALPHA)
  365. ff_MPV_common_init_axp(s);
  366. if (ARCH_ARM)
  367. ff_MPV_common_init_arm(s);
  368. if (ARCH_PPC)
  369. ff_MPV_common_init_ppc(s);
  370. if (ARCH_X86)
  371. ff_MPV_common_init_x86(s);
  372. /* load & permutate scantables
  373. * note: only wmv uses different ones
  374. */
  375. if (s->alternate_scan) {
  376. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  377. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  378. } else {
  379. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  380. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  381. }
  382. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  383. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  384. return 0;
  385. }
  386. static int frame_size_alloc(MpegEncContext *s, int linesize)
  387. {
  388. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  389. if (s->avctx->hwaccel || s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  390. return 0;
  391. if (linesize < 24) {
  392. av_log(s->avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
  393. return AVERROR_PATCHWELCOME;
  394. }
  395. // edge emu needs blocksize + filter length - 1
  396. // (= 17x17 for halfpel / 21x21 for h264)
  397. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  398. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  399. // linesize * interlaced * MBsize
  400. // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
  401. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 68,
  402. fail);
  403. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
  404. fail)
  405. s->me.temp = s->me.scratchpad;
  406. s->rd_scratchpad = s->me.scratchpad;
  407. s->b_scratchpad = s->me.scratchpad;
  408. s->obmc_scratchpad = s->me.scratchpad + 16;
  409. return 0;
  410. fail:
  411. av_freep(&s->edge_emu_buffer);
  412. return AVERROR(ENOMEM);
  413. }
  414. /**
  415. * Allocate a frame buffer
  416. */
  417. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  418. {
  419. int edges_needed = av_codec_is_encoder(s->avctx->codec);
  420. int r, ret;
  421. pic->tf.f = pic->f;
  422. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  423. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  424. s->codec_id != AV_CODEC_ID_MSS2) {
  425. if (edges_needed) {
  426. pic->f->width = s->avctx->width + 2 * EDGE_WIDTH;
  427. pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
  428. }
  429. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  430. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  431. } else {
  432. pic->f->width = s->avctx->width;
  433. pic->f->height = s->avctx->height;
  434. pic->f->format = s->avctx->pix_fmt;
  435. r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
  436. }
  437. if (r < 0 || !pic->f->buf[0]) {
  438. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  439. r, pic->f->data[0]);
  440. return -1;
  441. }
  442. if (edges_needed) {
  443. int i;
  444. for (i = 0; pic->f->data[i]; i++) {
  445. int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
  446. pic->f->linesize[i] +
  447. (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
  448. pic->f->data[i] += offset;
  449. }
  450. pic->f->width = s->avctx->width;
  451. pic->f->height = s->avctx->height;
  452. }
  453. if (s->avctx->hwaccel) {
  454. assert(!pic->hwaccel_picture_private);
  455. if (s->avctx->hwaccel->frame_priv_data_size) {
  456. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->frame_priv_data_size);
  457. if (!pic->hwaccel_priv_buf) {
  458. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  459. return -1;
  460. }
  461. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  462. }
  463. }
  464. if (s->linesize && (s->linesize != pic->f->linesize[0] ||
  465. s->uvlinesize != pic->f->linesize[1])) {
  466. av_log(s->avctx, AV_LOG_ERROR,
  467. "get_buffer() failed (stride changed)\n");
  468. ff_mpeg_unref_picture(s, pic);
  469. return -1;
  470. }
  471. if (pic->f->linesize[1] != pic->f->linesize[2]) {
  472. av_log(s->avctx, AV_LOG_ERROR,
  473. "get_buffer() failed (uv stride mismatch)\n");
  474. ff_mpeg_unref_picture(s, pic);
  475. return -1;
  476. }
  477. if (!s->edge_emu_buffer &&
  478. (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
  479. av_log(s->avctx, AV_LOG_ERROR,
  480. "get_buffer() failed to allocate context scratch buffers.\n");
  481. ff_mpeg_unref_picture(s, pic);
  482. return ret;
  483. }
  484. return 0;
  485. }
  486. void ff_free_picture_tables(Picture *pic)
  487. {
  488. int i;
  489. pic->alloc_mb_width =
  490. pic->alloc_mb_height = 0;
  491. av_buffer_unref(&pic->mb_var_buf);
  492. av_buffer_unref(&pic->mc_mb_var_buf);
  493. av_buffer_unref(&pic->mb_mean_buf);
  494. av_buffer_unref(&pic->mbskip_table_buf);
  495. av_buffer_unref(&pic->qscale_table_buf);
  496. av_buffer_unref(&pic->mb_type_buf);
  497. for (i = 0; i < 2; i++) {
  498. av_buffer_unref(&pic->motion_val_buf[i]);
  499. av_buffer_unref(&pic->ref_index_buf[i]);
  500. }
  501. }
  502. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  503. {
  504. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  505. const int mb_array_size = s->mb_stride * s->mb_height;
  506. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  507. int i;
  508. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  509. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  510. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  511. sizeof(uint32_t));
  512. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  513. return AVERROR(ENOMEM);
  514. if (s->encoding) {
  515. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  516. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  517. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  518. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  519. return AVERROR(ENOMEM);
  520. }
  521. if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
  522. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  523. int ref_index_size = 4 * mb_array_size;
  524. for (i = 0; mv_size && i < 2; i++) {
  525. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  526. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  527. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  528. return AVERROR(ENOMEM);
  529. }
  530. }
  531. pic->alloc_mb_width = s->mb_width;
  532. pic->alloc_mb_height = s->mb_height;
  533. return 0;
  534. }
  535. static int make_tables_writable(Picture *pic)
  536. {
  537. int ret, i;
  538. #define MAKE_WRITABLE(table) \
  539. do {\
  540. if (pic->table &&\
  541. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  542. return ret;\
  543. } while (0)
  544. MAKE_WRITABLE(mb_var_buf);
  545. MAKE_WRITABLE(mc_mb_var_buf);
  546. MAKE_WRITABLE(mb_mean_buf);
  547. MAKE_WRITABLE(mbskip_table_buf);
  548. MAKE_WRITABLE(qscale_table_buf);
  549. MAKE_WRITABLE(mb_type_buf);
  550. for (i = 0; i < 2; i++) {
  551. MAKE_WRITABLE(motion_val_buf[i]);
  552. MAKE_WRITABLE(ref_index_buf[i]);
  553. }
  554. return 0;
  555. }
  556. /**
  557. * Allocate a Picture.
  558. * The pixels are allocated/set by calling get_buffer() if shared = 0
  559. */
  560. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  561. {
  562. int i, ret;
  563. if (pic->qscale_table_buf)
  564. if ( pic->alloc_mb_width != s->mb_width
  565. || pic->alloc_mb_height != s->mb_height)
  566. ff_free_picture_tables(pic);
  567. if (shared) {
  568. av_assert0(pic->f->data[0]);
  569. pic->shared = 1;
  570. } else {
  571. av_assert0(!pic->f->buf[0]);
  572. if (alloc_frame_buffer(s, pic) < 0)
  573. return -1;
  574. s->linesize = pic->f->linesize[0];
  575. s->uvlinesize = pic->f->linesize[1];
  576. }
  577. if (!pic->qscale_table_buf)
  578. ret = alloc_picture_tables(s, pic);
  579. else
  580. ret = make_tables_writable(pic);
  581. if (ret < 0)
  582. goto fail;
  583. if (s->encoding) {
  584. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  585. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  586. pic->mb_mean = pic->mb_mean_buf->data;
  587. }
  588. pic->mbskip_table = pic->mbskip_table_buf->data;
  589. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  590. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  591. if (pic->motion_val_buf[0]) {
  592. for (i = 0; i < 2; i++) {
  593. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  594. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  595. }
  596. }
  597. return 0;
  598. fail:
  599. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  600. ff_mpeg_unref_picture(s, pic);
  601. ff_free_picture_tables(pic);
  602. return AVERROR(ENOMEM);
  603. }
  604. /**
  605. * Deallocate a picture.
  606. */
  607. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  608. {
  609. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  610. pic->tf.f = pic->f;
  611. /* WM Image / Screen codecs allocate internal buffers with different
  612. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  613. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  614. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  615. s->codec_id != AV_CODEC_ID_MSS2)
  616. ff_thread_release_buffer(s->avctx, &pic->tf);
  617. else if (pic->f)
  618. av_frame_unref(pic->f);
  619. av_buffer_unref(&pic->hwaccel_priv_buf);
  620. if (pic->needs_realloc)
  621. ff_free_picture_tables(pic);
  622. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  623. }
  624. static int update_picture_tables(Picture *dst, Picture *src)
  625. {
  626. int i;
  627. #define UPDATE_TABLE(table)\
  628. do {\
  629. if (src->table &&\
  630. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  631. av_buffer_unref(&dst->table);\
  632. dst->table = av_buffer_ref(src->table);\
  633. if (!dst->table) {\
  634. ff_free_picture_tables(dst);\
  635. return AVERROR(ENOMEM);\
  636. }\
  637. }\
  638. } while (0)
  639. UPDATE_TABLE(mb_var_buf);
  640. UPDATE_TABLE(mc_mb_var_buf);
  641. UPDATE_TABLE(mb_mean_buf);
  642. UPDATE_TABLE(mbskip_table_buf);
  643. UPDATE_TABLE(qscale_table_buf);
  644. UPDATE_TABLE(mb_type_buf);
  645. for (i = 0; i < 2; i++) {
  646. UPDATE_TABLE(motion_val_buf[i]);
  647. UPDATE_TABLE(ref_index_buf[i]);
  648. }
  649. dst->mb_var = src->mb_var;
  650. dst->mc_mb_var = src->mc_mb_var;
  651. dst->mb_mean = src->mb_mean;
  652. dst->mbskip_table = src->mbskip_table;
  653. dst->qscale_table = src->qscale_table;
  654. dst->mb_type = src->mb_type;
  655. for (i = 0; i < 2; i++) {
  656. dst->motion_val[i] = src->motion_val[i];
  657. dst->ref_index[i] = src->ref_index[i];
  658. }
  659. dst->alloc_mb_width = src->alloc_mb_width;
  660. dst->alloc_mb_height = src->alloc_mb_height;
  661. return 0;
  662. }
  663. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  664. {
  665. int ret;
  666. av_assert0(!dst->f->buf[0]);
  667. av_assert0(src->f->buf[0]);
  668. src->tf.f = src->f;
  669. dst->tf.f = dst->f;
  670. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  671. if (ret < 0)
  672. goto fail;
  673. ret = update_picture_tables(dst, src);
  674. if (ret < 0)
  675. goto fail;
  676. if (src->hwaccel_picture_private) {
  677. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  678. if (!dst->hwaccel_priv_buf)
  679. goto fail;
  680. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  681. }
  682. dst->field_picture = src->field_picture;
  683. dst->mb_var_sum = src->mb_var_sum;
  684. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  685. dst->b_frame_score = src->b_frame_score;
  686. dst->needs_realloc = src->needs_realloc;
  687. dst->reference = src->reference;
  688. dst->shared = src->shared;
  689. return 0;
  690. fail:
  691. ff_mpeg_unref_picture(s, dst);
  692. return ret;
  693. }
  694. static void exchange_uv(MpegEncContext *s)
  695. {
  696. int16_t (*tmp)[64];
  697. tmp = s->pblocks[4];
  698. s->pblocks[4] = s->pblocks[5];
  699. s->pblocks[5] = tmp;
  700. }
  701. static int init_duplicate_context(MpegEncContext *s)
  702. {
  703. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  704. int c_size = s->mb_stride * (s->mb_height + 1);
  705. int yc_size = y_size + 2 * c_size;
  706. int i;
  707. if (s->mb_height & 1)
  708. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  709. s->edge_emu_buffer =
  710. s->me.scratchpad =
  711. s->me.temp =
  712. s->rd_scratchpad =
  713. s->b_scratchpad =
  714. s->obmc_scratchpad = NULL;
  715. if (s->encoding) {
  716. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  717. ME_MAP_SIZE * sizeof(uint32_t), fail)
  718. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  719. ME_MAP_SIZE * sizeof(uint32_t), fail)
  720. if (s->avctx->noise_reduction) {
  721. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  722. 2 * 64 * sizeof(int), fail)
  723. }
  724. }
  725. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  726. s->block = s->blocks[0];
  727. for (i = 0; i < 12; i++) {
  728. s->pblocks[i] = &s->block[i];
  729. }
  730. if (s->avctx->codec_tag == AV_RL32("VCR2"))
  731. exchange_uv(s);
  732. if (s->out_format == FMT_H263) {
  733. /* ac values */
  734. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  735. yc_size * sizeof(int16_t) * 16, fail);
  736. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  737. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  738. s->ac_val[2] = s->ac_val[1] + c_size;
  739. }
  740. return 0;
  741. fail:
  742. return -1; // free() through ff_MPV_common_end()
  743. }
  744. static void free_duplicate_context(MpegEncContext *s)
  745. {
  746. if (s == NULL)
  747. return;
  748. av_freep(&s->edge_emu_buffer);
  749. av_freep(&s->me.scratchpad);
  750. s->me.temp =
  751. s->rd_scratchpad =
  752. s->b_scratchpad =
  753. s->obmc_scratchpad = NULL;
  754. av_freep(&s->dct_error_sum);
  755. av_freep(&s->me.map);
  756. av_freep(&s->me.score_map);
  757. av_freep(&s->blocks);
  758. av_freep(&s->ac_val_base);
  759. s->block = NULL;
  760. }
  761. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  762. {
  763. #define COPY(a) bak->a = src->a
  764. COPY(edge_emu_buffer);
  765. COPY(me.scratchpad);
  766. COPY(me.temp);
  767. COPY(rd_scratchpad);
  768. COPY(b_scratchpad);
  769. COPY(obmc_scratchpad);
  770. COPY(me.map);
  771. COPY(me.score_map);
  772. COPY(blocks);
  773. COPY(block);
  774. COPY(start_mb_y);
  775. COPY(end_mb_y);
  776. COPY(me.map_generation);
  777. COPY(pb);
  778. COPY(dct_error_sum);
  779. COPY(dct_count[0]);
  780. COPY(dct_count[1]);
  781. COPY(ac_val_base);
  782. COPY(ac_val[0]);
  783. COPY(ac_val[1]);
  784. COPY(ac_val[2]);
  785. #undef COPY
  786. }
  787. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  788. {
  789. MpegEncContext bak;
  790. int i, ret;
  791. // FIXME copy only needed parts
  792. // START_TIMER
  793. backup_duplicate_context(&bak, dst);
  794. memcpy(dst, src, sizeof(MpegEncContext));
  795. backup_duplicate_context(dst, &bak);
  796. for (i = 0; i < 12; i++) {
  797. dst->pblocks[i] = &dst->block[i];
  798. }
  799. if (dst->avctx->codec_tag == AV_RL32("VCR2"))
  800. exchange_uv(dst);
  801. if (!dst->edge_emu_buffer &&
  802. (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
  803. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  804. "scratch buffers.\n");
  805. return ret;
  806. }
  807. // STOP_TIMER("update_duplicate_context")
  808. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  809. return 0;
  810. }
  811. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  812. const AVCodecContext *src)
  813. {
  814. int i, ret;
  815. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  816. if (dst == src)
  817. return 0;
  818. av_assert0(s != s1);
  819. // FIXME can parameters change on I-frames?
  820. // in that case dst may need a reinit
  821. if (!s->context_initialized) {
  822. memcpy(s, s1, sizeof(MpegEncContext));
  823. s->avctx = dst;
  824. s->bitstream_buffer = NULL;
  825. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  826. if (s1->context_initialized){
  827. // s->picture_range_start += MAX_PICTURE_COUNT;
  828. // s->picture_range_end += MAX_PICTURE_COUNT;
  829. if((ret = ff_MPV_common_init(s)) < 0){
  830. memset(s, 0, sizeof(MpegEncContext));
  831. s->avctx = dst;
  832. return ret;
  833. }
  834. }
  835. }
  836. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  837. s->context_reinit = 0;
  838. s->height = s1->height;
  839. s->width = s1->width;
  840. if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
  841. return ret;
  842. }
  843. s->avctx->coded_height = s1->avctx->coded_height;
  844. s->avctx->coded_width = s1->avctx->coded_width;
  845. s->avctx->width = s1->avctx->width;
  846. s->avctx->height = s1->avctx->height;
  847. s->coded_picture_number = s1->coded_picture_number;
  848. s->picture_number = s1->picture_number;
  849. av_assert0(!s->picture || s->picture != s1->picture);
  850. if(s->picture)
  851. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  852. ff_mpeg_unref_picture(s, &s->picture[i]);
  853. if (s1->picture[i].f->buf[0] &&
  854. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  855. return ret;
  856. }
  857. #define UPDATE_PICTURE(pic)\
  858. do {\
  859. ff_mpeg_unref_picture(s, &s->pic);\
  860. if (s1->pic.f && s1->pic.f->buf[0])\
  861. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  862. else\
  863. ret = update_picture_tables(&s->pic, &s1->pic);\
  864. if (ret < 0)\
  865. return ret;\
  866. } while (0)
  867. UPDATE_PICTURE(current_picture);
  868. UPDATE_PICTURE(last_picture);
  869. UPDATE_PICTURE(next_picture);
  870. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  871. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  872. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  873. // Error/bug resilience
  874. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  875. s->workaround_bugs = s1->workaround_bugs;
  876. s->padding_bug_score = s1->padding_bug_score;
  877. // MPEG4 timing info
  878. memcpy(&s->last_time_base, &s1->last_time_base,
  879. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  880. (char *) &s1->last_time_base);
  881. // B-frame info
  882. s->max_b_frames = s1->max_b_frames;
  883. s->low_delay = s1->low_delay;
  884. s->droppable = s1->droppable;
  885. // DivX handling (doesn't work)
  886. s->divx_packed = s1->divx_packed;
  887. if (s1->bitstream_buffer) {
  888. if (s1->bitstream_buffer_size +
  889. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  890. av_fast_malloc(&s->bitstream_buffer,
  891. &s->allocated_bitstream_buffer_size,
  892. s1->allocated_bitstream_buffer_size);
  893. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  894. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  895. s1->bitstream_buffer_size);
  896. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  897. FF_INPUT_BUFFER_PADDING_SIZE);
  898. }
  899. // linesize dependend scratch buffer allocation
  900. if (!s->edge_emu_buffer)
  901. if (s1->linesize) {
  902. if (frame_size_alloc(s, s1->linesize) < 0) {
  903. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  904. "scratch buffers.\n");
  905. return AVERROR(ENOMEM);
  906. }
  907. } else {
  908. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  909. "be allocated due to unknown size.\n");
  910. }
  911. // MPEG2/interlacing info
  912. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  913. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  914. if (!s1->first_field) {
  915. s->last_pict_type = s1->pict_type;
  916. if (s1->current_picture_ptr)
  917. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  918. }
  919. return 0;
  920. }
  921. /**
  922. * Set the given MpegEncContext to common defaults
  923. * (same for encoding and decoding).
  924. * The changed fields will not depend upon the
  925. * prior state of the MpegEncContext.
  926. */
  927. void ff_MPV_common_defaults(MpegEncContext *s)
  928. {
  929. s->y_dc_scale_table =
  930. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  931. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  932. s->progressive_frame = 1;
  933. s->progressive_sequence = 1;
  934. s->picture_structure = PICT_FRAME;
  935. s->coded_picture_number = 0;
  936. s->picture_number = 0;
  937. s->f_code = 1;
  938. s->b_code = 1;
  939. s->slice_context_count = 1;
  940. }
  941. /**
  942. * Set the given MpegEncContext to defaults for decoding.
  943. * the changed fields will not depend upon
  944. * the prior state of the MpegEncContext.
  945. */
  946. void ff_MPV_decode_defaults(MpegEncContext *s)
  947. {
  948. ff_MPV_common_defaults(s);
  949. }
  950. static int init_er(MpegEncContext *s)
  951. {
  952. ERContext *er = &s->er;
  953. int mb_array_size = s->mb_height * s->mb_stride;
  954. int i;
  955. er->avctx = s->avctx;
  956. er->dsp = &s->dsp;
  957. er->mb_index2xy = s->mb_index2xy;
  958. er->mb_num = s->mb_num;
  959. er->mb_width = s->mb_width;
  960. er->mb_height = s->mb_height;
  961. er->mb_stride = s->mb_stride;
  962. er->b8_stride = s->b8_stride;
  963. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  964. er->error_status_table = av_mallocz(mb_array_size);
  965. if (!er->er_temp_buffer || !er->error_status_table)
  966. goto fail;
  967. er->mbskip_table = s->mbskip_table;
  968. er->mbintra_table = s->mbintra_table;
  969. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  970. er->dc_val[i] = s->dc_val[i];
  971. er->decode_mb = mpeg_er_decode_mb;
  972. er->opaque = s;
  973. return 0;
  974. fail:
  975. av_freep(&er->er_temp_buffer);
  976. av_freep(&er->error_status_table);
  977. return AVERROR(ENOMEM);
  978. }
  979. /**
  980. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  981. */
  982. static int init_context_frame(MpegEncContext *s)
  983. {
  984. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  985. s->mb_width = (s->width + 15) / 16;
  986. s->mb_stride = s->mb_width + 1;
  987. s->b8_stride = s->mb_width * 2 + 1;
  988. mb_array_size = s->mb_height * s->mb_stride;
  989. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  990. /* set default edge pos, will be overridden
  991. * in decode_header if needed */
  992. s->h_edge_pos = s->mb_width * 16;
  993. s->v_edge_pos = s->mb_height * 16;
  994. s->mb_num = s->mb_width * s->mb_height;
  995. s->block_wrap[0] =
  996. s->block_wrap[1] =
  997. s->block_wrap[2] =
  998. s->block_wrap[3] = s->b8_stride;
  999. s->block_wrap[4] =
  1000. s->block_wrap[5] = s->mb_stride;
  1001. y_size = s->b8_stride * (2 * s->mb_height + 1);
  1002. c_size = s->mb_stride * (s->mb_height + 1);
  1003. yc_size = y_size + 2 * c_size;
  1004. if (s->mb_height & 1)
  1005. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  1006. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
  1007. for (y = 0; y < s->mb_height; y++)
  1008. for (x = 0; x < s->mb_width; x++)
  1009. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  1010. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  1011. if (s->encoding) {
  1012. /* Allocate MV tables */
  1013. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  1014. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  1015. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  1016. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  1017. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  1018. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  1019. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  1020. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  1021. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  1022. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  1023. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  1024. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  1025. /* Allocate MB type table */
  1026. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  1027. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  1028. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  1029. mb_array_size * sizeof(float), fail);
  1030. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  1031. mb_array_size * sizeof(float), fail);
  1032. }
  1033. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  1034. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  1035. /* interlaced direct mode decoding tables */
  1036. for (i = 0; i < 2; i++) {
  1037. int j, k;
  1038. for (j = 0; j < 2; j++) {
  1039. for (k = 0; k < 2; k++) {
  1040. FF_ALLOCZ_OR_GOTO(s->avctx,
  1041. s->b_field_mv_table_base[i][j][k],
  1042. mv_table_size * 2 * sizeof(int16_t),
  1043. fail);
  1044. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  1045. s->mb_stride + 1;
  1046. }
  1047. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  1048. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  1049. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  1050. }
  1051. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  1052. }
  1053. }
  1054. if (s->out_format == FMT_H263) {
  1055. /* cbp values */
  1056. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  1057. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  1058. /* cbp, ac_pred, pred_dir */
  1059. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  1060. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  1061. }
  1062. if (s->h263_pred || s->h263_plus || !s->encoding) {
  1063. /* dc values */
  1064. // MN: we need these for error resilience of intra-frames
  1065. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  1066. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  1067. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  1068. s->dc_val[2] = s->dc_val[1] + c_size;
  1069. for (i = 0; i < yc_size; i++)
  1070. s->dc_val_base[i] = 1024;
  1071. }
  1072. /* which mb is a intra block */
  1073. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  1074. memset(s->mbintra_table, 1, mb_array_size);
  1075. /* init macroblock skip table */
  1076. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  1077. // Note the + 1 is for a quicker mpeg4 slice_end detection
  1078. return init_er(s);
  1079. fail:
  1080. return AVERROR(ENOMEM);
  1081. }
  1082. /**
  1083. * init common structure for both encoder and decoder.
  1084. * this assumes that some variables like width/height are already set
  1085. */
  1086. av_cold int ff_MPV_common_init(MpegEncContext *s)
  1087. {
  1088. int i;
  1089. int nb_slices = (HAVE_THREADS &&
  1090. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  1091. s->avctx->thread_count : 1;
  1092. if (s->encoding && s->avctx->slices)
  1093. nb_slices = s->avctx->slices;
  1094. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1095. s->mb_height = (s->height + 31) / 32 * 2;
  1096. else
  1097. s->mb_height = (s->height + 15) / 16;
  1098. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  1099. av_log(s->avctx, AV_LOG_ERROR,
  1100. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  1101. return -1;
  1102. }
  1103. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  1104. int max_slices;
  1105. if (s->mb_height)
  1106. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  1107. else
  1108. max_slices = MAX_THREADS;
  1109. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  1110. " reducing to %d\n", nb_slices, max_slices);
  1111. nb_slices = max_slices;
  1112. }
  1113. if ((s->width || s->height) &&
  1114. av_image_check_size(s->width, s->height, 0, s->avctx))
  1115. return -1;
  1116. ff_dct_common_init(s);
  1117. s->flags = s->avctx->flags;
  1118. s->flags2 = s->avctx->flags2;
  1119. /* set chroma shifts */
  1120. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  1121. &s->chroma_x_shift,
  1122. &s->chroma_y_shift);
  1123. /* convert fourcc to upper case */
  1124. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  1125. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  1126. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  1127. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  1128. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1129. s->picture[i].f = av_frame_alloc();
  1130. if (!s->picture[i].f)
  1131. goto fail;
  1132. }
  1133. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1134. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1135. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1136. memset(&s->new_picture, 0, sizeof(s->new_picture));
  1137. s->next_picture.f = av_frame_alloc();
  1138. if (!s->next_picture.f)
  1139. goto fail;
  1140. s->last_picture.f = av_frame_alloc();
  1141. if (!s->last_picture.f)
  1142. goto fail;
  1143. s->current_picture.f = av_frame_alloc();
  1144. if (!s->current_picture.f)
  1145. goto fail;
  1146. s->new_picture.f = av_frame_alloc();
  1147. if (!s->new_picture.f)
  1148. goto fail;
  1149. if (init_context_frame(s))
  1150. goto fail;
  1151. s->parse_context.state = -1;
  1152. s->context_initialized = 1;
  1153. s->thread_context[0] = s;
  1154. // if (s->width && s->height) {
  1155. if (nb_slices > 1) {
  1156. for (i = 1; i < nb_slices; i++) {
  1157. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1158. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1159. }
  1160. for (i = 0; i < nb_slices; i++) {
  1161. if (init_duplicate_context(s->thread_context[i]) < 0)
  1162. goto fail;
  1163. s->thread_context[i]->start_mb_y =
  1164. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1165. s->thread_context[i]->end_mb_y =
  1166. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1167. }
  1168. } else {
  1169. if (init_duplicate_context(s) < 0)
  1170. goto fail;
  1171. s->start_mb_y = 0;
  1172. s->end_mb_y = s->mb_height;
  1173. }
  1174. s->slice_context_count = nb_slices;
  1175. // }
  1176. return 0;
  1177. fail:
  1178. ff_MPV_common_end(s);
  1179. return -1;
  1180. }
  1181. /**
  1182. * Frees and resets MpegEncContext fields depending on the resolution.
  1183. * Is used during resolution changes to avoid a full reinitialization of the
  1184. * codec.
  1185. */
  1186. static int free_context_frame(MpegEncContext *s)
  1187. {
  1188. int i, j, k;
  1189. av_freep(&s->mb_type);
  1190. av_freep(&s->p_mv_table_base);
  1191. av_freep(&s->b_forw_mv_table_base);
  1192. av_freep(&s->b_back_mv_table_base);
  1193. av_freep(&s->b_bidir_forw_mv_table_base);
  1194. av_freep(&s->b_bidir_back_mv_table_base);
  1195. av_freep(&s->b_direct_mv_table_base);
  1196. s->p_mv_table = NULL;
  1197. s->b_forw_mv_table = NULL;
  1198. s->b_back_mv_table = NULL;
  1199. s->b_bidir_forw_mv_table = NULL;
  1200. s->b_bidir_back_mv_table = NULL;
  1201. s->b_direct_mv_table = NULL;
  1202. for (i = 0; i < 2; i++) {
  1203. for (j = 0; j < 2; j++) {
  1204. for (k = 0; k < 2; k++) {
  1205. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1206. s->b_field_mv_table[i][j][k] = NULL;
  1207. }
  1208. av_freep(&s->b_field_select_table[i][j]);
  1209. av_freep(&s->p_field_mv_table_base[i][j]);
  1210. s->p_field_mv_table[i][j] = NULL;
  1211. }
  1212. av_freep(&s->p_field_select_table[i]);
  1213. }
  1214. av_freep(&s->dc_val_base);
  1215. av_freep(&s->coded_block_base);
  1216. av_freep(&s->mbintra_table);
  1217. av_freep(&s->cbp_table);
  1218. av_freep(&s->pred_dir_table);
  1219. av_freep(&s->mbskip_table);
  1220. av_freep(&s->er.error_status_table);
  1221. av_freep(&s->er.er_temp_buffer);
  1222. av_freep(&s->mb_index2xy);
  1223. av_freep(&s->lambda_table);
  1224. av_freep(&s->cplx_tab);
  1225. av_freep(&s->bits_tab);
  1226. s->linesize = s->uvlinesize = 0;
  1227. return 0;
  1228. }
  1229. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1230. {
  1231. int i, err = 0;
  1232. if (s->slice_context_count > 1) {
  1233. for (i = 0; i < s->slice_context_count; i++) {
  1234. free_duplicate_context(s->thread_context[i]);
  1235. }
  1236. for (i = 1; i < s->slice_context_count; i++) {
  1237. av_freep(&s->thread_context[i]);
  1238. }
  1239. } else
  1240. free_duplicate_context(s);
  1241. if ((err = free_context_frame(s)) < 0)
  1242. return err;
  1243. if (s->picture)
  1244. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1245. s->picture[i].needs_realloc = 1;
  1246. }
  1247. s->last_picture_ptr =
  1248. s->next_picture_ptr =
  1249. s->current_picture_ptr = NULL;
  1250. // init
  1251. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1252. s->mb_height = (s->height + 31) / 32 * 2;
  1253. else
  1254. s->mb_height = (s->height + 15) / 16;
  1255. if ((s->width || s->height) &&
  1256. av_image_check_size(s->width, s->height, 0, s->avctx))
  1257. return AVERROR_INVALIDDATA;
  1258. if ((err = init_context_frame(s)))
  1259. goto fail;
  1260. s->thread_context[0] = s;
  1261. if (s->width && s->height) {
  1262. int nb_slices = s->slice_context_count;
  1263. if (nb_slices > 1) {
  1264. for (i = 1; i < nb_slices; i++) {
  1265. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1266. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1267. }
  1268. for (i = 0; i < nb_slices; i++) {
  1269. if (init_duplicate_context(s->thread_context[i]) < 0)
  1270. goto fail;
  1271. s->thread_context[i]->start_mb_y =
  1272. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1273. s->thread_context[i]->end_mb_y =
  1274. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1275. }
  1276. } else {
  1277. err = init_duplicate_context(s);
  1278. if (err < 0)
  1279. goto fail;
  1280. s->start_mb_y = 0;
  1281. s->end_mb_y = s->mb_height;
  1282. }
  1283. s->slice_context_count = nb_slices;
  1284. }
  1285. return 0;
  1286. fail:
  1287. ff_MPV_common_end(s);
  1288. return err;
  1289. }
  1290. /* init common structure for both encoder and decoder */
  1291. void ff_MPV_common_end(MpegEncContext *s)
  1292. {
  1293. int i;
  1294. if (s->slice_context_count > 1) {
  1295. for (i = 0; i < s->slice_context_count; i++) {
  1296. free_duplicate_context(s->thread_context[i]);
  1297. }
  1298. for (i = 1; i < s->slice_context_count; i++) {
  1299. av_freep(&s->thread_context[i]);
  1300. }
  1301. s->slice_context_count = 1;
  1302. } else free_duplicate_context(s);
  1303. av_freep(&s->parse_context.buffer);
  1304. s->parse_context.buffer_size = 0;
  1305. av_freep(&s->bitstream_buffer);
  1306. s->allocated_bitstream_buffer_size = 0;
  1307. if (s->picture) {
  1308. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1309. ff_free_picture_tables(&s->picture[i]);
  1310. ff_mpeg_unref_picture(s, &s->picture[i]);
  1311. av_frame_free(&s->picture[i].f);
  1312. }
  1313. }
  1314. av_freep(&s->picture);
  1315. ff_free_picture_tables(&s->last_picture);
  1316. ff_mpeg_unref_picture(s, &s->last_picture);
  1317. av_frame_free(&s->last_picture.f);
  1318. ff_free_picture_tables(&s->current_picture);
  1319. ff_mpeg_unref_picture(s, &s->current_picture);
  1320. av_frame_free(&s->current_picture.f);
  1321. ff_free_picture_tables(&s->next_picture);
  1322. ff_mpeg_unref_picture(s, &s->next_picture);
  1323. av_frame_free(&s->next_picture.f);
  1324. ff_free_picture_tables(&s->new_picture);
  1325. ff_mpeg_unref_picture(s, &s->new_picture);
  1326. av_frame_free(&s->new_picture.f);
  1327. free_context_frame(s);
  1328. s->context_initialized = 0;
  1329. s->last_picture_ptr =
  1330. s->next_picture_ptr =
  1331. s->current_picture_ptr = NULL;
  1332. s->linesize = s->uvlinesize = 0;
  1333. }
  1334. av_cold void ff_init_rl(RLTable *rl,
  1335. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1336. {
  1337. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1338. uint8_t index_run[MAX_RUN + 1];
  1339. int last, run, level, start, end, i;
  1340. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1341. if (static_store && rl->max_level[0])
  1342. return;
  1343. /* compute max_level[], max_run[] and index_run[] */
  1344. for (last = 0; last < 2; last++) {
  1345. if (last == 0) {
  1346. start = 0;
  1347. end = rl->last;
  1348. } else {
  1349. start = rl->last;
  1350. end = rl->n;
  1351. }
  1352. memset(max_level, 0, MAX_RUN + 1);
  1353. memset(max_run, 0, MAX_LEVEL + 1);
  1354. memset(index_run, rl->n, MAX_RUN + 1);
  1355. for (i = start; i < end; i++) {
  1356. run = rl->table_run[i];
  1357. level = rl->table_level[i];
  1358. if (index_run[run] == rl->n)
  1359. index_run[run] = i;
  1360. if (level > max_level[run])
  1361. max_level[run] = level;
  1362. if (run > max_run[level])
  1363. max_run[level] = run;
  1364. }
  1365. if (static_store)
  1366. rl->max_level[last] = static_store[last];
  1367. else
  1368. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1369. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1370. if (static_store)
  1371. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1372. else
  1373. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1374. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1375. if (static_store)
  1376. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1377. else
  1378. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1379. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1380. }
  1381. }
  1382. av_cold void ff_init_vlc_rl(RLTable *rl)
  1383. {
  1384. int i, q;
  1385. for (q = 0; q < 32; q++) {
  1386. int qmul = q * 2;
  1387. int qadd = (q - 1) | 1;
  1388. if (q == 0) {
  1389. qmul = 1;
  1390. qadd = 0;
  1391. }
  1392. for (i = 0; i < rl->vlc.table_size; i++) {
  1393. int code = rl->vlc.table[i][0];
  1394. int len = rl->vlc.table[i][1];
  1395. int level, run;
  1396. if (len == 0) { // illegal code
  1397. run = 66;
  1398. level = MAX_LEVEL;
  1399. } else if (len < 0) { // more bits needed
  1400. run = 0;
  1401. level = code;
  1402. } else {
  1403. if (code == rl->n) { // esc
  1404. run = 66;
  1405. level = 0;
  1406. } else {
  1407. run = rl->table_run[code] + 1;
  1408. level = rl->table_level[code] * qmul + qadd;
  1409. if (code >= rl->last) run += 192;
  1410. }
  1411. }
  1412. rl->rl_vlc[q][i].len = len;
  1413. rl->rl_vlc[q][i].level = level;
  1414. rl->rl_vlc[q][i].run = run;
  1415. }
  1416. }
  1417. }
  1418. static void release_unused_pictures(MpegEncContext *s)
  1419. {
  1420. int i;
  1421. /* release non reference frames */
  1422. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1423. if (!s->picture[i].reference)
  1424. ff_mpeg_unref_picture(s, &s->picture[i]);
  1425. }
  1426. }
  1427. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1428. {
  1429. if (pic == s->last_picture_ptr)
  1430. return 0;
  1431. if (pic->f->buf[0] == NULL)
  1432. return 1;
  1433. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1434. return 1;
  1435. return 0;
  1436. }
  1437. static int find_unused_picture(MpegEncContext *s, int shared)
  1438. {
  1439. int i;
  1440. if (shared) {
  1441. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1442. if (s->picture[i].f->buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
  1443. return i;
  1444. }
  1445. } else {
  1446. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1447. if (pic_is_unused(s, &s->picture[i]))
  1448. return i;
  1449. }
  1450. }
  1451. av_log(s->avctx, AV_LOG_FATAL,
  1452. "Internal error, picture buffer overflow\n");
  1453. /* We could return -1, but the codec would crash trying to draw into a
  1454. * non-existing frame anyway. This is safer than waiting for a random crash.
  1455. * Also the return of this is never useful, an encoder must only allocate
  1456. * as much as allowed in the specification. This has no relationship to how
  1457. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1458. * enough for such valid streams).
  1459. * Plus, a decoder has to check stream validity and remove frames if too
  1460. * many reference frames are around. Waiting for "OOM" is not correct at
  1461. * all. Similarly, missing reference frames have to be replaced by
  1462. * interpolated/MC frames, anything else is a bug in the codec ...
  1463. */
  1464. abort();
  1465. return -1;
  1466. }
  1467. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1468. {
  1469. int ret = find_unused_picture(s, shared);
  1470. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1471. if (s->picture[ret].needs_realloc) {
  1472. s->picture[ret].needs_realloc = 0;
  1473. ff_free_picture_tables(&s->picture[ret]);
  1474. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1475. }
  1476. }
  1477. return ret;
  1478. }
  1479. static void gray_frame(AVFrame *frame)
  1480. {
  1481. int i, h_chroma_shift, v_chroma_shift;
  1482. av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
  1483. for(i=0; i<frame->height; i++)
  1484. memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
  1485. for(i=0; i<FF_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
  1486. memset(frame->data[1] + frame->linesize[1]*i,
  1487. 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1488. memset(frame->data[2] + frame->linesize[2]*i,
  1489. 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1490. }
  1491. }
  1492. /**
  1493. * generic function called after decoding
  1494. * the header and before a frame is decoded.
  1495. */
  1496. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1497. {
  1498. int i, ret;
  1499. Picture *pic;
  1500. s->mb_skipped = 0;
  1501. if (!ff_thread_can_start_frame(avctx)) {
  1502. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1503. return -1;
  1504. }
  1505. /* mark & release old frames */
  1506. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1507. s->last_picture_ptr != s->next_picture_ptr &&
  1508. s->last_picture_ptr->f->buf[0]) {
  1509. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1510. }
  1511. /* release forgotten pictures */
  1512. /* if (mpeg124/h263) */
  1513. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1514. if (&s->picture[i] != s->last_picture_ptr &&
  1515. &s->picture[i] != s->next_picture_ptr &&
  1516. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1517. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1518. av_log(avctx, AV_LOG_ERROR,
  1519. "releasing zombie picture\n");
  1520. ff_mpeg_unref_picture(s, &s->picture[i]);
  1521. }
  1522. }
  1523. ff_mpeg_unref_picture(s, &s->current_picture);
  1524. release_unused_pictures(s);
  1525. if (s->current_picture_ptr &&
  1526. s->current_picture_ptr->f->buf[0] == NULL) {
  1527. // we already have a unused image
  1528. // (maybe it was set before reading the header)
  1529. pic = s->current_picture_ptr;
  1530. } else {
  1531. i = ff_find_unused_picture(s, 0);
  1532. if (i < 0) {
  1533. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1534. return i;
  1535. }
  1536. pic = &s->picture[i];
  1537. }
  1538. pic->reference = 0;
  1539. if (!s->droppable) {
  1540. if (s->pict_type != AV_PICTURE_TYPE_B)
  1541. pic->reference = 3;
  1542. }
  1543. pic->f->coded_picture_number = s->coded_picture_number++;
  1544. if (ff_alloc_picture(s, pic, 0) < 0)
  1545. return -1;
  1546. s->current_picture_ptr = pic;
  1547. // FIXME use only the vars from current_pic
  1548. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1549. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1550. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1551. if (s->picture_structure != PICT_FRAME)
  1552. s->current_picture_ptr->f->top_field_first =
  1553. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1554. }
  1555. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1556. !s->progressive_sequence;
  1557. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1558. s->current_picture_ptr->f->pict_type = s->pict_type;
  1559. // if (s->flags && CODEC_FLAG_QSCALE)
  1560. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1561. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1562. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1563. s->current_picture_ptr)) < 0)
  1564. return ret;
  1565. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1566. s->last_picture_ptr = s->next_picture_ptr;
  1567. if (!s->droppable)
  1568. s->next_picture_ptr = s->current_picture_ptr;
  1569. }
  1570. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1571. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1572. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1573. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1574. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1575. s->pict_type, s->droppable);
  1576. if ((s->last_picture_ptr == NULL ||
  1577. s->last_picture_ptr->f->buf[0] == NULL) &&
  1578. (s->pict_type != AV_PICTURE_TYPE_I ||
  1579. s->picture_structure != PICT_FRAME)) {
  1580. int h_chroma_shift, v_chroma_shift;
  1581. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1582. &h_chroma_shift, &v_chroma_shift);
  1583. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
  1584. av_log(avctx, AV_LOG_DEBUG,
  1585. "allocating dummy last picture for B frame\n");
  1586. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1587. av_log(avctx, AV_LOG_ERROR,
  1588. "warning: first frame is no keyframe\n");
  1589. else if (s->picture_structure != PICT_FRAME)
  1590. av_log(avctx, AV_LOG_DEBUG,
  1591. "allocate dummy last picture for field based first keyframe\n");
  1592. /* Allocate a dummy frame */
  1593. i = ff_find_unused_picture(s, 0);
  1594. if (i < 0) {
  1595. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1596. return i;
  1597. }
  1598. s->last_picture_ptr = &s->picture[i];
  1599. s->last_picture_ptr->reference = 3;
  1600. s->last_picture_ptr->f->key_frame = 0;
  1601. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1602. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1603. s->last_picture_ptr = NULL;
  1604. return -1;
  1605. }
  1606. if (!avctx->hwaccel && !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
  1607. for(i=0; i<avctx->height; i++)
  1608. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
  1609. 0x80, avctx->width);
  1610. for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1611. memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
  1612. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1613. memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
  1614. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1615. }
  1616. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1617. for(i=0; i<avctx->height; i++)
  1618. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
  1619. }
  1620. }
  1621. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1622. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1623. }
  1624. if ((s->next_picture_ptr == NULL ||
  1625. s->next_picture_ptr->f->buf[0] == NULL) &&
  1626. s->pict_type == AV_PICTURE_TYPE_B) {
  1627. /* Allocate a dummy frame */
  1628. i = ff_find_unused_picture(s, 0);
  1629. if (i < 0) {
  1630. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1631. return i;
  1632. }
  1633. s->next_picture_ptr = &s->picture[i];
  1634. s->next_picture_ptr->reference = 3;
  1635. s->next_picture_ptr->f->key_frame = 0;
  1636. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1637. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1638. s->next_picture_ptr = NULL;
  1639. return -1;
  1640. }
  1641. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1642. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1643. }
  1644. #if 0 // BUFREF-FIXME
  1645. memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
  1646. memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
  1647. #endif
  1648. if (s->last_picture_ptr) {
  1649. ff_mpeg_unref_picture(s, &s->last_picture);
  1650. if (s->last_picture_ptr->f->buf[0] &&
  1651. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1652. s->last_picture_ptr)) < 0)
  1653. return ret;
  1654. }
  1655. if (s->next_picture_ptr) {
  1656. ff_mpeg_unref_picture(s, &s->next_picture);
  1657. if (s->next_picture_ptr->f->buf[0] &&
  1658. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1659. s->next_picture_ptr)) < 0)
  1660. return ret;
  1661. }
  1662. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1663. s->last_picture_ptr->f->buf[0]));
  1664. if (s->picture_structure!= PICT_FRAME) {
  1665. int i;
  1666. for (i = 0; i < 4; i++) {
  1667. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1668. s->current_picture.f->data[i] +=
  1669. s->current_picture.f->linesize[i];
  1670. }
  1671. s->current_picture.f->linesize[i] *= 2;
  1672. s->last_picture.f->linesize[i] *= 2;
  1673. s->next_picture.f->linesize[i] *= 2;
  1674. }
  1675. }
  1676. s->err_recognition = avctx->err_recognition;
  1677. /* set dequantizer, we can't do it during init as
  1678. * it might change for mpeg4 and we can't do it in the header
  1679. * decode as init is not called for mpeg4 there yet */
  1680. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1681. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1682. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1683. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1684. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1685. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1686. } else {
  1687. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1688. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1689. }
  1690. if (s->avctx->debug & FF_DEBUG_NOMC) {
  1691. gray_frame(s->current_picture_ptr->f);
  1692. }
  1693. return 0;
  1694. }
  1695. /* called after a frame has been decoded. */
  1696. void ff_MPV_frame_end(MpegEncContext *s)
  1697. {
  1698. emms_c();
  1699. if (s->current_picture.reference)
  1700. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1701. }
  1702. static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
  1703. {
  1704. if(*sx > *ex)
  1705. return clip_line(ex, ey, sx, sy, maxx);
  1706. if (*sx < 0) {
  1707. if (*ex < 0)
  1708. return 1;
  1709. *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
  1710. *sx = 0;
  1711. }
  1712. if (*ex > maxx) {
  1713. if (*sx > maxx)
  1714. return 1;
  1715. *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
  1716. *ex = maxx;
  1717. }
  1718. return 0;
  1719. }
  1720. /**
  1721. * Draw a line from (ex, ey) -> (sx, sy).
  1722. * @param w width of the image
  1723. * @param h height of the image
  1724. * @param stride stride/linesize of the image
  1725. * @param color color of the arrow
  1726. */
  1727. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1728. int w, int h, int stride, int color)
  1729. {
  1730. int x, y, fr, f;
  1731. if (clip_line(&sx, &sy, &ex, &ey, w - 1))
  1732. return;
  1733. if (clip_line(&sy, &sx, &ey, &ex, h - 1))
  1734. return;
  1735. sx = av_clip(sx, 0, w - 1);
  1736. sy = av_clip(sy, 0, h - 1);
  1737. ex = av_clip(ex, 0, w - 1);
  1738. ey = av_clip(ey, 0, h - 1);
  1739. buf[sy * stride + sx] += color;
  1740. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1741. if (sx > ex) {
  1742. FFSWAP(int, sx, ex);
  1743. FFSWAP(int, sy, ey);
  1744. }
  1745. buf += sx + sy * stride;
  1746. ex -= sx;
  1747. f = ((ey - sy) << 16) / ex;
  1748. for (x = 0; x <= ex; x++) {
  1749. y = (x * f) >> 16;
  1750. fr = (x * f) & 0xFFFF;
  1751. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1752. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1753. }
  1754. } else {
  1755. if (sy > ey) {
  1756. FFSWAP(int, sx, ex);
  1757. FFSWAP(int, sy, ey);
  1758. }
  1759. buf += sx + sy * stride;
  1760. ey -= sy;
  1761. if (ey)
  1762. f = ((ex - sx) << 16) / ey;
  1763. else
  1764. f = 0;
  1765. for(y= 0; y <= ey; y++){
  1766. x = (y*f) >> 16;
  1767. fr = (y*f) & 0xFFFF;
  1768. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1769. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1770. }
  1771. }
  1772. }
  1773. /**
  1774. * Draw an arrow from (ex, ey) -> (sx, sy).
  1775. * @param w width of the image
  1776. * @param h height of the image
  1777. * @param stride stride/linesize of the image
  1778. * @param color color of the arrow
  1779. */
  1780. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1781. int ey, int w, int h, int stride, int color, int tail, int direction)
  1782. {
  1783. int dx,dy;
  1784. if (direction) {
  1785. FFSWAP(int, sx, ex);
  1786. FFSWAP(int, sy, ey);
  1787. }
  1788. sx = av_clip(sx, -100, w + 100);
  1789. sy = av_clip(sy, -100, h + 100);
  1790. ex = av_clip(ex, -100, w + 100);
  1791. ey = av_clip(ey, -100, h + 100);
  1792. dx = ex - sx;
  1793. dy = ey - sy;
  1794. if (dx * dx + dy * dy > 3 * 3) {
  1795. int rx = dx + dy;
  1796. int ry = -dx + dy;
  1797. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1798. // FIXME subpixel accuracy
  1799. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1800. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1801. if (tail) {
  1802. rx = -rx;
  1803. ry = -ry;
  1804. }
  1805. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1806. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1807. }
  1808. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1809. }
  1810. /**
  1811. * Print debugging info for the given picture.
  1812. */
  1813. void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
  1814. uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
  1815. int *low_delay,
  1816. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1817. {
  1818. if (avctx->hwaccel || !mbtype_table
  1819. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1820. return;
  1821. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1822. int x,y;
  1823. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1824. av_get_picture_type_char(pict->pict_type));
  1825. for (y = 0; y < mb_height; y++) {
  1826. for (x = 0; x < mb_width; x++) {
  1827. if (avctx->debug & FF_DEBUG_SKIP) {
  1828. int count = mbskip_table[x + y * mb_stride];
  1829. if (count > 9)
  1830. count = 9;
  1831. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1832. }
  1833. if (avctx->debug & FF_DEBUG_QP) {
  1834. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1835. qscale_table[x + y * mb_stride]);
  1836. }
  1837. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1838. int mb_type = mbtype_table[x + y * mb_stride];
  1839. // Type & MV direction
  1840. if (IS_PCM(mb_type))
  1841. av_log(avctx, AV_LOG_DEBUG, "P");
  1842. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1843. av_log(avctx, AV_LOG_DEBUG, "A");
  1844. else if (IS_INTRA4x4(mb_type))
  1845. av_log(avctx, AV_LOG_DEBUG, "i");
  1846. else if (IS_INTRA16x16(mb_type))
  1847. av_log(avctx, AV_LOG_DEBUG, "I");
  1848. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1849. av_log(avctx, AV_LOG_DEBUG, "d");
  1850. else if (IS_DIRECT(mb_type))
  1851. av_log(avctx, AV_LOG_DEBUG, "D");
  1852. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1853. av_log(avctx, AV_LOG_DEBUG, "g");
  1854. else if (IS_GMC(mb_type))
  1855. av_log(avctx, AV_LOG_DEBUG, "G");
  1856. else if (IS_SKIP(mb_type))
  1857. av_log(avctx, AV_LOG_DEBUG, "S");
  1858. else if (!USES_LIST(mb_type, 1))
  1859. av_log(avctx, AV_LOG_DEBUG, ">");
  1860. else if (!USES_LIST(mb_type, 0))
  1861. av_log(avctx, AV_LOG_DEBUG, "<");
  1862. else {
  1863. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1864. av_log(avctx, AV_LOG_DEBUG, "X");
  1865. }
  1866. // segmentation
  1867. if (IS_8X8(mb_type))
  1868. av_log(avctx, AV_LOG_DEBUG, "+");
  1869. else if (IS_16X8(mb_type))
  1870. av_log(avctx, AV_LOG_DEBUG, "-");
  1871. else if (IS_8X16(mb_type))
  1872. av_log(avctx, AV_LOG_DEBUG, "|");
  1873. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1874. av_log(avctx, AV_LOG_DEBUG, " ");
  1875. else
  1876. av_log(avctx, AV_LOG_DEBUG, "?");
  1877. if (IS_INTERLACED(mb_type))
  1878. av_log(avctx, AV_LOG_DEBUG, "=");
  1879. else
  1880. av_log(avctx, AV_LOG_DEBUG, " ");
  1881. }
  1882. }
  1883. av_log(avctx, AV_LOG_DEBUG, "\n");
  1884. }
  1885. }
  1886. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1887. (avctx->debug_mv)) {
  1888. const int shift = 1 + quarter_sample;
  1889. int mb_y;
  1890. uint8_t *ptr;
  1891. int i;
  1892. int h_chroma_shift, v_chroma_shift, block_height;
  1893. const int width = avctx->width;
  1894. const int height = avctx->height;
  1895. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1896. const int mv_stride = (mb_width << mv_sample_log2) +
  1897. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1898. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1899. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1900. av_frame_make_writable(pict);
  1901. pict->opaque = NULL;
  1902. ptr = pict->data[0];
  1903. block_height = 16 >> v_chroma_shift;
  1904. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1905. int mb_x;
  1906. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1907. const int mb_index = mb_x + mb_y * mb_stride;
  1908. if ((avctx->debug_mv) && motion_val[0]) {
  1909. int type;
  1910. for (type = 0; type < 3; type++) {
  1911. int direction = 0;
  1912. switch (type) {
  1913. case 0:
  1914. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1915. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1916. continue;
  1917. direction = 0;
  1918. break;
  1919. case 1:
  1920. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1921. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1922. continue;
  1923. direction = 0;
  1924. break;
  1925. case 2:
  1926. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1927. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1928. continue;
  1929. direction = 1;
  1930. break;
  1931. }
  1932. if (!USES_LIST(mbtype_table[mb_index], direction))
  1933. continue;
  1934. if (IS_8X8(mbtype_table[mb_index])) {
  1935. int i;
  1936. for (i = 0; i < 4; i++) {
  1937. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1938. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1939. int xy = (mb_x * 2 + (i & 1) +
  1940. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1941. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1942. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1943. draw_arrow(ptr, sx, sy, mx, my, width,
  1944. height, pict->linesize[0], 100, 0, direction);
  1945. }
  1946. } else if (IS_16X8(mbtype_table[mb_index])) {
  1947. int i;
  1948. for (i = 0; i < 2; i++) {
  1949. int sx = mb_x * 16 + 8;
  1950. int sy = mb_y * 16 + 4 + 8 * i;
  1951. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1952. int mx = (motion_val[direction][xy][0] >> shift);
  1953. int my = (motion_val[direction][xy][1] >> shift);
  1954. if (IS_INTERLACED(mbtype_table[mb_index]))
  1955. my *= 2;
  1956. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1957. height, pict->linesize[0], 100, 0, direction);
  1958. }
  1959. } else if (IS_8X16(mbtype_table[mb_index])) {
  1960. int i;
  1961. for (i = 0; i < 2; i++) {
  1962. int sx = mb_x * 16 + 4 + 8 * i;
  1963. int sy = mb_y * 16 + 8;
  1964. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1965. int mx = motion_val[direction][xy][0] >> shift;
  1966. int my = motion_val[direction][xy][1] >> shift;
  1967. if (IS_INTERLACED(mbtype_table[mb_index]))
  1968. my *= 2;
  1969. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1970. height, pict->linesize[0], 100, 0, direction);
  1971. }
  1972. } else {
  1973. int sx= mb_x * 16 + 8;
  1974. int sy= mb_y * 16 + 8;
  1975. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1976. int mx= (motion_val[direction][xy][0]>>shift) + sx;
  1977. int my= (motion_val[direction][xy][1]>>shift) + sy;
  1978. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100, 0, direction);
  1979. }
  1980. }
  1981. }
  1982. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1983. uint64_t c = (qscale_table[mb_index] * 128 / 31) *
  1984. 0x0101010101010101ULL;
  1985. int y;
  1986. for (y = 0; y < block_height; y++) {
  1987. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1988. (block_height * mb_y + y) *
  1989. pict->linesize[1]) = c;
  1990. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1991. (block_height * mb_y + y) *
  1992. pict->linesize[2]) = c;
  1993. }
  1994. }
  1995. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1996. motion_val[0]) {
  1997. int mb_type = mbtype_table[mb_index];
  1998. uint64_t u,v;
  1999. int y;
  2000. #define COLOR(theta, r) \
  2001. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  2002. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  2003. u = v = 128;
  2004. if (IS_PCM(mb_type)) {
  2005. COLOR(120, 48)
  2006. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  2007. IS_INTRA16x16(mb_type)) {
  2008. COLOR(30, 48)
  2009. } else if (IS_INTRA4x4(mb_type)) {
  2010. COLOR(90, 48)
  2011. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  2012. // COLOR(120, 48)
  2013. } else if (IS_DIRECT(mb_type)) {
  2014. COLOR(150, 48)
  2015. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  2016. COLOR(170, 48)
  2017. } else if (IS_GMC(mb_type)) {
  2018. COLOR(190, 48)
  2019. } else if (IS_SKIP(mb_type)) {
  2020. // COLOR(180, 48)
  2021. } else if (!USES_LIST(mb_type, 1)) {
  2022. COLOR(240, 48)
  2023. } else if (!USES_LIST(mb_type, 0)) {
  2024. COLOR(0, 48)
  2025. } else {
  2026. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  2027. COLOR(300,48)
  2028. }
  2029. u *= 0x0101010101010101ULL;
  2030. v *= 0x0101010101010101ULL;
  2031. for (y = 0; y < block_height; y++) {
  2032. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  2033. (block_height * mb_y + y) * pict->linesize[1]) = u;
  2034. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  2035. (block_height * mb_y + y) * pict->linesize[2]) = v;
  2036. }
  2037. // segmentation
  2038. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  2039. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  2040. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  2041. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  2042. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  2043. }
  2044. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  2045. for (y = 0; y < 16; y++)
  2046. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  2047. pict->linesize[0]] ^= 0x80;
  2048. }
  2049. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  2050. int dm = 1 << (mv_sample_log2 - 2);
  2051. for (i = 0; i < 4; i++) {
  2052. int sx = mb_x * 16 + 8 * (i & 1);
  2053. int sy = mb_y * 16 + 8 * (i >> 1);
  2054. int xy = (mb_x * 2 + (i & 1) +
  2055. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  2056. // FIXME bidir
  2057. int32_t *mv = (int32_t *) &motion_val[0][xy];
  2058. if (mv[0] != mv[dm] ||
  2059. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  2060. for (y = 0; y < 8; y++)
  2061. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  2062. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  2063. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  2064. pict->linesize[0]) ^= 0x8080808080808080ULL;
  2065. }
  2066. }
  2067. if (IS_INTERLACED(mb_type) &&
  2068. avctx->codec->id == AV_CODEC_ID_H264) {
  2069. // hmm
  2070. }
  2071. }
  2072. mbskip_table[mb_index] = 0;
  2073. }
  2074. }
  2075. }
  2076. }
  2077. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  2078. {
  2079. ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
  2080. p->qscale_table, p->motion_val, &s->low_delay,
  2081. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  2082. }
  2083. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  2084. {
  2085. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  2086. int offset = 2*s->mb_stride + 1;
  2087. if(!ref)
  2088. return AVERROR(ENOMEM);
  2089. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  2090. ref->size -= offset;
  2091. ref->data += offset;
  2092. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  2093. }
  2094. static inline int hpel_motion_lowres(MpegEncContext *s,
  2095. uint8_t *dest, uint8_t *src,
  2096. int field_based, int field_select,
  2097. int src_x, int src_y,
  2098. int width, int height, ptrdiff_t stride,
  2099. int h_edge_pos, int v_edge_pos,
  2100. int w, int h, h264_chroma_mc_func *pix_op,
  2101. int motion_x, int motion_y)
  2102. {
  2103. const int lowres = s->avctx->lowres;
  2104. const int op_index = FFMIN(lowres, 3);
  2105. const int s_mask = (2 << lowres) - 1;
  2106. int emu = 0;
  2107. int sx, sy;
  2108. if (s->quarter_sample) {
  2109. motion_x /= 2;
  2110. motion_y /= 2;
  2111. }
  2112. sx = motion_x & s_mask;
  2113. sy = motion_y & s_mask;
  2114. src_x += motion_x >> lowres + 1;
  2115. src_y += motion_y >> lowres + 1;
  2116. src += src_y * stride + src_x;
  2117. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  2118. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2119. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  2120. s->linesize, s->linesize,
  2121. w + 1, (h + 1) << field_based,
  2122. src_x, src_y << field_based,
  2123. h_edge_pos, v_edge_pos);
  2124. src = s->edge_emu_buffer;
  2125. emu = 1;
  2126. }
  2127. sx = (sx << 2) >> lowres;
  2128. sy = (sy << 2) >> lowres;
  2129. if (field_select)
  2130. src += s->linesize;
  2131. pix_op[op_index](dest, src, stride, h, sx, sy);
  2132. return emu;
  2133. }
  2134. /* apply one mpeg motion vector to the three components */
  2135. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  2136. uint8_t *dest_y,
  2137. uint8_t *dest_cb,
  2138. uint8_t *dest_cr,
  2139. int field_based,
  2140. int bottom_field,
  2141. int field_select,
  2142. uint8_t **ref_picture,
  2143. h264_chroma_mc_func *pix_op,
  2144. int motion_x, int motion_y,
  2145. int h, int mb_y)
  2146. {
  2147. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2148. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  2149. ptrdiff_t uvlinesize, linesize;
  2150. const int lowres = s->avctx->lowres;
  2151. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  2152. const int block_s = 8>>lowres;
  2153. const int s_mask = (2 << lowres) - 1;
  2154. const int h_edge_pos = s->h_edge_pos >> lowres;
  2155. const int v_edge_pos = s->v_edge_pos >> lowres;
  2156. linesize = s->current_picture.f->linesize[0] << field_based;
  2157. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  2158. // FIXME obviously not perfect but qpel will not work in lowres anyway
  2159. if (s->quarter_sample) {
  2160. motion_x /= 2;
  2161. motion_y /= 2;
  2162. }
  2163. if(field_based){
  2164. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  2165. }
  2166. sx = motion_x & s_mask;
  2167. sy = motion_y & s_mask;
  2168. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  2169. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  2170. if (s->out_format == FMT_H263) {
  2171. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  2172. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  2173. uvsrc_x = src_x >> 1;
  2174. uvsrc_y = src_y >> 1;
  2175. } else if (s->out_format == FMT_H261) {
  2176. // even chroma mv's are full pel in H261
  2177. mx = motion_x / 4;
  2178. my = motion_y / 4;
  2179. uvsx = (2 * mx) & s_mask;
  2180. uvsy = (2 * my) & s_mask;
  2181. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  2182. uvsrc_y = mb_y * block_s + (my >> lowres);
  2183. } else {
  2184. if(s->chroma_y_shift){
  2185. mx = motion_x / 2;
  2186. my = motion_y / 2;
  2187. uvsx = mx & s_mask;
  2188. uvsy = my & s_mask;
  2189. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  2190. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  2191. } else {
  2192. if(s->chroma_x_shift){
  2193. //Chroma422
  2194. mx = motion_x / 2;
  2195. uvsx = mx & s_mask;
  2196. uvsy = motion_y & s_mask;
  2197. uvsrc_y = src_y;
  2198. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2199. } else {
  2200. //Chroma444
  2201. uvsx = motion_x & s_mask;
  2202. uvsy = motion_y & s_mask;
  2203. uvsrc_x = src_x;
  2204. uvsrc_y = src_y;
  2205. }
  2206. }
  2207. }
  2208. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2209. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2210. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2211. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  2212. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2213. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2214. linesize >> field_based, linesize >> field_based,
  2215. 17, 17 + field_based,
  2216. src_x, src_y << field_based, h_edge_pos,
  2217. v_edge_pos);
  2218. ptr_y = s->edge_emu_buffer;
  2219. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2220. uint8_t *ubuf = s->edge_emu_buffer + 18 * s->linesize;
  2221. uint8_t *vbuf =ubuf + 9 * s->uvlinesize;
  2222. s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
  2223. uvlinesize >> field_based, uvlinesize >> field_based,
  2224. 9, 9 + field_based,
  2225. uvsrc_x, uvsrc_y << field_based,
  2226. h_edge_pos >> 1, v_edge_pos >> 1);
  2227. s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
  2228. uvlinesize >> field_based,uvlinesize >> field_based,
  2229. 9, 9 + field_based,
  2230. uvsrc_x, uvsrc_y << field_based,
  2231. h_edge_pos >> 1, v_edge_pos >> 1);
  2232. ptr_cb = ubuf;
  2233. ptr_cr = vbuf;
  2234. }
  2235. }
  2236. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
  2237. if (bottom_field) {
  2238. dest_y += s->linesize;
  2239. dest_cb += s->uvlinesize;
  2240. dest_cr += s->uvlinesize;
  2241. }
  2242. if (field_select) {
  2243. ptr_y += s->linesize;
  2244. ptr_cb += s->uvlinesize;
  2245. ptr_cr += s->uvlinesize;
  2246. }
  2247. sx = (sx << 2) >> lowres;
  2248. sy = (sy << 2) >> lowres;
  2249. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2250. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2251. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  2252. uvsx = (uvsx << 2) >> lowres;
  2253. uvsy = (uvsy << 2) >> lowres;
  2254. if (hc) {
  2255. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  2256. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  2257. }
  2258. }
  2259. // FIXME h261 lowres loop filter
  2260. }
  2261. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2262. uint8_t *dest_cb, uint8_t *dest_cr,
  2263. uint8_t **ref_picture,
  2264. h264_chroma_mc_func * pix_op,
  2265. int mx, int my)
  2266. {
  2267. const int lowres = s->avctx->lowres;
  2268. const int op_index = FFMIN(lowres, 3);
  2269. const int block_s = 8 >> lowres;
  2270. const int s_mask = (2 << lowres) - 1;
  2271. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2272. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2273. int emu = 0, src_x, src_y, sx, sy;
  2274. ptrdiff_t offset;
  2275. uint8_t *ptr;
  2276. if (s->quarter_sample) {
  2277. mx /= 2;
  2278. my /= 2;
  2279. }
  2280. /* In case of 8X8, we construct a single chroma motion vector
  2281. with a special rounding */
  2282. mx = ff_h263_round_chroma(mx);
  2283. my = ff_h263_round_chroma(my);
  2284. sx = mx & s_mask;
  2285. sy = my & s_mask;
  2286. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2287. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2288. offset = src_y * s->uvlinesize + src_x;
  2289. ptr = ref_picture[1] + offset;
  2290. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2291. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2292. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2293. s->uvlinesize, s->uvlinesize,
  2294. 9, 9,
  2295. src_x, src_y, h_edge_pos, v_edge_pos);
  2296. ptr = s->edge_emu_buffer;
  2297. emu = 1;
  2298. }
  2299. sx = (sx << 2) >> lowres;
  2300. sy = (sy << 2) >> lowres;
  2301. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2302. ptr = ref_picture[2] + offset;
  2303. if (emu) {
  2304. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2305. s->uvlinesize, s->uvlinesize,
  2306. 9, 9,
  2307. src_x, src_y, h_edge_pos, v_edge_pos);
  2308. ptr = s->edge_emu_buffer;
  2309. }
  2310. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2311. }
  2312. /**
  2313. * motion compensation of a single macroblock
  2314. * @param s context
  2315. * @param dest_y luma destination pointer
  2316. * @param dest_cb chroma cb/u destination pointer
  2317. * @param dest_cr chroma cr/v destination pointer
  2318. * @param dir direction (0->forward, 1->backward)
  2319. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2320. * @param pix_op halfpel motion compensation function (average or put normally)
  2321. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2322. */
  2323. static inline void MPV_motion_lowres(MpegEncContext *s,
  2324. uint8_t *dest_y, uint8_t *dest_cb,
  2325. uint8_t *dest_cr,
  2326. int dir, uint8_t **ref_picture,
  2327. h264_chroma_mc_func *pix_op)
  2328. {
  2329. int mx, my;
  2330. int mb_x, mb_y, i;
  2331. const int lowres = s->avctx->lowres;
  2332. const int block_s = 8 >>lowres;
  2333. mb_x = s->mb_x;
  2334. mb_y = s->mb_y;
  2335. switch (s->mv_type) {
  2336. case MV_TYPE_16X16:
  2337. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2338. 0, 0, 0,
  2339. ref_picture, pix_op,
  2340. s->mv[dir][0][0], s->mv[dir][0][1],
  2341. 2 * block_s, mb_y);
  2342. break;
  2343. case MV_TYPE_8X8:
  2344. mx = 0;
  2345. my = 0;
  2346. for (i = 0; i < 4; i++) {
  2347. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2348. s->linesize) * block_s,
  2349. ref_picture[0], 0, 0,
  2350. (2 * mb_x + (i & 1)) * block_s,
  2351. (2 * mb_y + (i >> 1)) * block_s,
  2352. s->width, s->height, s->linesize,
  2353. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2354. block_s, block_s, pix_op,
  2355. s->mv[dir][i][0], s->mv[dir][i][1]);
  2356. mx += s->mv[dir][i][0];
  2357. my += s->mv[dir][i][1];
  2358. }
  2359. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2360. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2361. pix_op, mx, my);
  2362. break;
  2363. case MV_TYPE_FIELD:
  2364. if (s->picture_structure == PICT_FRAME) {
  2365. /* top field */
  2366. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2367. 1, 0, s->field_select[dir][0],
  2368. ref_picture, pix_op,
  2369. s->mv[dir][0][0], s->mv[dir][0][1],
  2370. block_s, mb_y);
  2371. /* bottom field */
  2372. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2373. 1, 1, s->field_select[dir][1],
  2374. ref_picture, pix_op,
  2375. s->mv[dir][1][0], s->mv[dir][1][1],
  2376. block_s, mb_y);
  2377. } else {
  2378. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2379. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2380. ref_picture = s->current_picture_ptr->f->data;
  2381. }
  2382. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2383. 0, 0, s->field_select[dir][0],
  2384. ref_picture, pix_op,
  2385. s->mv[dir][0][0],
  2386. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2387. }
  2388. break;
  2389. case MV_TYPE_16X8:
  2390. for (i = 0; i < 2; i++) {
  2391. uint8_t **ref2picture;
  2392. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2393. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2394. ref2picture = ref_picture;
  2395. } else {
  2396. ref2picture = s->current_picture_ptr->f->data;
  2397. }
  2398. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2399. 0, 0, s->field_select[dir][i],
  2400. ref2picture, pix_op,
  2401. s->mv[dir][i][0], s->mv[dir][i][1] +
  2402. 2 * block_s * i, block_s, mb_y >> 1);
  2403. dest_y += 2 * block_s * s->linesize;
  2404. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2405. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2406. }
  2407. break;
  2408. case MV_TYPE_DMV:
  2409. if (s->picture_structure == PICT_FRAME) {
  2410. for (i = 0; i < 2; i++) {
  2411. int j;
  2412. for (j = 0; j < 2; j++) {
  2413. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2414. 1, j, j ^ i,
  2415. ref_picture, pix_op,
  2416. s->mv[dir][2 * i + j][0],
  2417. s->mv[dir][2 * i + j][1],
  2418. block_s, mb_y);
  2419. }
  2420. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2421. }
  2422. } else {
  2423. for (i = 0; i < 2; i++) {
  2424. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2425. 0, 0, s->picture_structure != i + 1,
  2426. ref_picture, pix_op,
  2427. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2428. 2 * block_s, mb_y >> 1);
  2429. // after put we make avg of the same block
  2430. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2431. // opposite parity is always in the same
  2432. // frame if this is second field
  2433. if (!s->first_field) {
  2434. ref_picture = s->current_picture_ptr->f->data;
  2435. }
  2436. }
  2437. }
  2438. break;
  2439. default:
  2440. av_assert2(0);
  2441. }
  2442. }
  2443. /**
  2444. * find the lowest MB row referenced in the MVs
  2445. */
  2446. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2447. {
  2448. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2449. int my, off, i, mvs;
  2450. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2451. goto unhandled;
  2452. switch (s->mv_type) {
  2453. case MV_TYPE_16X16:
  2454. mvs = 1;
  2455. break;
  2456. case MV_TYPE_16X8:
  2457. mvs = 2;
  2458. break;
  2459. case MV_TYPE_8X8:
  2460. mvs = 4;
  2461. break;
  2462. default:
  2463. goto unhandled;
  2464. }
  2465. for (i = 0; i < mvs; i++) {
  2466. my = s->mv[dir][i][1]<<qpel_shift;
  2467. my_max = FFMAX(my_max, my);
  2468. my_min = FFMIN(my_min, my);
  2469. }
  2470. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2471. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2472. unhandled:
  2473. return s->mb_height-1;
  2474. }
  2475. /* put block[] to dest[] */
  2476. static inline void put_dct(MpegEncContext *s,
  2477. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2478. {
  2479. s->dct_unquantize_intra(s, block, i, qscale);
  2480. s->idsp.idct_put(dest, line_size, block);
  2481. }
  2482. /* add block[] to dest[] */
  2483. static inline void add_dct(MpegEncContext *s,
  2484. int16_t *block, int i, uint8_t *dest, int line_size)
  2485. {
  2486. if (s->block_last_index[i] >= 0) {
  2487. s->idsp.idct_add(dest, line_size, block);
  2488. }
  2489. }
  2490. static inline void add_dequant_dct(MpegEncContext *s,
  2491. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2492. {
  2493. if (s->block_last_index[i] >= 0) {
  2494. s->dct_unquantize_inter(s, block, i, qscale);
  2495. s->idsp.idct_add(dest, line_size, block);
  2496. }
  2497. }
  2498. /**
  2499. * Clean dc, ac, coded_block for the current non-intra MB.
  2500. */
  2501. void ff_clean_intra_table_entries(MpegEncContext *s)
  2502. {
  2503. int wrap = s->b8_stride;
  2504. int xy = s->block_index[0];
  2505. s->dc_val[0][xy ] =
  2506. s->dc_val[0][xy + 1 ] =
  2507. s->dc_val[0][xy + wrap] =
  2508. s->dc_val[0][xy + 1 + wrap] = 1024;
  2509. /* ac pred */
  2510. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2511. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2512. if (s->msmpeg4_version>=3) {
  2513. s->coded_block[xy ] =
  2514. s->coded_block[xy + 1 ] =
  2515. s->coded_block[xy + wrap] =
  2516. s->coded_block[xy + 1 + wrap] = 0;
  2517. }
  2518. /* chroma */
  2519. wrap = s->mb_stride;
  2520. xy = s->mb_x + s->mb_y * wrap;
  2521. s->dc_val[1][xy] =
  2522. s->dc_val[2][xy] = 1024;
  2523. /* ac pred */
  2524. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2525. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2526. s->mbintra_table[xy]= 0;
  2527. }
  2528. /* generic function called after a macroblock has been parsed by the
  2529. decoder or after it has been encoded by the encoder.
  2530. Important variables used:
  2531. s->mb_intra : true if intra macroblock
  2532. s->mv_dir : motion vector direction
  2533. s->mv_type : motion vector type
  2534. s->mv : motion vector
  2535. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2536. */
  2537. static av_always_inline
  2538. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2539. int lowres_flag, int is_mpeg12)
  2540. {
  2541. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2542. if (CONFIG_XVMC &&
  2543. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2544. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2545. return;
  2546. }
  2547. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2548. /* print DCT coefficients */
  2549. int i,j;
  2550. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2551. for(i=0; i<6; i++){
  2552. for(j=0; j<64; j++){
  2553. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  2554. block[i][s->idsp.idct_permutation[j]]);
  2555. }
  2556. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2557. }
  2558. }
  2559. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2560. /* update DC predictors for P macroblocks */
  2561. if (!s->mb_intra) {
  2562. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2563. if(s->mbintra_table[mb_xy])
  2564. ff_clean_intra_table_entries(s);
  2565. } else {
  2566. s->last_dc[0] =
  2567. s->last_dc[1] =
  2568. s->last_dc[2] = 128 << s->intra_dc_precision;
  2569. }
  2570. }
  2571. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2572. s->mbintra_table[mb_xy]=1;
  2573. if ( (s->flags&CODEC_FLAG_PSNR)
  2574. || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
  2575. || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  2576. uint8_t *dest_y, *dest_cb, *dest_cr;
  2577. int dct_linesize, dct_offset;
  2578. op_pixels_func (*op_pix)[4];
  2579. qpel_mc_func (*op_qpix)[16];
  2580. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2581. const int uvlinesize = s->current_picture.f->linesize[1];
  2582. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2583. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2584. /* avoid copy if macroblock skipped in last frame too */
  2585. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2586. if(!s->encoding){
  2587. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2588. if (s->mb_skipped) {
  2589. s->mb_skipped= 0;
  2590. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2591. *mbskip_ptr = 1;
  2592. } else if(!s->current_picture.reference) {
  2593. *mbskip_ptr = 1;
  2594. } else{
  2595. *mbskip_ptr = 0; /* not skipped */
  2596. }
  2597. }
  2598. dct_linesize = linesize << s->interlaced_dct;
  2599. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2600. if(readable){
  2601. dest_y= s->dest[0];
  2602. dest_cb= s->dest[1];
  2603. dest_cr= s->dest[2];
  2604. }else{
  2605. dest_y = s->b_scratchpad;
  2606. dest_cb= s->b_scratchpad+16*linesize;
  2607. dest_cr= s->b_scratchpad+32*linesize;
  2608. }
  2609. if (!s->mb_intra) {
  2610. /* motion handling */
  2611. /* decoding or more than one mb_type (MC was already done otherwise) */
  2612. if(!s->encoding){
  2613. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2614. if (s->mv_dir & MV_DIR_FORWARD) {
  2615. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2616. ff_MPV_lowest_referenced_row(s, 0),
  2617. 0);
  2618. }
  2619. if (s->mv_dir & MV_DIR_BACKWARD) {
  2620. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2621. ff_MPV_lowest_referenced_row(s, 1),
  2622. 0);
  2623. }
  2624. }
  2625. if(lowres_flag){
  2626. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2627. if (s->mv_dir & MV_DIR_FORWARD) {
  2628. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
  2629. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2630. }
  2631. if (s->mv_dir & MV_DIR_BACKWARD) {
  2632. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
  2633. }
  2634. }else{
  2635. op_qpix = s->me.qpel_put;
  2636. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2637. op_pix = s->hdsp.put_pixels_tab;
  2638. }else{
  2639. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2640. }
  2641. if (s->mv_dir & MV_DIR_FORWARD) {
  2642. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  2643. op_pix = s->hdsp.avg_pixels_tab;
  2644. op_qpix= s->me.qpel_avg;
  2645. }
  2646. if (s->mv_dir & MV_DIR_BACKWARD) {
  2647. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  2648. }
  2649. }
  2650. }
  2651. /* skip dequant / idct if we are really late ;) */
  2652. if(s->avctx->skip_idct){
  2653. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2654. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2655. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2656. goto skip_idct;
  2657. }
  2658. /* add dct residue */
  2659. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2660. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2661. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2662. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2663. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2664. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2665. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2666. if (s->chroma_y_shift){
  2667. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2668. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2669. }else{
  2670. dct_linesize >>= 1;
  2671. dct_offset >>=1;
  2672. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2673. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2674. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2675. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2676. }
  2677. }
  2678. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2679. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2680. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2681. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2682. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2683. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2684. if(s->chroma_y_shift){//Chroma420
  2685. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2686. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2687. }else{
  2688. //chroma422
  2689. dct_linesize = uvlinesize << s->interlaced_dct;
  2690. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2691. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2692. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2693. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2694. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2695. if(!s->chroma_x_shift){//Chroma444
  2696. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2697. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2698. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2699. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2700. }
  2701. }
  2702. }//fi gray
  2703. }
  2704. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2705. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2706. }
  2707. } else {
  2708. /* dct only in intra block */
  2709. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2710. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2711. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2712. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2713. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2714. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2715. if(s->chroma_y_shift){
  2716. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2717. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2718. }else{
  2719. dct_offset >>=1;
  2720. dct_linesize >>=1;
  2721. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2722. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2723. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2724. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2725. }
  2726. }
  2727. }else{
  2728. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  2729. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2730. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  2731. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2732. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2733. if(s->chroma_y_shift){
  2734. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  2735. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  2736. }else{
  2737. dct_linesize = uvlinesize << s->interlaced_dct;
  2738. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2739. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  2740. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  2741. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2742. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2743. if(!s->chroma_x_shift){//Chroma444
  2744. s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2745. s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2746. s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2747. s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2748. }
  2749. }
  2750. }//gray
  2751. }
  2752. }
  2753. skip_idct:
  2754. if(!readable){
  2755. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2756. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2757. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2758. }
  2759. }
  2760. }
  2761. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2762. #if !CONFIG_SMALL
  2763. if(s->out_format == FMT_MPEG1) {
  2764. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2765. else MPV_decode_mb_internal(s, block, 0, 1);
  2766. } else
  2767. #endif
  2768. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2769. else MPV_decode_mb_internal(s, block, 0, 0);
  2770. }
  2771. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2772. {
  2773. ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
  2774. s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
  2775. s->first_field, s->low_delay);
  2776. }
  2777. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2778. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2779. const int uvlinesize = s->current_picture.f->linesize[1];
  2780. const int mb_size= 4 - s->avctx->lowres;
  2781. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2782. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2783. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2784. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2785. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2786. 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;
  2787. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2788. s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
  2789. s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2790. s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2791. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2792. {
  2793. if(s->picture_structure==PICT_FRAME){
  2794. s->dest[0] += s->mb_y * linesize << mb_size;
  2795. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2796. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2797. }else{
  2798. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2799. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2800. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2801. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2802. }
  2803. }
  2804. }
  2805. /**
  2806. * Permute an 8x8 block.
  2807. * @param block the block which will be permuted according to the given permutation vector
  2808. * @param permutation the permutation vector
  2809. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2810. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2811. * (inverse) permutated to scantable order!
  2812. */
  2813. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2814. {
  2815. int i;
  2816. int16_t temp[64];
  2817. if(last<=0) return;
  2818. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2819. for(i=0; i<=last; i++){
  2820. const int j= scantable[i];
  2821. temp[j]= block[j];
  2822. block[j]=0;
  2823. }
  2824. for(i=0; i<=last; i++){
  2825. const int j= scantable[i];
  2826. const int perm_j= permutation[j];
  2827. block[perm_j]= temp[j];
  2828. }
  2829. }
  2830. void ff_mpeg_flush(AVCodecContext *avctx){
  2831. int i;
  2832. MpegEncContext *s = avctx->priv_data;
  2833. if(s==NULL || s->picture==NULL)
  2834. return;
  2835. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2836. ff_mpeg_unref_picture(s, &s->picture[i]);
  2837. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2838. ff_mpeg_unref_picture(s, &s->current_picture);
  2839. ff_mpeg_unref_picture(s, &s->last_picture);
  2840. ff_mpeg_unref_picture(s, &s->next_picture);
  2841. s->mb_x= s->mb_y= 0;
  2842. s->closed_gop= 0;
  2843. s->parse_context.state= -1;
  2844. s->parse_context.frame_start_found= 0;
  2845. s->parse_context.overread= 0;
  2846. s->parse_context.overread_index= 0;
  2847. s->parse_context.index= 0;
  2848. s->parse_context.last_index= 0;
  2849. s->bitstream_buffer_size=0;
  2850. s->pp_time=0;
  2851. }
  2852. /**
  2853. * set qscale and update qscale dependent variables.
  2854. */
  2855. void ff_set_qscale(MpegEncContext * s, int qscale)
  2856. {
  2857. if (qscale < 1)
  2858. qscale = 1;
  2859. else if (qscale > 31)
  2860. qscale = 31;
  2861. s->qscale = qscale;
  2862. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2863. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2864. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2865. }
  2866. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2867. {
  2868. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2869. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2870. }