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.

3416 lines
123KB

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