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.

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