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.

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