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.

3233 lines
118KB

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