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.

3231 lines
117KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/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. s->thread_context[0] = s;
  1100. // if (s->width && s->height) {
  1101. if (nb_slices > 1) {
  1102. for (i = 0; i < nb_slices; i++) {
  1103. if (i) {
  1104. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1105. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1106. }
  1107. if (init_duplicate_context(s->thread_context[i]) < 0)
  1108. goto fail;
  1109. s->thread_context[i]->start_mb_y =
  1110. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1111. s->thread_context[i]->end_mb_y =
  1112. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1113. }
  1114. } else {
  1115. if (init_duplicate_context(s) < 0)
  1116. goto fail;
  1117. s->start_mb_y = 0;
  1118. s->end_mb_y = s->mb_height;
  1119. }
  1120. s->slice_context_count = nb_slices;
  1121. // }
  1122. return 0;
  1123. fail:
  1124. ff_mpv_common_end(s);
  1125. return -1;
  1126. }
  1127. /**
  1128. * Frees and resets MpegEncContext fields depending on the resolution.
  1129. * Is used during resolution changes to avoid a full reinitialization of the
  1130. * codec.
  1131. */
  1132. static void free_context_frame(MpegEncContext *s)
  1133. {
  1134. int i, j, k;
  1135. av_freep(&s->mb_type);
  1136. av_freep(&s->p_mv_table_base);
  1137. av_freep(&s->b_forw_mv_table_base);
  1138. av_freep(&s->b_back_mv_table_base);
  1139. av_freep(&s->b_bidir_forw_mv_table_base);
  1140. av_freep(&s->b_bidir_back_mv_table_base);
  1141. av_freep(&s->b_direct_mv_table_base);
  1142. s->p_mv_table = NULL;
  1143. s->b_forw_mv_table = NULL;
  1144. s->b_back_mv_table = NULL;
  1145. s->b_bidir_forw_mv_table = NULL;
  1146. s->b_bidir_back_mv_table = NULL;
  1147. s->b_direct_mv_table = NULL;
  1148. for (i = 0; i < 2; i++) {
  1149. for (j = 0; j < 2; j++) {
  1150. for (k = 0; k < 2; k++) {
  1151. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1152. s->b_field_mv_table[i][j][k] = NULL;
  1153. }
  1154. av_freep(&s->b_field_select_table[i][j]);
  1155. av_freep(&s->p_field_mv_table_base[i][j]);
  1156. s->p_field_mv_table[i][j] = NULL;
  1157. }
  1158. av_freep(&s->p_field_select_table[i]);
  1159. }
  1160. av_freep(&s->dc_val_base);
  1161. av_freep(&s->coded_block_base);
  1162. av_freep(&s->mbintra_table);
  1163. av_freep(&s->cbp_table);
  1164. av_freep(&s->pred_dir_table);
  1165. av_freep(&s->mbskip_table);
  1166. av_freep(&s->er.error_status_table);
  1167. av_freep(&s->er.er_temp_buffer);
  1168. av_freep(&s->mb_index2xy);
  1169. av_freep(&s->lambda_table);
  1170. av_freep(&s->cplx_tab);
  1171. av_freep(&s->bits_tab);
  1172. s->linesize = s->uvlinesize = 0;
  1173. }
  1174. int ff_mpv_common_frame_size_change(MpegEncContext *s)
  1175. {
  1176. int i, err = 0;
  1177. if (!s->context_initialized)
  1178. return AVERROR(EINVAL);
  1179. if (s->slice_context_count > 1) {
  1180. for (i = 0; i < s->slice_context_count; i++) {
  1181. free_duplicate_context(s->thread_context[i]);
  1182. }
  1183. for (i = 1; i < s->slice_context_count; i++) {
  1184. av_freep(&s->thread_context[i]);
  1185. }
  1186. } else
  1187. free_duplicate_context(s);
  1188. free_context_frame(s);
  1189. if (s->picture)
  1190. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1191. s->picture[i].needs_realloc = 1;
  1192. }
  1193. s->last_picture_ptr =
  1194. s->next_picture_ptr =
  1195. s->current_picture_ptr = NULL;
  1196. // init
  1197. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1198. s->mb_height = (s->height + 31) / 32 * 2;
  1199. else
  1200. s->mb_height = (s->height + 15) / 16;
  1201. if ((s->width || s->height) &&
  1202. (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  1203. goto fail;
  1204. if ((err = init_context_frame(s)))
  1205. goto fail;
  1206. s->thread_context[0] = s;
  1207. if (s->width && s->height) {
  1208. int nb_slices = s->slice_context_count;
  1209. if (nb_slices > 1) {
  1210. for (i = 0; i < nb_slices; i++) {
  1211. if (i) {
  1212. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1213. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1214. }
  1215. if ((err = init_duplicate_context(s->thread_context[i])) < 0)
  1216. goto fail;
  1217. s->thread_context[i]->start_mb_y =
  1218. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1219. s->thread_context[i]->end_mb_y =
  1220. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1221. }
  1222. } else {
  1223. err = init_duplicate_context(s);
  1224. if (err < 0)
  1225. goto fail;
  1226. s->start_mb_y = 0;
  1227. s->end_mb_y = s->mb_height;
  1228. }
  1229. s->slice_context_count = nb_slices;
  1230. }
  1231. return 0;
  1232. fail:
  1233. ff_mpv_common_end(s);
  1234. return err;
  1235. }
  1236. /* init common structure for both encoder and decoder */
  1237. void ff_mpv_common_end(MpegEncContext *s)
  1238. {
  1239. int i;
  1240. if (s->slice_context_count > 1) {
  1241. for (i = 0; i < s->slice_context_count; i++) {
  1242. free_duplicate_context(s->thread_context[i]);
  1243. }
  1244. for (i = 1; i < s->slice_context_count; i++) {
  1245. av_freep(&s->thread_context[i]);
  1246. }
  1247. s->slice_context_count = 1;
  1248. } else free_duplicate_context(s);
  1249. av_freep(&s->parse_context.buffer);
  1250. s->parse_context.buffer_size = 0;
  1251. av_freep(&s->bitstream_buffer);
  1252. s->allocated_bitstream_buffer_size = 0;
  1253. if (s->picture) {
  1254. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1255. ff_free_picture_tables(&s->picture[i]);
  1256. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1257. av_frame_free(&s->picture[i].f);
  1258. }
  1259. }
  1260. av_freep(&s->picture);
  1261. ff_free_picture_tables(&s->last_picture);
  1262. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1263. av_frame_free(&s->last_picture.f);
  1264. ff_free_picture_tables(&s->current_picture);
  1265. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1266. av_frame_free(&s->current_picture.f);
  1267. ff_free_picture_tables(&s->next_picture);
  1268. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1269. av_frame_free(&s->next_picture.f);
  1270. ff_free_picture_tables(&s->new_picture);
  1271. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  1272. av_frame_free(&s->new_picture.f);
  1273. free_context_frame(s);
  1274. s->context_initialized = 0;
  1275. s->last_picture_ptr =
  1276. s->next_picture_ptr =
  1277. s->current_picture_ptr = NULL;
  1278. s->linesize = s->uvlinesize = 0;
  1279. }
  1280. static void release_unused_pictures(AVCodecContext *avctx, Picture *picture)
  1281. {
  1282. int i;
  1283. /* release non reference frames */
  1284. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1285. if (!picture[i].reference)
  1286. ff_mpeg_unref_picture(avctx, &picture[i]);
  1287. }
  1288. }
  1289. static inline int pic_is_unused(Picture *pic)
  1290. {
  1291. if (!pic->f->buf[0])
  1292. return 1;
  1293. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1294. return 1;
  1295. return 0;
  1296. }
  1297. static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  1298. {
  1299. int i;
  1300. if (shared) {
  1301. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1302. if (!picture[i].f->buf[0])
  1303. return i;
  1304. }
  1305. } else {
  1306. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1307. if (pic_is_unused(&picture[i]))
  1308. return i;
  1309. }
  1310. }
  1311. av_log(avctx, AV_LOG_FATAL,
  1312. "Internal error, picture buffer overflow\n");
  1313. /* We could return -1, but the codec would crash trying to draw into a
  1314. * non-existing frame anyway. This is safer than waiting for a random crash.
  1315. * Also the return of this is never useful, an encoder must only allocate
  1316. * as much as allowed in the specification. This has no relationship to how
  1317. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1318. * enough for such valid streams).
  1319. * Plus, a decoder has to check stream validity and remove frames if too
  1320. * many reference frames are around. Waiting for "OOM" is not correct at
  1321. * all. Similarly, missing reference frames have to be replaced by
  1322. * interpolated/MC frames, anything else is a bug in the codec ...
  1323. */
  1324. abort();
  1325. return -1;
  1326. }
  1327. int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  1328. {
  1329. int ret = find_unused_picture(avctx, picture, shared);
  1330. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1331. if (picture[ret].needs_realloc) {
  1332. picture[ret].needs_realloc = 0;
  1333. ff_free_picture_tables(&picture[ret]);
  1334. ff_mpeg_unref_picture(avctx, &picture[ret]);
  1335. }
  1336. }
  1337. return ret;
  1338. }
  1339. static void gray_frame(AVFrame *frame)
  1340. {
  1341. int i, h_chroma_shift, v_chroma_shift;
  1342. av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
  1343. for(i=0; i<frame->height; i++)
  1344. memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
  1345. for(i=0; i<FF_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
  1346. memset(frame->data[1] + frame->linesize[1]*i,
  1347. 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1348. memset(frame->data[2] + frame->linesize[2]*i,
  1349. 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1350. }
  1351. }
  1352. /**
  1353. * generic function called after decoding
  1354. * the header and before a frame is decoded.
  1355. */
  1356. int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1357. {
  1358. int i, ret;
  1359. Picture *pic;
  1360. s->mb_skipped = 0;
  1361. if (!ff_thread_can_start_frame(avctx)) {
  1362. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1363. return -1;
  1364. }
  1365. /* mark & release old frames */
  1366. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1367. s->last_picture_ptr != s->next_picture_ptr &&
  1368. s->last_picture_ptr->f->buf[0]) {
  1369. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1370. }
  1371. /* release forgotten pictures */
  1372. /* if (mpeg124/h263) */
  1373. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1374. if (&s->picture[i] != s->last_picture_ptr &&
  1375. &s->picture[i] != s->next_picture_ptr &&
  1376. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1377. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1378. av_log(avctx, AV_LOG_ERROR,
  1379. "releasing zombie picture\n");
  1380. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1381. }
  1382. }
  1383. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1384. release_unused_pictures(s->avctx, s->picture);
  1385. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  1386. // we already have a unused image
  1387. // (maybe it was set before reading the header)
  1388. pic = s->current_picture_ptr;
  1389. } else {
  1390. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1391. if (i < 0) {
  1392. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1393. return i;
  1394. }
  1395. pic = &s->picture[i];
  1396. }
  1397. pic->reference = 0;
  1398. if (!s->droppable) {
  1399. if (s->pict_type != AV_PICTURE_TYPE_B)
  1400. pic->reference = 3;
  1401. }
  1402. pic->f->coded_picture_number = s->coded_picture_number++;
  1403. if (ff_alloc_picture(s, pic, 0) < 0)
  1404. return -1;
  1405. s->current_picture_ptr = pic;
  1406. // FIXME use only the vars from current_pic
  1407. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1408. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1409. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1410. if (s->picture_structure != PICT_FRAME)
  1411. s->current_picture_ptr->f->top_field_first =
  1412. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1413. }
  1414. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1415. !s->progressive_sequence;
  1416. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1417. s->current_picture_ptr->f->pict_type = s->pict_type;
  1418. // if (s->avctx->flags && CODEC_FLAG_QSCALE)
  1419. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1420. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1421. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1422. s->current_picture_ptr)) < 0)
  1423. return ret;
  1424. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1425. s->last_picture_ptr = s->next_picture_ptr;
  1426. if (!s->droppable)
  1427. s->next_picture_ptr = s->current_picture_ptr;
  1428. }
  1429. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1430. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1431. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1432. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1433. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1434. s->pict_type, s->droppable);
  1435. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  1436. (s->pict_type != AV_PICTURE_TYPE_I ||
  1437. s->picture_structure != PICT_FRAME)) {
  1438. int h_chroma_shift, v_chroma_shift;
  1439. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1440. &h_chroma_shift, &v_chroma_shift);
  1441. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
  1442. av_log(avctx, AV_LOG_DEBUG,
  1443. "allocating dummy last picture for B frame\n");
  1444. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1445. av_log(avctx, AV_LOG_ERROR,
  1446. "warning: first frame is no keyframe\n");
  1447. else if (s->picture_structure != PICT_FRAME)
  1448. av_log(avctx, AV_LOG_DEBUG,
  1449. "allocate dummy last picture for field based first keyframe\n");
  1450. /* Allocate a dummy frame */
  1451. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1452. if (i < 0) {
  1453. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1454. return i;
  1455. }
  1456. s->last_picture_ptr = &s->picture[i];
  1457. s->last_picture_ptr->reference = 3;
  1458. s->last_picture_ptr->f->key_frame = 0;
  1459. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1460. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1461. s->last_picture_ptr = NULL;
  1462. return -1;
  1463. }
  1464. if (!avctx->hwaccel && !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
  1465. for(i=0; i<avctx->height; i++)
  1466. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
  1467. 0x80, avctx->width);
  1468. if (s->last_picture_ptr->f->data[2]) {
  1469. for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1470. memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
  1471. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1472. memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
  1473. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1474. }
  1475. }
  1476. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1477. for(i=0; i<avctx->height; i++)
  1478. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
  1479. }
  1480. }
  1481. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1482. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1483. }
  1484. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1485. s->pict_type == AV_PICTURE_TYPE_B) {
  1486. /* Allocate a dummy frame */
  1487. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1488. if (i < 0) {
  1489. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1490. return i;
  1491. }
  1492. s->next_picture_ptr = &s->picture[i];
  1493. s->next_picture_ptr->reference = 3;
  1494. s->next_picture_ptr->f->key_frame = 0;
  1495. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1496. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1497. s->next_picture_ptr = NULL;
  1498. return -1;
  1499. }
  1500. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1501. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1502. }
  1503. #if 0 // BUFREF-FIXME
  1504. memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
  1505. memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
  1506. #endif
  1507. if (s->last_picture_ptr) {
  1508. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1509. if (s->last_picture_ptr->f->buf[0] &&
  1510. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1511. s->last_picture_ptr)) < 0)
  1512. return ret;
  1513. }
  1514. if (s->next_picture_ptr) {
  1515. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1516. if (s->next_picture_ptr->f->buf[0] &&
  1517. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1518. s->next_picture_ptr)) < 0)
  1519. return ret;
  1520. }
  1521. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1522. s->last_picture_ptr->f->buf[0]));
  1523. if (s->picture_structure!= PICT_FRAME) {
  1524. int i;
  1525. for (i = 0; i < 4; i++) {
  1526. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1527. s->current_picture.f->data[i] +=
  1528. s->current_picture.f->linesize[i];
  1529. }
  1530. s->current_picture.f->linesize[i] *= 2;
  1531. s->last_picture.f->linesize[i] *= 2;
  1532. s->next_picture.f->linesize[i] *= 2;
  1533. }
  1534. }
  1535. /* set dequantizer, we can't do it during init as
  1536. * it might change for mpeg4 and we can't do it in the header
  1537. * decode as init is not called for mpeg4 there yet */
  1538. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1539. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1540. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1541. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1542. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1543. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1544. } else {
  1545. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1546. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1547. }
  1548. if (s->avctx->debug & FF_DEBUG_NOMC) {
  1549. gray_frame(s->current_picture_ptr->f);
  1550. }
  1551. return 0;
  1552. }
  1553. /* called after a frame has been decoded. */
  1554. void ff_mpv_frame_end(MpegEncContext *s)
  1555. {
  1556. emms_c();
  1557. if (s->current_picture.reference)
  1558. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1559. }
  1560. #if FF_API_VISMV
  1561. static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
  1562. {
  1563. if(*sx > *ex)
  1564. return clip_line(ex, ey, sx, sy, maxx);
  1565. if (*sx < 0) {
  1566. if (*ex < 0)
  1567. return 1;
  1568. *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
  1569. *sx = 0;
  1570. }
  1571. if (*ex > maxx) {
  1572. if (*sx > maxx)
  1573. return 1;
  1574. *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
  1575. *ex = maxx;
  1576. }
  1577. return 0;
  1578. }
  1579. /**
  1580. * Draw a line from (ex, ey) -> (sx, sy).
  1581. * @param w width of the image
  1582. * @param h height of the image
  1583. * @param stride stride/linesize of the image
  1584. * @param color color of the arrow
  1585. */
  1586. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1587. int w, int h, int stride, int color)
  1588. {
  1589. int x, y, fr, f;
  1590. if (clip_line(&sx, &sy, &ex, &ey, w - 1))
  1591. return;
  1592. if (clip_line(&sy, &sx, &ey, &ex, h - 1))
  1593. return;
  1594. sx = av_clip(sx, 0, w - 1);
  1595. sy = av_clip(sy, 0, h - 1);
  1596. ex = av_clip(ex, 0, w - 1);
  1597. ey = av_clip(ey, 0, h - 1);
  1598. buf[sy * stride + sx] += color;
  1599. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1600. if (sx > ex) {
  1601. FFSWAP(int, sx, ex);
  1602. FFSWAP(int, sy, ey);
  1603. }
  1604. buf += sx + sy * stride;
  1605. ex -= sx;
  1606. f = ((ey - sy) << 16) / ex;
  1607. for (x = 0; x <= ex; x++) {
  1608. y = (x * f) >> 16;
  1609. fr = (x * f) & 0xFFFF;
  1610. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1611. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1612. }
  1613. } else {
  1614. if (sy > ey) {
  1615. FFSWAP(int, sx, ex);
  1616. FFSWAP(int, sy, ey);
  1617. }
  1618. buf += sx + sy * stride;
  1619. ey -= sy;
  1620. if (ey)
  1621. f = ((ex - sx) << 16) / ey;
  1622. else
  1623. f = 0;
  1624. for(y= 0; y <= ey; y++){
  1625. x = (y*f) >> 16;
  1626. fr = (y*f) & 0xFFFF;
  1627. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1628. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1629. }
  1630. }
  1631. }
  1632. /**
  1633. * Draw an arrow from (ex, ey) -> (sx, sy).
  1634. * @param w width of the image
  1635. * @param h height of the image
  1636. * @param stride stride/linesize of the image
  1637. * @param color color of the arrow
  1638. */
  1639. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1640. int ey, int w, int h, int stride, int color, int tail, int direction)
  1641. {
  1642. int dx,dy;
  1643. if (direction) {
  1644. FFSWAP(int, sx, ex);
  1645. FFSWAP(int, sy, ey);
  1646. }
  1647. sx = av_clip(sx, -100, w + 100);
  1648. sy = av_clip(sy, -100, h + 100);
  1649. ex = av_clip(ex, -100, w + 100);
  1650. ey = av_clip(ey, -100, h + 100);
  1651. dx = ex - sx;
  1652. dy = ey - sy;
  1653. if (dx * dx + dy * dy > 3 * 3) {
  1654. int rx = dx + dy;
  1655. int ry = -dx + dy;
  1656. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1657. // FIXME subpixel accuracy
  1658. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1659. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1660. if (tail) {
  1661. rx = -rx;
  1662. ry = -ry;
  1663. }
  1664. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1665. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1666. }
  1667. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1668. }
  1669. #endif
  1670. static int add_mb(AVMotionVector *mb, uint32_t mb_type,
  1671. int dst_x, int dst_y,
  1672. int src_x, int src_y,
  1673. int direction)
  1674. {
  1675. mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
  1676. mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
  1677. mb->src_x = src_x;
  1678. mb->src_y = src_y;
  1679. mb->dst_x = dst_x;
  1680. mb->dst_y = dst_y;
  1681. mb->source = direction ? 1 : -1;
  1682. mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
  1683. return 1;
  1684. }
  1685. /**
  1686. * Print debugging info for the given picture.
  1687. */
  1688. void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
  1689. uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
  1690. int *low_delay,
  1691. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1692. {
  1693. if ((avctx->flags2 & CODEC_FLAG2_EXPORT_MVS) && mbtype_table && motion_val[0]) {
  1694. const int shift = 1 + quarter_sample;
  1695. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1696. const int mv_stride = (mb_width << mv_sample_log2) +
  1697. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1698. int mb_x, mb_y, mbcount = 0;
  1699. /* size is width * height * 2 * 4 where 2 is for directions and 4 is
  1700. * for the maximum number of MB (4 MB in case of IS_8x8) */
  1701. AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
  1702. if (!mvs)
  1703. return;
  1704. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1705. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1706. int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
  1707. for (direction = 0; direction < 2; direction++) {
  1708. if (!USES_LIST(mb_type, direction))
  1709. continue;
  1710. if (IS_8X8(mb_type)) {
  1711. for (i = 0; i < 4; i++) {
  1712. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1713. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1714. int xy = (mb_x * 2 + (i & 1) +
  1715. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1716. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1717. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1718. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
  1719. }
  1720. } else if (IS_16X8(mb_type)) {
  1721. for (i = 0; i < 2; i++) {
  1722. int sx = mb_x * 16 + 8;
  1723. int sy = mb_y * 16 + 4 + 8 * i;
  1724. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1725. int mx = (motion_val[direction][xy][0] >> shift);
  1726. int my = (motion_val[direction][xy][1] >> shift);
  1727. if (IS_INTERLACED(mb_type))
  1728. my *= 2;
  1729. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
  1730. }
  1731. } else if (IS_8X16(mb_type)) {
  1732. for (i = 0; i < 2; i++) {
  1733. int sx = mb_x * 16 + 4 + 8 * i;
  1734. int sy = mb_y * 16 + 8;
  1735. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1736. int mx = motion_val[direction][xy][0] >> shift;
  1737. int my = motion_val[direction][xy][1] >> shift;
  1738. if (IS_INTERLACED(mb_type))
  1739. my *= 2;
  1740. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
  1741. }
  1742. } else {
  1743. int sx = mb_x * 16 + 8;
  1744. int sy = mb_y * 16 + 8;
  1745. int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1746. int mx = (motion_val[direction][xy][0]>>shift) + sx;
  1747. int my = (motion_val[direction][xy][1]>>shift) + sy;
  1748. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
  1749. }
  1750. }
  1751. }
  1752. }
  1753. if (mbcount) {
  1754. AVFrameSideData *sd;
  1755. av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
  1756. sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
  1757. if (!sd) {
  1758. av_freep(&mvs);
  1759. return;
  1760. }
  1761. memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
  1762. }
  1763. av_freep(&mvs);
  1764. }
  1765. /* TODO: export all the following to make them accessible for users (and filters) */
  1766. if (avctx->hwaccel || !mbtype_table
  1767. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1768. return;
  1769. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1770. int x,y;
  1771. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1772. av_get_picture_type_char(pict->pict_type));
  1773. for (y = 0; y < mb_height; y++) {
  1774. for (x = 0; x < mb_width; x++) {
  1775. if (avctx->debug & FF_DEBUG_SKIP) {
  1776. int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
  1777. if (count > 9)
  1778. count = 9;
  1779. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1780. }
  1781. if (avctx->debug & FF_DEBUG_QP) {
  1782. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1783. qscale_table[x + y * mb_stride]);
  1784. }
  1785. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1786. int mb_type = mbtype_table[x + y * mb_stride];
  1787. // Type & MV direction
  1788. if (IS_PCM(mb_type))
  1789. av_log(avctx, AV_LOG_DEBUG, "P");
  1790. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1791. av_log(avctx, AV_LOG_DEBUG, "A");
  1792. else if (IS_INTRA4x4(mb_type))
  1793. av_log(avctx, AV_LOG_DEBUG, "i");
  1794. else if (IS_INTRA16x16(mb_type))
  1795. av_log(avctx, AV_LOG_DEBUG, "I");
  1796. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1797. av_log(avctx, AV_LOG_DEBUG, "d");
  1798. else if (IS_DIRECT(mb_type))
  1799. av_log(avctx, AV_LOG_DEBUG, "D");
  1800. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1801. av_log(avctx, AV_LOG_DEBUG, "g");
  1802. else if (IS_GMC(mb_type))
  1803. av_log(avctx, AV_LOG_DEBUG, "G");
  1804. else if (IS_SKIP(mb_type))
  1805. av_log(avctx, AV_LOG_DEBUG, "S");
  1806. else if (!USES_LIST(mb_type, 1))
  1807. av_log(avctx, AV_LOG_DEBUG, ">");
  1808. else if (!USES_LIST(mb_type, 0))
  1809. av_log(avctx, AV_LOG_DEBUG, "<");
  1810. else {
  1811. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1812. av_log(avctx, AV_LOG_DEBUG, "X");
  1813. }
  1814. // segmentation
  1815. if (IS_8X8(mb_type))
  1816. av_log(avctx, AV_LOG_DEBUG, "+");
  1817. else if (IS_16X8(mb_type))
  1818. av_log(avctx, AV_LOG_DEBUG, "-");
  1819. else if (IS_8X16(mb_type))
  1820. av_log(avctx, AV_LOG_DEBUG, "|");
  1821. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1822. av_log(avctx, AV_LOG_DEBUG, " ");
  1823. else
  1824. av_log(avctx, AV_LOG_DEBUG, "?");
  1825. if (IS_INTERLACED(mb_type))
  1826. av_log(avctx, AV_LOG_DEBUG, "=");
  1827. else
  1828. av_log(avctx, AV_LOG_DEBUG, " ");
  1829. }
  1830. }
  1831. av_log(avctx, AV_LOG_DEBUG, "\n");
  1832. }
  1833. }
  1834. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1835. (avctx->debug_mv)) {
  1836. int mb_y;
  1837. int i;
  1838. int h_chroma_shift, v_chroma_shift, block_height;
  1839. #if FF_API_VISMV
  1840. const int shift = 1 + quarter_sample;
  1841. uint8_t *ptr;
  1842. const int width = avctx->width;
  1843. const int height = avctx->height;
  1844. #endif
  1845. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1846. const int mv_stride = (mb_width << mv_sample_log2) +
  1847. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1848. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1849. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1850. av_frame_make_writable(pict);
  1851. pict->opaque = NULL;
  1852. #if FF_API_VISMV
  1853. ptr = pict->data[0];
  1854. #endif
  1855. block_height = 16 >> v_chroma_shift;
  1856. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1857. int mb_x;
  1858. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1859. const int mb_index = mb_x + mb_y * mb_stride;
  1860. #if FF_API_VISMV
  1861. if ((avctx->debug_mv) && motion_val[0]) {
  1862. int type;
  1863. for (type = 0; type < 3; type++) {
  1864. int direction = 0;
  1865. switch (type) {
  1866. case 0:
  1867. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1868. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1869. continue;
  1870. direction = 0;
  1871. break;
  1872. case 1:
  1873. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1874. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1875. continue;
  1876. direction = 0;
  1877. break;
  1878. case 2:
  1879. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1880. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1881. continue;
  1882. direction = 1;
  1883. break;
  1884. }
  1885. if (!USES_LIST(mbtype_table[mb_index], direction))
  1886. continue;
  1887. if (IS_8X8(mbtype_table[mb_index])) {
  1888. int i;
  1889. for (i = 0; i < 4; i++) {
  1890. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1891. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1892. int xy = (mb_x * 2 + (i & 1) +
  1893. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1894. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1895. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1896. draw_arrow(ptr, sx, sy, mx, my, width,
  1897. height, pict->linesize[0], 100, 0, direction);
  1898. }
  1899. } else if (IS_16X8(mbtype_table[mb_index])) {
  1900. int i;
  1901. for (i = 0; i < 2; i++) {
  1902. int sx = mb_x * 16 + 8;
  1903. int sy = mb_y * 16 + 4 + 8 * i;
  1904. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1905. int mx = (motion_val[direction][xy][0] >> shift);
  1906. int my = (motion_val[direction][xy][1] >> shift);
  1907. if (IS_INTERLACED(mbtype_table[mb_index]))
  1908. my *= 2;
  1909. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1910. height, pict->linesize[0], 100, 0, direction);
  1911. }
  1912. } else if (IS_8X16(mbtype_table[mb_index])) {
  1913. int i;
  1914. for (i = 0; i < 2; i++) {
  1915. int sx = mb_x * 16 + 4 + 8 * i;
  1916. int sy = mb_y * 16 + 8;
  1917. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1918. int mx = motion_val[direction][xy][0] >> shift;
  1919. int my = motion_val[direction][xy][1] >> shift;
  1920. if (IS_INTERLACED(mbtype_table[mb_index]))
  1921. my *= 2;
  1922. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1923. height, pict->linesize[0], 100, 0, direction);
  1924. }
  1925. } else {
  1926. int sx= mb_x * 16 + 8;
  1927. int sy= mb_y * 16 + 8;
  1928. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1929. int mx= (motion_val[direction][xy][0]>>shift) + sx;
  1930. int my= (motion_val[direction][xy][1]>>shift) + sy;
  1931. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100, 0, direction);
  1932. }
  1933. }
  1934. }
  1935. #endif
  1936. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1937. uint64_t c = (qscale_table[mb_index] * 128 / 31) *
  1938. 0x0101010101010101ULL;
  1939. int y;
  1940. for (y = 0; y < block_height; y++) {
  1941. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1942. (block_height * mb_y + y) *
  1943. pict->linesize[1]) = c;
  1944. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1945. (block_height * mb_y + y) *
  1946. pict->linesize[2]) = c;
  1947. }
  1948. }
  1949. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1950. motion_val[0]) {
  1951. int mb_type = mbtype_table[mb_index];
  1952. uint64_t u,v;
  1953. int y;
  1954. #define COLOR(theta, r) \
  1955. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1956. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1957. u = v = 128;
  1958. if (IS_PCM(mb_type)) {
  1959. COLOR(120, 48)
  1960. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1961. IS_INTRA16x16(mb_type)) {
  1962. COLOR(30, 48)
  1963. } else if (IS_INTRA4x4(mb_type)) {
  1964. COLOR(90, 48)
  1965. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1966. // COLOR(120, 48)
  1967. } else if (IS_DIRECT(mb_type)) {
  1968. COLOR(150, 48)
  1969. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1970. COLOR(170, 48)
  1971. } else if (IS_GMC(mb_type)) {
  1972. COLOR(190, 48)
  1973. } else if (IS_SKIP(mb_type)) {
  1974. // COLOR(180, 48)
  1975. } else if (!USES_LIST(mb_type, 1)) {
  1976. COLOR(240, 48)
  1977. } else if (!USES_LIST(mb_type, 0)) {
  1978. COLOR(0, 48)
  1979. } else {
  1980. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1981. COLOR(300,48)
  1982. }
  1983. u *= 0x0101010101010101ULL;
  1984. v *= 0x0101010101010101ULL;
  1985. for (y = 0; y < block_height; y++) {
  1986. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1987. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1988. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1989. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1990. }
  1991. // segmentation
  1992. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1993. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1994. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1995. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1996. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1997. }
  1998. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1999. for (y = 0; y < 16; y++)
  2000. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  2001. pict->linesize[0]] ^= 0x80;
  2002. }
  2003. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  2004. int dm = 1 << (mv_sample_log2 - 2);
  2005. for (i = 0; i < 4; i++) {
  2006. int sx = mb_x * 16 + 8 * (i & 1);
  2007. int sy = mb_y * 16 + 8 * (i >> 1);
  2008. int xy = (mb_x * 2 + (i & 1) +
  2009. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  2010. // FIXME bidir
  2011. int32_t *mv = (int32_t *) &motion_val[0][xy];
  2012. if (mv[0] != mv[dm] ||
  2013. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  2014. for (y = 0; y < 8; y++)
  2015. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  2016. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  2017. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  2018. pict->linesize[0]) ^= 0x8080808080808080ULL;
  2019. }
  2020. }
  2021. if (IS_INTERLACED(mb_type) &&
  2022. avctx->codec->id == AV_CODEC_ID_H264) {
  2023. // hmm
  2024. }
  2025. }
  2026. if (mbskip_table)
  2027. mbskip_table[mb_index] = 0;
  2028. }
  2029. }
  2030. }
  2031. }
  2032. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  2033. {
  2034. ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
  2035. p->qscale_table, p->motion_val, &s->low_delay,
  2036. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  2037. }
  2038. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  2039. {
  2040. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  2041. int offset = 2*s->mb_stride + 1;
  2042. if(!ref)
  2043. return AVERROR(ENOMEM);
  2044. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  2045. ref->size -= offset;
  2046. ref->data += offset;
  2047. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  2048. }
  2049. static inline int hpel_motion_lowres(MpegEncContext *s,
  2050. uint8_t *dest, uint8_t *src,
  2051. int field_based, int field_select,
  2052. int src_x, int src_y,
  2053. int width, int height, ptrdiff_t stride,
  2054. int h_edge_pos, int v_edge_pos,
  2055. int w, int h, h264_chroma_mc_func *pix_op,
  2056. int motion_x, int motion_y)
  2057. {
  2058. const int lowres = s->avctx->lowres;
  2059. const int op_index = FFMIN(lowres, 3);
  2060. const int s_mask = (2 << lowres) - 1;
  2061. int emu = 0;
  2062. int sx, sy;
  2063. if (s->quarter_sample) {
  2064. motion_x /= 2;
  2065. motion_y /= 2;
  2066. }
  2067. sx = motion_x & s_mask;
  2068. sy = motion_y & s_mask;
  2069. src_x += motion_x >> lowres + 1;
  2070. src_y += motion_y >> lowres + 1;
  2071. src += src_y * stride + src_x;
  2072. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  2073. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2074. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  2075. s->linesize, s->linesize,
  2076. w + 1, (h + 1) << field_based,
  2077. src_x, src_y << field_based,
  2078. h_edge_pos, v_edge_pos);
  2079. src = s->edge_emu_buffer;
  2080. emu = 1;
  2081. }
  2082. sx = (sx << 2) >> lowres;
  2083. sy = (sy << 2) >> lowres;
  2084. if (field_select)
  2085. src += s->linesize;
  2086. pix_op[op_index](dest, src, stride, h, sx, sy);
  2087. return emu;
  2088. }
  2089. /* apply one mpeg motion vector to the three components */
  2090. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  2091. uint8_t *dest_y,
  2092. uint8_t *dest_cb,
  2093. uint8_t *dest_cr,
  2094. int field_based,
  2095. int bottom_field,
  2096. int field_select,
  2097. uint8_t **ref_picture,
  2098. h264_chroma_mc_func *pix_op,
  2099. int motion_x, int motion_y,
  2100. int h, int mb_y)
  2101. {
  2102. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2103. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  2104. ptrdiff_t uvlinesize, linesize;
  2105. const int lowres = s->avctx->lowres;
  2106. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  2107. const int block_s = 8>>lowres;
  2108. const int s_mask = (2 << lowres) - 1;
  2109. const int h_edge_pos = s->h_edge_pos >> lowres;
  2110. const int v_edge_pos = s->v_edge_pos >> lowres;
  2111. linesize = s->current_picture.f->linesize[0] << field_based;
  2112. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  2113. // FIXME obviously not perfect but qpel will not work in lowres anyway
  2114. if (s->quarter_sample) {
  2115. motion_x /= 2;
  2116. motion_y /= 2;
  2117. }
  2118. if(field_based){
  2119. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  2120. }
  2121. sx = motion_x & s_mask;
  2122. sy = motion_y & s_mask;
  2123. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  2124. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  2125. if (s->out_format == FMT_H263) {
  2126. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  2127. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  2128. uvsrc_x = src_x >> 1;
  2129. uvsrc_y = src_y >> 1;
  2130. } else if (s->out_format == FMT_H261) {
  2131. // even chroma mv's are full pel in H261
  2132. mx = motion_x / 4;
  2133. my = motion_y / 4;
  2134. uvsx = (2 * mx) & s_mask;
  2135. uvsy = (2 * my) & s_mask;
  2136. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  2137. uvsrc_y = mb_y * block_s + (my >> lowres);
  2138. } else {
  2139. if(s->chroma_y_shift){
  2140. mx = motion_x / 2;
  2141. my = motion_y / 2;
  2142. uvsx = mx & s_mask;
  2143. uvsy = my & s_mask;
  2144. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  2145. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  2146. } else {
  2147. if(s->chroma_x_shift){
  2148. //Chroma422
  2149. mx = motion_x / 2;
  2150. uvsx = mx & s_mask;
  2151. uvsy = motion_y & s_mask;
  2152. uvsrc_y = src_y;
  2153. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2154. } else {
  2155. //Chroma444
  2156. uvsx = motion_x & s_mask;
  2157. uvsy = motion_y & s_mask;
  2158. uvsrc_x = src_x;
  2159. uvsrc_y = src_y;
  2160. }
  2161. }
  2162. }
  2163. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2164. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2165. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2166. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  2167. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2168. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2169. linesize >> field_based, linesize >> field_based,
  2170. 17, 17 + field_based,
  2171. src_x, src_y << field_based, h_edge_pos,
  2172. v_edge_pos);
  2173. ptr_y = s->edge_emu_buffer;
  2174. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2175. uint8_t *ubuf = s->edge_emu_buffer + 18 * s->linesize;
  2176. uint8_t *vbuf =ubuf + 9 * s->uvlinesize;
  2177. s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
  2178. uvlinesize >> field_based, uvlinesize >> field_based,
  2179. 9, 9 + field_based,
  2180. uvsrc_x, uvsrc_y << field_based,
  2181. h_edge_pos >> 1, v_edge_pos >> 1);
  2182. s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
  2183. uvlinesize >> field_based,uvlinesize >> field_based,
  2184. 9, 9 + field_based,
  2185. uvsrc_x, uvsrc_y << field_based,
  2186. h_edge_pos >> 1, v_edge_pos >> 1);
  2187. ptr_cb = ubuf;
  2188. ptr_cr = vbuf;
  2189. }
  2190. }
  2191. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
  2192. if (bottom_field) {
  2193. dest_y += s->linesize;
  2194. dest_cb += s->uvlinesize;
  2195. dest_cr += s->uvlinesize;
  2196. }
  2197. if (field_select) {
  2198. ptr_y += s->linesize;
  2199. ptr_cb += s->uvlinesize;
  2200. ptr_cr += s->uvlinesize;
  2201. }
  2202. sx = (sx << 2) >> lowres;
  2203. sy = (sy << 2) >> lowres;
  2204. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2205. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2206. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  2207. uvsx = (uvsx << 2) >> lowres;
  2208. uvsy = (uvsy << 2) >> lowres;
  2209. if (hc) {
  2210. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  2211. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  2212. }
  2213. }
  2214. // FIXME h261 lowres loop filter
  2215. }
  2216. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2217. uint8_t *dest_cb, uint8_t *dest_cr,
  2218. uint8_t **ref_picture,
  2219. h264_chroma_mc_func * pix_op,
  2220. int mx, int my)
  2221. {
  2222. const int lowres = s->avctx->lowres;
  2223. const int op_index = FFMIN(lowres, 3);
  2224. const int block_s = 8 >> lowres;
  2225. const int s_mask = (2 << lowres) - 1;
  2226. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2227. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2228. int emu = 0, src_x, src_y, sx, sy;
  2229. ptrdiff_t offset;
  2230. uint8_t *ptr;
  2231. if (s->quarter_sample) {
  2232. mx /= 2;
  2233. my /= 2;
  2234. }
  2235. /* In case of 8X8, we construct a single chroma motion vector
  2236. with a special rounding */
  2237. mx = ff_h263_round_chroma(mx);
  2238. my = ff_h263_round_chroma(my);
  2239. sx = mx & s_mask;
  2240. sy = my & s_mask;
  2241. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2242. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2243. offset = src_y * s->uvlinesize + src_x;
  2244. ptr = ref_picture[1] + offset;
  2245. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2246. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2247. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2248. s->uvlinesize, s->uvlinesize,
  2249. 9, 9,
  2250. src_x, src_y, h_edge_pos, v_edge_pos);
  2251. ptr = s->edge_emu_buffer;
  2252. emu = 1;
  2253. }
  2254. sx = (sx << 2) >> lowres;
  2255. sy = (sy << 2) >> lowres;
  2256. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2257. ptr = ref_picture[2] + offset;
  2258. if (emu) {
  2259. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2260. s->uvlinesize, s->uvlinesize,
  2261. 9, 9,
  2262. src_x, src_y, h_edge_pos, v_edge_pos);
  2263. ptr = s->edge_emu_buffer;
  2264. }
  2265. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2266. }
  2267. /**
  2268. * motion compensation of a single macroblock
  2269. * @param s context
  2270. * @param dest_y luma destination pointer
  2271. * @param dest_cb chroma cb/u destination pointer
  2272. * @param dest_cr chroma cr/v destination pointer
  2273. * @param dir direction (0->forward, 1->backward)
  2274. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2275. * @param pix_op halfpel motion compensation function (average or put normally)
  2276. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2277. */
  2278. static inline void MPV_motion_lowres(MpegEncContext *s,
  2279. uint8_t *dest_y, uint8_t *dest_cb,
  2280. uint8_t *dest_cr,
  2281. int dir, uint8_t **ref_picture,
  2282. h264_chroma_mc_func *pix_op)
  2283. {
  2284. int mx, my;
  2285. int mb_x, mb_y, i;
  2286. const int lowres = s->avctx->lowres;
  2287. const int block_s = 8 >>lowres;
  2288. mb_x = s->mb_x;
  2289. mb_y = s->mb_y;
  2290. switch (s->mv_type) {
  2291. case MV_TYPE_16X16:
  2292. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2293. 0, 0, 0,
  2294. ref_picture, pix_op,
  2295. s->mv[dir][0][0], s->mv[dir][0][1],
  2296. 2 * block_s, mb_y);
  2297. break;
  2298. case MV_TYPE_8X8:
  2299. mx = 0;
  2300. my = 0;
  2301. for (i = 0; i < 4; i++) {
  2302. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2303. s->linesize) * block_s,
  2304. ref_picture[0], 0, 0,
  2305. (2 * mb_x + (i & 1)) * block_s,
  2306. (2 * mb_y + (i >> 1)) * block_s,
  2307. s->width, s->height, s->linesize,
  2308. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2309. block_s, block_s, pix_op,
  2310. s->mv[dir][i][0], s->mv[dir][i][1]);
  2311. mx += s->mv[dir][i][0];
  2312. my += s->mv[dir][i][1];
  2313. }
  2314. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY))
  2315. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2316. pix_op, mx, my);
  2317. break;
  2318. case MV_TYPE_FIELD:
  2319. if (s->picture_structure == PICT_FRAME) {
  2320. /* top field */
  2321. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2322. 1, 0, s->field_select[dir][0],
  2323. ref_picture, pix_op,
  2324. s->mv[dir][0][0], s->mv[dir][0][1],
  2325. block_s, mb_y);
  2326. /* bottom field */
  2327. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2328. 1, 1, s->field_select[dir][1],
  2329. ref_picture, pix_op,
  2330. s->mv[dir][1][0], s->mv[dir][1][1],
  2331. block_s, mb_y);
  2332. } else {
  2333. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2334. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2335. ref_picture = s->current_picture_ptr->f->data;
  2336. }
  2337. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2338. 0, 0, s->field_select[dir][0],
  2339. ref_picture, pix_op,
  2340. s->mv[dir][0][0],
  2341. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2342. }
  2343. break;
  2344. case MV_TYPE_16X8:
  2345. for (i = 0; i < 2; i++) {
  2346. uint8_t **ref2picture;
  2347. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2348. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2349. ref2picture = ref_picture;
  2350. } else {
  2351. ref2picture = s->current_picture_ptr->f->data;
  2352. }
  2353. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2354. 0, 0, s->field_select[dir][i],
  2355. ref2picture, pix_op,
  2356. s->mv[dir][i][0], s->mv[dir][i][1] +
  2357. 2 * block_s * i, block_s, mb_y >> 1);
  2358. dest_y += 2 * block_s * s->linesize;
  2359. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2360. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2361. }
  2362. break;
  2363. case MV_TYPE_DMV:
  2364. if (s->picture_structure == PICT_FRAME) {
  2365. for (i = 0; i < 2; i++) {
  2366. int j;
  2367. for (j = 0; j < 2; j++) {
  2368. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2369. 1, j, j ^ i,
  2370. ref_picture, pix_op,
  2371. s->mv[dir][2 * i + j][0],
  2372. s->mv[dir][2 * i + j][1],
  2373. block_s, mb_y);
  2374. }
  2375. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2376. }
  2377. } else {
  2378. for (i = 0; i < 2; i++) {
  2379. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2380. 0, 0, s->picture_structure != i + 1,
  2381. ref_picture, pix_op,
  2382. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2383. 2 * block_s, mb_y >> 1);
  2384. // after put we make avg of the same block
  2385. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2386. // opposite parity is always in the same
  2387. // frame if this is second field
  2388. if (!s->first_field) {
  2389. ref_picture = s->current_picture_ptr->f->data;
  2390. }
  2391. }
  2392. }
  2393. break;
  2394. default:
  2395. av_assert2(0);
  2396. }
  2397. }
  2398. /**
  2399. * find the lowest MB row referenced in the MVs
  2400. */
  2401. int ff_mpv_lowest_referenced_row(MpegEncContext *s, int dir)
  2402. {
  2403. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2404. int my, off, i, mvs;
  2405. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2406. goto unhandled;
  2407. switch (s->mv_type) {
  2408. case MV_TYPE_16X16:
  2409. mvs = 1;
  2410. break;
  2411. case MV_TYPE_16X8:
  2412. mvs = 2;
  2413. break;
  2414. case MV_TYPE_8X8:
  2415. mvs = 4;
  2416. break;
  2417. default:
  2418. goto unhandled;
  2419. }
  2420. for (i = 0; i < mvs; i++) {
  2421. my = s->mv[dir][i][1];
  2422. my_max = FFMAX(my_max, my);
  2423. my_min = FFMIN(my_min, my);
  2424. }
  2425. off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
  2426. return av_clip(s->mb_y + off, 0, s->mb_height - 1);
  2427. unhandled:
  2428. return s->mb_height-1;
  2429. }
  2430. /* put block[] to dest[] */
  2431. static inline void put_dct(MpegEncContext *s,
  2432. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2433. {
  2434. s->dct_unquantize_intra(s, block, i, qscale);
  2435. s->idsp.idct_put(dest, line_size, block);
  2436. }
  2437. /* add block[] to dest[] */
  2438. static inline void add_dct(MpegEncContext *s,
  2439. int16_t *block, int i, uint8_t *dest, int line_size)
  2440. {
  2441. if (s->block_last_index[i] >= 0) {
  2442. s->idsp.idct_add(dest, line_size, block);
  2443. }
  2444. }
  2445. static inline void add_dequant_dct(MpegEncContext *s,
  2446. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2447. {
  2448. if (s->block_last_index[i] >= 0) {
  2449. s->dct_unquantize_inter(s, block, i, qscale);
  2450. s->idsp.idct_add(dest, line_size, block);
  2451. }
  2452. }
  2453. /**
  2454. * Clean dc, ac, coded_block for the current non-intra MB.
  2455. */
  2456. void ff_clean_intra_table_entries(MpegEncContext *s)
  2457. {
  2458. int wrap = s->b8_stride;
  2459. int xy = s->block_index[0];
  2460. s->dc_val[0][xy ] =
  2461. s->dc_val[0][xy + 1 ] =
  2462. s->dc_val[0][xy + wrap] =
  2463. s->dc_val[0][xy + 1 + wrap] = 1024;
  2464. /* ac pred */
  2465. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2466. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2467. if (s->msmpeg4_version>=3) {
  2468. s->coded_block[xy ] =
  2469. s->coded_block[xy + 1 ] =
  2470. s->coded_block[xy + wrap] =
  2471. s->coded_block[xy + 1 + wrap] = 0;
  2472. }
  2473. /* chroma */
  2474. wrap = s->mb_stride;
  2475. xy = s->mb_x + s->mb_y * wrap;
  2476. s->dc_val[1][xy] =
  2477. s->dc_val[2][xy] = 1024;
  2478. /* ac pred */
  2479. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2480. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2481. s->mbintra_table[xy]= 0;
  2482. }
  2483. /* generic function called after a macroblock has been parsed by the
  2484. decoder or after it has been encoded by the encoder.
  2485. Important variables used:
  2486. s->mb_intra : true if intra macroblock
  2487. s->mv_dir : motion vector direction
  2488. s->mv_type : motion vector type
  2489. s->mv : motion vector
  2490. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2491. */
  2492. static av_always_inline
  2493. void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2494. int lowres_flag, int is_mpeg12)
  2495. {
  2496. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2497. if (CONFIG_XVMC &&
  2498. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2499. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2500. return;
  2501. }
  2502. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2503. /* print DCT coefficients */
  2504. int i,j;
  2505. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2506. for(i=0; i<6; i++){
  2507. for(j=0; j<64; j++){
  2508. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  2509. block[i][s->idsp.idct_permutation[j]]);
  2510. }
  2511. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2512. }
  2513. }
  2514. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2515. /* update DC predictors for P macroblocks */
  2516. if (!s->mb_intra) {
  2517. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2518. if(s->mbintra_table[mb_xy])
  2519. ff_clean_intra_table_entries(s);
  2520. } else {
  2521. s->last_dc[0] =
  2522. s->last_dc[1] =
  2523. s->last_dc[2] = 128 << s->intra_dc_precision;
  2524. }
  2525. }
  2526. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2527. s->mbintra_table[mb_xy]=1;
  2528. if ((s->avctx->flags & CODEC_FLAG_PSNR) || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor ||
  2529. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  2530. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  2531. uint8_t *dest_y, *dest_cb, *dest_cr;
  2532. int dct_linesize, dct_offset;
  2533. op_pixels_func (*op_pix)[4];
  2534. qpel_mc_func (*op_qpix)[16];
  2535. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2536. const int uvlinesize = s->current_picture.f->linesize[1];
  2537. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2538. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2539. /* avoid copy if macroblock skipped in last frame too */
  2540. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2541. if(!s->encoding){
  2542. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2543. if (s->mb_skipped) {
  2544. s->mb_skipped= 0;
  2545. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2546. *mbskip_ptr = 1;
  2547. } else if(!s->current_picture.reference) {
  2548. *mbskip_ptr = 1;
  2549. } else{
  2550. *mbskip_ptr = 0; /* not skipped */
  2551. }
  2552. }
  2553. dct_linesize = linesize << s->interlaced_dct;
  2554. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2555. if(readable){
  2556. dest_y= s->dest[0];
  2557. dest_cb= s->dest[1];
  2558. dest_cr= s->dest[2];
  2559. }else{
  2560. dest_y = s->b_scratchpad;
  2561. dest_cb= s->b_scratchpad+16*linesize;
  2562. dest_cr= s->b_scratchpad+32*linesize;
  2563. }
  2564. if (!s->mb_intra) {
  2565. /* motion handling */
  2566. /* decoding or more than one mb_type (MC was already done otherwise) */
  2567. if(!s->encoding){
  2568. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2569. if (s->mv_dir & MV_DIR_FORWARD) {
  2570. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2571. ff_mpv_lowest_referenced_row(s, 0),
  2572. 0);
  2573. }
  2574. if (s->mv_dir & MV_DIR_BACKWARD) {
  2575. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2576. ff_mpv_lowest_referenced_row(s, 1),
  2577. 0);
  2578. }
  2579. }
  2580. if(lowres_flag){
  2581. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2582. if (s->mv_dir & MV_DIR_FORWARD) {
  2583. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
  2584. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2585. }
  2586. if (s->mv_dir & MV_DIR_BACKWARD) {
  2587. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
  2588. }
  2589. }else{
  2590. op_qpix = s->me.qpel_put;
  2591. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2592. op_pix = s->hdsp.put_pixels_tab;
  2593. }else{
  2594. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2595. }
  2596. if (s->mv_dir & MV_DIR_FORWARD) {
  2597. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  2598. op_pix = s->hdsp.avg_pixels_tab;
  2599. op_qpix= s->me.qpel_avg;
  2600. }
  2601. if (s->mv_dir & MV_DIR_BACKWARD) {
  2602. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  2603. }
  2604. }
  2605. }
  2606. /* skip dequant / idct if we are really late ;) */
  2607. if(s->avctx->skip_idct){
  2608. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2609. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2610. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2611. goto skip_idct;
  2612. }
  2613. /* add dct residue */
  2614. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2615. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2616. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2617. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2618. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2619. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2620. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2621. if (s->chroma_y_shift){
  2622. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2623. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2624. }else{
  2625. dct_linesize >>= 1;
  2626. dct_offset >>=1;
  2627. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2628. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2629. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2630. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2631. }
  2632. }
  2633. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2634. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2635. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2636. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2637. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2638. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2639. if(s->chroma_y_shift){//Chroma420
  2640. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2641. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2642. }else{
  2643. //chroma422
  2644. dct_linesize = uvlinesize << s->interlaced_dct;
  2645. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2646. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2647. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2648. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2649. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2650. if(!s->chroma_x_shift){//Chroma444
  2651. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2652. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2653. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2654. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2655. }
  2656. }
  2657. }//fi gray
  2658. }
  2659. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2660. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2661. }
  2662. } else {
  2663. /* dct only in intra block */
  2664. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2665. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2666. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2667. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2668. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2669. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2670. if(s->chroma_y_shift){
  2671. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2672. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2673. }else{
  2674. dct_offset >>=1;
  2675. dct_linesize >>=1;
  2676. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2677. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2678. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2679. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2680. }
  2681. }
  2682. }else{
  2683. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  2684. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2685. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  2686. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2687. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2688. if(s->chroma_y_shift){
  2689. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  2690. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  2691. }else{
  2692. dct_linesize = uvlinesize << s->interlaced_dct;
  2693. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2694. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  2695. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  2696. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2697. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2698. if(!s->chroma_x_shift){//Chroma444
  2699. s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2700. s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2701. s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2702. s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2703. }
  2704. }
  2705. }//gray
  2706. }
  2707. }
  2708. skip_idct:
  2709. if(!readable){
  2710. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2711. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2712. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2713. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2714. }
  2715. }
  2716. }
  2717. }
  2718. void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
  2719. {
  2720. #if !CONFIG_SMALL
  2721. if(s->out_format == FMT_MPEG1) {
  2722. if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 1);
  2723. else mpv_decode_mb_internal(s, block, 0, 1);
  2724. } else
  2725. #endif
  2726. if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 0);
  2727. else mpv_decode_mb_internal(s, block, 0, 0);
  2728. }
  2729. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2730. {
  2731. ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
  2732. s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
  2733. s->first_field, s->low_delay);
  2734. }
  2735. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2736. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2737. const int uvlinesize = s->current_picture.f->linesize[1];
  2738. const int mb_size= 4 - s->avctx->lowres;
  2739. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2740. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2741. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2742. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2743. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2744. 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;
  2745. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2746. s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << mb_size);
  2747. s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
  2748. s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
  2749. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2750. {
  2751. if(s->picture_structure==PICT_FRAME){
  2752. s->dest[0] += s->mb_y * linesize << mb_size;
  2753. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2754. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2755. }else{
  2756. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2757. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2758. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2759. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2760. }
  2761. }
  2762. }
  2763. /**
  2764. * Permute an 8x8 block.
  2765. * @param block the block which will be permuted according to the given permutation vector
  2766. * @param permutation the permutation vector
  2767. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2768. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2769. * (inverse) permutated to scantable order!
  2770. */
  2771. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2772. {
  2773. int i;
  2774. int16_t temp[64];
  2775. if(last<=0) return;
  2776. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2777. for(i=0; i<=last; i++){
  2778. const int j= scantable[i];
  2779. temp[j]= block[j];
  2780. block[j]=0;
  2781. }
  2782. for(i=0; i<=last; i++){
  2783. const int j= scantable[i];
  2784. const int perm_j= permutation[j];
  2785. block[perm_j]= temp[j];
  2786. }
  2787. }
  2788. void ff_mpeg_flush(AVCodecContext *avctx){
  2789. int i;
  2790. MpegEncContext *s = avctx->priv_data;
  2791. if (!s || !s->picture)
  2792. return;
  2793. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2794. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  2795. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2796. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  2797. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  2798. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  2799. s->mb_x= s->mb_y= 0;
  2800. s->closed_gop= 0;
  2801. s->parse_context.state= -1;
  2802. s->parse_context.frame_start_found= 0;
  2803. s->parse_context.overread= 0;
  2804. s->parse_context.overread_index= 0;
  2805. s->parse_context.index= 0;
  2806. s->parse_context.last_index= 0;
  2807. s->bitstream_buffer_size=0;
  2808. s->pp_time=0;
  2809. }
  2810. /**
  2811. * set qscale and update qscale dependent variables.
  2812. */
  2813. void ff_set_qscale(MpegEncContext * s, int qscale)
  2814. {
  2815. if (qscale < 1)
  2816. qscale = 1;
  2817. else if (qscale > 31)
  2818. qscale = 31;
  2819. s->qscale = qscale;
  2820. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2821. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2822. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2823. }
  2824. void ff_mpv_report_decode_progress(MpegEncContext *s)
  2825. {
  2826. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2827. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2828. }