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.

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