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.

3279 lines
117KB

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