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.

2816 lines
105KB

  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 alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  320. {
  321. return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
  322. s->chroma_x_shift, s->chroma_y_shift, s->out_format,
  323. s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
  324. &s->linesize, &s->uvlinesize);
  325. }
  326. static int init_duplicate_context(MpegEncContext *s)
  327. {
  328. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  329. int c_size = s->mb_stride * (s->mb_height + 1);
  330. int yc_size = y_size + 2 * c_size;
  331. int i;
  332. if (s->mb_height & 1)
  333. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  334. s->sc.edge_emu_buffer =
  335. s->me.scratchpad =
  336. s->me.temp =
  337. s->sc.rd_scratchpad =
  338. s->sc.b_scratchpad =
  339. s->sc.obmc_scratchpad = NULL;
  340. if (s->encoding) {
  341. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  342. ME_MAP_SIZE * sizeof(uint32_t), fail)
  343. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  344. ME_MAP_SIZE * sizeof(uint32_t), fail)
  345. if (s->avctx->noise_reduction) {
  346. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  347. 2 * 64 * sizeof(int), fail)
  348. }
  349. }
  350. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  351. s->block = s->blocks[0];
  352. for (i = 0; i < 12; i++) {
  353. s->pblocks[i] = &s->block[i];
  354. }
  355. if (s->avctx->codec_tag == AV_RL32("VCR2")) {
  356. // exchange uv
  357. FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
  358. }
  359. if (s->out_format == FMT_H263) {
  360. /* ac values */
  361. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  362. yc_size * sizeof(int16_t) * 16, fail);
  363. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  364. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  365. s->ac_val[2] = s->ac_val[1] + c_size;
  366. }
  367. return 0;
  368. fail:
  369. return -1; // free() through ff_mpv_common_end()
  370. }
  371. static void free_duplicate_context(MpegEncContext *s)
  372. {
  373. if (!s)
  374. return;
  375. av_freep(&s->sc.edge_emu_buffer);
  376. av_freep(&s->me.scratchpad);
  377. s->me.temp =
  378. s->sc.rd_scratchpad =
  379. s->sc.b_scratchpad =
  380. s->sc.obmc_scratchpad = NULL;
  381. av_freep(&s->dct_error_sum);
  382. av_freep(&s->me.map);
  383. av_freep(&s->me.score_map);
  384. av_freep(&s->blocks);
  385. av_freep(&s->ac_val_base);
  386. s->block = NULL;
  387. }
  388. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  389. {
  390. #define COPY(a) bak->a = src->a
  391. COPY(sc.edge_emu_buffer);
  392. COPY(me.scratchpad);
  393. COPY(me.temp);
  394. COPY(sc.rd_scratchpad);
  395. COPY(sc.b_scratchpad);
  396. COPY(sc.obmc_scratchpad);
  397. COPY(me.map);
  398. COPY(me.score_map);
  399. COPY(blocks);
  400. COPY(block);
  401. COPY(start_mb_y);
  402. COPY(end_mb_y);
  403. COPY(me.map_generation);
  404. COPY(pb);
  405. COPY(dct_error_sum);
  406. COPY(dct_count[0]);
  407. COPY(dct_count[1]);
  408. COPY(ac_val_base);
  409. COPY(ac_val[0]);
  410. COPY(ac_val[1]);
  411. COPY(ac_val[2]);
  412. #undef COPY
  413. }
  414. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  415. {
  416. MpegEncContext bak;
  417. int i, ret;
  418. // FIXME copy only needed parts
  419. // START_TIMER
  420. backup_duplicate_context(&bak, dst);
  421. memcpy(dst, src, sizeof(MpegEncContext));
  422. backup_duplicate_context(dst, &bak);
  423. for (i = 0; i < 12; i++) {
  424. dst->pblocks[i] = &dst->block[i];
  425. }
  426. if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
  427. // exchange uv
  428. FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
  429. }
  430. if (!dst->sc.edge_emu_buffer &&
  431. (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
  432. &dst->sc, dst->linesize)) < 0) {
  433. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  434. "scratch buffers.\n");
  435. return ret;
  436. }
  437. // STOP_TIMER("update_duplicate_context")
  438. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  439. return 0;
  440. }
  441. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  442. const AVCodecContext *src)
  443. {
  444. int i, ret;
  445. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  446. if (dst == src)
  447. return 0;
  448. av_assert0(s != s1);
  449. // FIXME can parameters change on I-frames?
  450. // in that case dst may need a reinit
  451. if (!s->context_initialized) {
  452. int err;
  453. memcpy(s, s1, sizeof(MpegEncContext));
  454. s->avctx = dst;
  455. s->bitstream_buffer = NULL;
  456. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  457. if (s1->context_initialized){
  458. // s->picture_range_start += MAX_PICTURE_COUNT;
  459. // s->picture_range_end += MAX_PICTURE_COUNT;
  460. ff_mpv_idct_init(s);
  461. if((err = ff_mpv_common_init(s)) < 0){
  462. memset(s, 0, sizeof(MpegEncContext));
  463. s->avctx = dst;
  464. return err;
  465. }
  466. }
  467. }
  468. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  469. s->context_reinit = 0;
  470. s->height = s1->height;
  471. s->width = s1->width;
  472. if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
  473. return ret;
  474. }
  475. s->avctx->coded_height = s1->avctx->coded_height;
  476. s->avctx->coded_width = s1->avctx->coded_width;
  477. s->avctx->width = s1->avctx->width;
  478. s->avctx->height = s1->avctx->height;
  479. s->coded_picture_number = s1->coded_picture_number;
  480. s->picture_number = s1->picture_number;
  481. av_assert0(!s->picture || s->picture != s1->picture);
  482. if(s->picture)
  483. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  484. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  485. if (s1->picture[i].f->buf[0] &&
  486. (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
  487. return ret;
  488. }
  489. #define UPDATE_PICTURE(pic)\
  490. do {\
  491. ff_mpeg_unref_picture(s->avctx, &s->pic);\
  492. if (s1->pic.f && s1->pic.f->buf[0])\
  493. ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
  494. else\
  495. ret = ff_update_picture_tables(&s->pic, &s1->pic);\
  496. if (ret < 0)\
  497. return ret;\
  498. } while (0)
  499. UPDATE_PICTURE(current_picture);
  500. UPDATE_PICTURE(last_picture);
  501. UPDATE_PICTURE(next_picture);
  502. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  503. ((pic && pic >= old_ctx->picture && \
  504. pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
  505. &new_ctx->picture[pic - old_ctx->picture] : NULL)
  506. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  507. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  508. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  509. // Error/bug resilience
  510. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  511. s->workaround_bugs = s1->workaround_bugs;
  512. s->padding_bug_score = s1->padding_bug_score;
  513. // MPEG4 timing info
  514. memcpy(&s->last_time_base, &s1->last_time_base,
  515. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  516. (char *) &s1->last_time_base);
  517. // B-frame info
  518. s->max_b_frames = s1->max_b_frames;
  519. s->low_delay = s1->low_delay;
  520. s->droppable = s1->droppable;
  521. // DivX handling (doesn't work)
  522. s->divx_packed = s1->divx_packed;
  523. if (s1->bitstream_buffer) {
  524. if (s1->bitstream_buffer_size +
  525. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) {
  526. av_fast_malloc(&s->bitstream_buffer,
  527. &s->allocated_bitstream_buffer_size,
  528. s1->allocated_bitstream_buffer_size);
  529. if (!s->bitstream_buffer) {
  530. s->bitstream_buffer_size = 0;
  531. return AVERROR(ENOMEM);
  532. }
  533. }
  534. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  535. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  536. s1->bitstream_buffer_size);
  537. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  538. FF_INPUT_BUFFER_PADDING_SIZE);
  539. }
  540. // linesize dependend scratch buffer allocation
  541. if (!s->sc.edge_emu_buffer)
  542. if (s1->linesize) {
  543. if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
  544. &s->sc, s1->linesize) < 0) {
  545. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  546. "scratch buffers.\n");
  547. return AVERROR(ENOMEM);
  548. }
  549. } else {
  550. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  551. "be allocated due to unknown size.\n");
  552. }
  553. // MPEG2/interlacing info
  554. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  555. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  556. if (!s1->first_field) {
  557. s->last_pict_type = s1->pict_type;
  558. if (s1->current_picture_ptr)
  559. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  560. }
  561. return 0;
  562. }
  563. /**
  564. * Set the given MpegEncContext to common defaults
  565. * (same for encoding and decoding).
  566. * The changed fields will not depend upon the
  567. * prior state of the MpegEncContext.
  568. */
  569. void ff_mpv_common_defaults(MpegEncContext *s)
  570. {
  571. s->y_dc_scale_table =
  572. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  573. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  574. s->progressive_frame = 1;
  575. s->progressive_sequence = 1;
  576. s->picture_structure = PICT_FRAME;
  577. s->coded_picture_number = 0;
  578. s->picture_number = 0;
  579. s->f_code = 1;
  580. s->b_code = 1;
  581. s->slice_context_count = 1;
  582. }
  583. /**
  584. * Set the given MpegEncContext to defaults for decoding.
  585. * the changed fields will not depend upon
  586. * the prior state of the MpegEncContext.
  587. */
  588. void ff_mpv_decode_defaults(MpegEncContext *s)
  589. {
  590. ff_mpv_common_defaults(s);
  591. }
  592. void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
  593. {
  594. s->avctx = avctx;
  595. s->width = avctx->coded_width;
  596. s->height = avctx->coded_height;
  597. s->codec_id = avctx->codec->id;
  598. s->workaround_bugs = avctx->workaround_bugs;
  599. /* convert fourcc to upper case */
  600. s->codec_tag = avpriv_toupper4(avctx->codec_tag);
  601. }
  602. static int init_er(MpegEncContext *s)
  603. {
  604. ERContext *er = &s->er;
  605. int mb_array_size = s->mb_height * s->mb_stride;
  606. int i;
  607. er->avctx = s->avctx;
  608. er->mb_index2xy = s->mb_index2xy;
  609. er->mb_num = s->mb_num;
  610. er->mb_width = s->mb_width;
  611. er->mb_height = s->mb_height;
  612. er->mb_stride = s->mb_stride;
  613. er->b8_stride = s->b8_stride;
  614. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  615. er->error_status_table = av_mallocz(mb_array_size);
  616. if (!er->er_temp_buffer || !er->error_status_table)
  617. goto fail;
  618. er->mbskip_table = s->mbskip_table;
  619. er->mbintra_table = s->mbintra_table;
  620. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  621. er->dc_val[i] = s->dc_val[i];
  622. er->decode_mb = mpeg_er_decode_mb;
  623. er->opaque = s;
  624. return 0;
  625. fail:
  626. av_freep(&er->er_temp_buffer);
  627. av_freep(&er->error_status_table);
  628. return AVERROR(ENOMEM);
  629. }
  630. /**
  631. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  632. */
  633. static int init_context_frame(MpegEncContext *s)
  634. {
  635. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  636. s->mb_width = (s->width + 15) / 16;
  637. s->mb_stride = s->mb_width + 1;
  638. s->b8_stride = s->mb_width * 2 + 1;
  639. mb_array_size = s->mb_height * s->mb_stride;
  640. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  641. /* set default edge pos, will be overridden
  642. * in decode_header if needed */
  643. s->h_edge_pos = s->mb_width * 16;
  644. s->v_edge_pos = s->mb_height * 16;
  645. s->mb_num = s->mb_width * s->mb_height;
  646. s->block_wrap[0] =
  647. s->block_wrap[1] =
  648. s->block_wrap[2] =
  649. s->block_wrap[3] = s->b8_stride;
  650. s->block_wrap[4] =
  651. s->block_wrap[5] = s->mb_stride;
  652. y_size = s->b8_stride * (2 * s->mb_height + 1);
  653. c_size = s->mb_stride * (s->mb_height + 1);
  654. yc_size = y_size + 2 * c_size;
  655. if (s->mb_height & 1)
  656. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  657. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
  658. for (y = 0; y < s->mb_height; y++)
  659. for (x = 0; x < s->mb_width; x++)
  660. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  661. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  662. if (s->encoding) {
  663. /* Allocate MV tables */
  664. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  665. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  666. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  667. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  668. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  669. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  670. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  671. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  672. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  673. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  674. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  675. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  676. /* Allocate MB type table */
  677. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  678. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  679. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  680. mb_array_size * sizeof(float), fail);
  681. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  682. mb_array_size * sizeof(float), fail);
  683. }
  684. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  685. (s->avctx->flags & CODEC_FLAG_INTERLACED_ME)) {
  686. /* interlaced direct mode decoding tables */
  687. for (i = 0; i < 2; i++) {
  688. int j, k;
  689. for (j = 0; j < 2; j++) {
  690. for (k = 0; k < 2; k++) {
  691. FF_ALLOCZ_OR_GOTO(s->avctx,
  692. s->b_field_mv_table_base[i][j][k],
  693. mv_table_size * 2 * sizeof(int16_t),
  694. fail);
  695. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  696. s->mb_stride + 1;
  697. }
  698. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  699. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  700. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  701. }
  702. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  703. }
  704. }
  705. if (s->out_format == FMT_H263) {
  706. /* cbp values */
  707. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  708. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  709. /* cbp, ac_pred, pred_dir */
  710. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  711. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  712. }
  713. if (s->h263_pred || s->h263_plus || !s->encoding) {
  714. /* dc values */
  715. // MN: we need these for error resilience of intra-frames
  716. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  717. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  718. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  719. s->dc_val[2] = s->dc_val[1] + c_size;
  720. for (i = 0; i < yc_size; i++)
  721. s->dc_val_base[i] = 1024;
  722. }
  723. /* which mb is a intra block */
  724. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  725. memset(s->mbintra_table, 1, mb_array_size);
  726. /* init macroblock skip table */
  727. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  728. // Note the + 1 is for a quicker mpeg4 slice_end detection
  729. return init_er(s);
  730. fail:
  731. return AVERROR(ENOMEM);
  732. }
  733. /**
  734. * init common structure for both encoder and decoder.
  735. * this assumes that some variables like width/height are already set
  736. */
  737. av_cold int ff_mpv_common_init(MpegEncContext *s)
  738. {
  739. int i;
  740. int nb_slices = (HAVE_THREADS &&
  741. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  742. s->avctx->thread_count : 1;
  743. if (s->encoding && s->avctx->slices)
  744. nb_slices = s->avctx->slices;
  745. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  746. s->mb_height = (s->height + 31) / 32 * 2;
  747. else
  748. s->mb_height = (s->height + 15) / 16;
  749. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  750. av_log(s->avctx, AV_LOG_ERROR,
  751. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  752. return -1;
  753. }
  754. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  755. int max_slices;
  756. if (s->mb_height)
  757. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  758. else
  759. max_slices = MAX_THREADS;
  760. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  761. " reducing to %d\n", nb_slices, max_slices);
  762. nb_slices = max_slices;
  763. }
  764. if ((s->width || s->height) &&
  765. av_image_check_size(s->width, s->height, 0, s->avctx))
  766. return -1;
  767. dct_init(s);
  768. /* set chroma shifts */
  769. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  770. &s->chroma_x_shift,
  771. &s->chroma_y_shift);
  772. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  773. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  774. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  775. s->picture[i].f = av_frame_alloc();
  776. if (!s->picture[i].f)
  777. goto fail;
  778. }
  779. memset(&s->next_picture, 0, sizeof(s->next_picture));
  780. memset(&s->last_picture, 0, sizeof(s->last_picture));
  781. memset(&s->current_picture, 0, sizeof(s->current_picture));
  782. memset(&s->new_picture, 0, sizeof(s->new_picture));
  783. s->next_picture.f = av_frame_alloc();
  784. if (!s->next_picture.f)
  785. goto fail;
  786. s->last_picture.f = av_frame_alloc();
  787. if (!s->last_picture.f)
  788. goto fail;
  789. s->current_picture.f = av_frame_alloc();
  790. if (!s->current_picture.f)
  791. goto fail;
  792. s->new_picture.f = av_frame_alloc();
  793. if (!s->new_picture.f)
  794. goto fail;
  795. if (init_context_frame(s))
  796. goto fail;
  797. s->parse_context.state = -1;
  798. s->context_initialized = 1;
  799. memset(s->thread_context, 0, sizeof(s->thread_context));
  800. s->thread_context[0] = s;
  801. // if (s->width && s->height) {
  802. if (nb_slices > 1) {
  803. for (i = 0; i < nb_slices; i++) {
  804. if (i) {
  805. s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
  806. if (!s->thread_context[i])
  807. goto fail;
  808. }
  809. if (init_duplicate_context(s->thread_context[i]) < 0)
  810. goto fail;
  811. s->thread_context[i]->start_mb_y =
  812. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  813. s->thread_context[i]->end_mb_y =
  814. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  815. }
  816. } else {
  817. if (init_duplicate_context(s) < 0)
  818. goto fail;
  819. s->start_mb_y = 0;
  820. s->end_mb_y = s->mb_height;
  821. }
  822. s->slice_context_count = nb_slices;
  823. // }
  824. return 0;
  825. fail:
  826. ff_mpv_common_end(s);
  827. return -1;
  828. }
  829. /**
  830. * Frees and resets MpegEncContext fields depending on the resolution.
  831. * Is used during resolution changes to avoid a full reinitialization of the
  832. * codec.
  833. */
  834. static void free_context_frame(MpegEncContext *s)
  835. {
  836. int i, j, k;
  837. av_freep(&s->mb_type);
  838. av_freep(&s->p_mv_table_base);
  839. av_freep(&s->b_forw_mv_table_base);
  840. av_freep(&s->b_back_mv_table_base);
  841. av_freep(&s->b_bidir_forw_mv_table_base);
  842. av_freep(&s->b_bidir_back_mv_table_base);
  843. av_freep(&s->b_direct_mv_table_base);
  844. s->p_mv_table = NULL;
  845. s->b_forw_mv_table = NULL;
  846. s->b_back_mv_table = NULL;
  847. s->b_bidir_forw_mv_table = NULL;
  848. s->b_bidir_back_mv_table = NULL;
  849. s->b_direct_mv_table = NULL;
  850. for (i = 0; i < 2; i++) {
  851. for (j = 0; j < 2; j++) {
  852. for (k = 0; k < 2; k++) {
  853. av_freep(&s->b_field_mv_table_base[i][j][k]);
  854. s->b_field_mv_table[i][j][k] = NULL;
  855. }
  856. av_freep(&s->b_field_select_table[i][j]);
  857. av_freep(&s->p_field_mv_table_base[i][j]);
  858. s->p_field_mv_table[i][j] = NULL;
  859. }
  860. av_freep(&s->p_field_select_table[i]);
  861. }
  862. av_freep(&s->dc_val_base);
  863. av_freep(&s->coded_block_base);
  864. av_freep(&s->mbintra_table);
  865. av_freep(&s->cbp_table);
  866. av_freep(&s->pred_dir_table);
  867. av_freep(&s->mbskip_table);
  868. av_freep(&s->er.error_status_table);
  869. av_freep(&s->er.er_temp_buffer);
  870. av_freep(&s->mb_index2xy);
  871. av_freep(&s->lambda_table);
  872. av_freep(&s->cplx_tab);
  873. av_freep(&s->bits_tab);
  874. s->linesize = s->uvlinesize = 0;
  875. }
  876. int ff_mpv_common_frame_size_change(MpegEncContext *s)
  877. {
  878. int i, err = 0;
  879. if (!s->context_initialized)
  880. return AVERROR(EINVAL);
  881. if (s->slice_context_count > 1) {
  882. for (i = 0; i < s->slice_context_count; i++) {
  883. free_duplicate_context(s->thread_context[i]);
  884. }
  885. for (i = 1; i < s->slice_context_count; i++) {
  886. av_freep(&s->thread_context[i]);
  887. }
  888. } else
  889. free_duplicate_context(s);
  890. free_context_frame(s);
  891. if (s->picture)
  892. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  893. s->picture[i].needs_realloc = 1;
  894. }
  895. s->last_picture_ptr =
  896. s->next_picture_ptr =
  897. s->current_picture_ptr = NULL;
  898. // init
  899. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  900. s->mb_height = (s->height + 31) / 32 * 2;
  901. else
  902. s->mb_height = (s->height + 15) / 16;
  903. if ((s->width || s->height) &&
  904. (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  905. goto fail;
  906. if ((err = init_context_frame(s)))
  907. goto fail;
  908. memset(s->thread_context, 0, sizeof(s->thread_context));
  909. s->thread_context[0] = s;
  910. if (s->width && s->height) {
  911. int nb_slices = s->slice_context_count;
  912. if (nb_slices > 1) {
  913. for (i = 0; i < nb_slices; i++) {
  914. if (i) {
  915. s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
  916. if (!s->thread_context[i]) {
  917. err = AVERROR(ENOMEM);
  918. goto fail;
  919. }
  920. }
  921. if ((err = init_duplicate_context(s->thread_context[i])) < 0)
  922. goto fail;
  923. s->thread_context[i]->start_mb_y =
  924. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  925. s->thread_context[i]->end_mb_y =
  926. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  927. }
  928. } else {
  929. err = init_duplicate_context(s);
  930. if (err < 0)
  931. goto fail;
  932. s->start_mb_y = 0;
  933. s->end_mb_y = s->mb_height;
  934. }
  935. s->slice_context_count = nb_slices;
  936. }
  937. return 0;
  938. fail:
  939. ff_mpv_common_end(s);
  940. return err;
  941. }
  942. /* init common structure for both encoder and decoder */
  943. void ff_mpv_common_end(MpegEncContext *s)
  944. {
  945. int i;
  946. if (s->slice_context_count > 1) {
  947. for (i = 0; i < s->slice_context_count; i++) {
  948. free_duplicate_context(s->thread_context[i]);
  949. }
  950. for (i = 1; i < s->slice_context_count; i++) {
  951. av_freep(&s->thread_context[i]);
  952. }
  953. s->slice_context_count = 1;
  954. } else free_duplicate_context(s);
  955. av_freep(&s->parse_context.buffer);
  956. s->parse_context.buffer_size = 0;
  957. av_freep(&s->bitstream_buffer);
  958. s->allocated_bitstream_buffer_size = 0;
  959. if (s->picture) {
  960. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  961. ff_free_picture_tables(&s->picture[i]);
  962. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  963. av_frame_free(&s->picture[i].f);
  964. }
  965. }
  966. av_freep(&s->picture);
  967. ff_free_picture_tables(&s->last_picture);
  968. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  969. av_frame_free(&s->last_picture.f);
  970. ff_free_picture_tables(&s->current_picture);
  971. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  972. av_frame_free(&s->current_picture.f);
  973. ff_free_picture_tables(&s->next_picture);
  974. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  975. av_frame_free(&s->next_picture.f);
  976. ff_free_picture_tables(&s->new_picture);
  977. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  978. av_frame_free(&s->new_picture.f);
  979. free_context_frame(s);
  980. s->context_initialized = 0;
  981. s->last_picture_ptr =
  982. s->next_picture_ptr =
  983. s->current_picture_ptr = NULL;
  984. s->linesize = s->uvlinesize = 0;
  985. }
  986. static void gray_frame(AVFrame *frame)
  987. {
  988. int i, h_chroma_shift, v_chroma_shift;
  989. av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
  990. for(i=0; i<frame->height; i++)
  991. memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
  992. for(i=0; i<FF_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
  993. memset(frame->data[1] + frame->linesize[1]*i,
  994. 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
  995. memset(frame->data[2] + frame->linesize[2]*i,
  996. 0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
  997. }
  998. }
  999. /**
  1000. * generic function called after decoding
  1001. * the header and before a frame is decoded.
  1002. */
  1003. int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1004. {
  1005. int i, ret;
  1006. Picture *pic;
  1007. s->mb_skipped = 0;
  1008. if (!ff_thread_can_start_frame(avctx)) {
  1009. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1010. return -1;
  1011. }
  1012. /* mark & release old frames */
  1013. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1014. s->last_picture_ptr != s->next_picture_ptr &&
  1015. s->last_picture_ptr->f->buf[0]) {
  1016. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1017. }
  1018. /* release forgotten pictures */
  1019. /* if (mpeg124/h263) */
  1020. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1021. if (&s->picture[i] != s->last_picture_ptr &&
  1022. &s->picture[i] != s->next_picture_ptr &&
  1023. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1024. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1025. av_log(avctx, AV_LOG_ERROR,
  1026. "releasing zombie picture\n");
  1027. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1028. }
  1029. }
  1030. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1031. /* release non reference frames */
  1032. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1033. if (!s->picture[i].reference)
  1034. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1035. }
  1036. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  1037. // we already have a unused image
  1038. // (maybe it was set before reading the header)
  1039. pic = s->current_picture_ptr;
  1040. } else {
  1041. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1042. if (i < 0) {
  1043. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1044. return i;
  1045. }
  1046. pic = &s->picture[i];
  1047. }
  1048. pic->reference = 0;
  1049. if (!s->droppable) {
  1050. if (s->pict_type != AV_PICTURE_TYPE_B)
  1051. pic->reference = 3;
  1052. }
  1053. pic->f->coded_picture_number = s->coded_picture_number++;
  1054. if (alloc_picture(s, pic, 0) < 0)
  1055. return -1;
  1056. s->current_picture_ptr = pic;
  1057. // FIXME use only the vars from current_pic
  1058. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1059. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1060. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1061. if (s->picture_structure != PICT_FRAME)
  1062. s->current_picture_ptr->f->top_field_first =
  1063. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1064. }
  1065. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1066. !s->progressive_sequence;
  1067. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1068. s->current_picture_ptr->f->pict_type = s->pict_type;
  1069. // if (s->avctx->flags && CODEC_FLAG_QSCALE)
  1070. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1071. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1072. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1073. s->current_picture_ptr)) < 0)
  1074. return ret;
  1075. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1076. s->last_picture_ptr = s->next_picture_ptr;
  1077. if (!s->droppable)
  1078. s->next_picture_ptr = s->current_picture_ptr;
  1079. }
  1080. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1081. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1082. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1083. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1084. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1085. s->pict_type, s->droppable);
  1086. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  1087. (s->pict_type != AV_PICTURE_TYPE_I ||
  1088. s->picture_structure != PICT_FRAME)) {
  1089. int h_chroma_shift, v_chroma_shift;
  1090. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1091. &h_chroma_shift, &v_chroma_shift);
  1092. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
  1093. av_log(avctx, AV_LOG_DEBUG,
  1094. "allocating dummy last picture for B frame\n");
  1095. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1096. av_log(avctx, AV_LOG_ERROR,
  1097. "warning: first frame is no keyframe\n");
  1098. else if (s->picture_structure != PICT_FRAME)
  1099. av_log(avctx, AV_LOG_DEBUG,
  1100. "allocate dummy last picture for field based first keyframe\n");
  1101. /* Allocate a dummy frame */
  1102. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1103. if (i < 0) {
  1104. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1105. return i;
  1106. }
  1107. s->last_picture_ptr = &s->picture[i];
  1108. s->last_picture_ptr->reference = 3;
  1109. s->last_picture_ptr->f->key_frame = 0;
  1110. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1111. if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1112. s->last_picture_ptr = NULL;
  1113. return -1;
  1114. }
  1115. if (!avctx->hwaccel && !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
  1116. for(i=0; i<avctx->height; i++)
  1117. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
  1118. 0x80, avctx->width);
  1119. if (s->last_picture_ptr->f->data[2]) {
  1120. for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1121. memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
  1122. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1123. memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
  1124. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1125. }
  1126. }
  1127. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1128. for(i=0; i<avctx->height; i++)
  1129. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
  1130. }
  1131. }
  1132. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1133. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1134. }
  1135. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1136. s->pict_type == AV_PICTURE_TYPE_B) {
  1137. /* Allocate a dummy frame */
  1138. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1139. if (i < 0) {
  1140. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1141. return i;
  1142. }
  1143. s->next_picture_ptr = &s->picture[i];
  1144. s->next_picture_ptr->reference = 3;
  1145. s->next_picture_ptr->f->key_frame = 0;
  1146. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1147. if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1148. s->next_picture_ptr = NULL;
  1149. return -1;
  1150. }
  1151. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1152. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1153. }
  1154. #if 0 // BUFREF-FIXME
  1155. memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
  1156. memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
  1157. #endif
  1158. if (s->last_picture_ptr) {
  1159. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1160. if (s->last_picture_ptr->f->buf[0] &&
  1161. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1162. s->last_picture_ptr)) < 0)
  1163. return ret;
  1164. }
  1165. if (s->next_picture_ptr) {
  1166. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1167. if (s->next_picture_ptr->f->buf[0] &&
  1168. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1169. s->next_picture_ptr)) < 0)
  1170. return ret;
  1171. }
  1172. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1173. s->last_picture_ptr->f->buf[0]));
  1174. if (s->picture_structure!= PICT_FRAME) {
  1175. int i;
  1176. for (i = 0; i < 4; i++) {
  1177. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1178. s->current_picture.f->data[i] +=
  1179. s->current_picture.f->linesize[i];
  1180. }
  1181. s->current_picture.f->linesize[i] *= 2;
  1182. s->last_picture.f->linesize[i] *= 2;
  1183. s->next_picture.f->linesize[i] *= 2;
  1184. }
  1185. }
  1186. /* set dequantizer, we can't do it during init as
  1187. * it might change for mpeg4 and we can't do it in the header
  1188. * decode as init is not called for mpeg4 there yet */
  1189. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1190. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1191. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1192. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1193. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1194. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1195. } else {
  1196. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1197. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1198. }
  1199. if (s->avctx->debug & FF_DEBUG_NOMC) {
  1200. gray_frame(s->current_picture_ptr->f);
  1201. }
  1202. return 0;
  1203. }
  1204. /* called after a frame has been decoded. */
  1205. void ff_mpv_frame_end(MpegEncContext *s)
  1206. {
  1207. emms_c();
  1208. if (s->current_picture.reference)
  1209. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1210. }
  1211. #if FF_API_VISMV
  1212. static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
  1213. {
  1214. if(*sx > *ex)
  1215. return clip_line(ex, ey, sx, sy, maxx);
  1216. if (*sx < 0) {
  1217. if (*ex < 0)
  1218. return 1;
  1219. *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
  1220. *sx = 0;
  1221. }
  1222. if (*ex > maxx) {
  1223. if (*sx > maxx)
  1224. return 1;
  1225. *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
  1226. *ex = maxx;
  1227. }
  1228. return 0;
  1229. }
  1230. /**
  1231. * Draw a line from (ex, ey) -> (sx, sy).
  1232. * @param w width of the image
  1233. * @param h height of the image
  1234. * @param stride stride/linesize of the image
  1235. * @param color color of the arrow
  1236. */
  1237. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1238. int w, int h, int stride, int color)
  1239. {
  1240. int x, y, fr, f;
  1241. if (clip_line(&sx, &sy, &ex, &ey, w - 1))
  1242. return;
  1243. if (clip_line(&sy, &sx, &ey, &ex, h - 1))
  1244. return;
  1245. sx = av_clip(sx, 0, w - 1);
  1246. sy = av_clip(sy, 0, h - 1);
  1247. ex = av_clip(ex, 0, w - 1);
  1248. ey = av_clip(ey, 0, h - 1);
  1249. buf[sy * stride + sx] += color;
  1250. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1251. if (sx > ex) {
  1252. FFSWAP(int, sx, ex);
  1253. FFSWAP(int, sy, ey);
  1254. }
  1255. buf += sx + sy * stride;
  1256. ex -= sx;
  1257. f = ((ey - sy) << 16) / ex;
  1258. for (x = 0; x <= ex; x++) {
  1259. y = (x * f) >> 16;
  1260. fr = (x * f) & 0xFFFF;
  1261. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1262. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1263. }
  1264. } else {
  1265. if (sy > ey) {
  1266. FFSWAP(int, sx, ex);
  1267. FFSWAP(int, sy, ey);
  1268. }
  1269. buf += sx + sy * stride;
  1270. ey -= sy;
  1271. if (ey)
  1272. f = ((ex - sx) << 16) / ey;
  1273. else
  1274. f = 0;
  1275. for(y= 0; y <= ey; y++){
  1276. x = (y*f) >> 16;
  1277. fr = (y*f) & 0xFFFF;
  1278. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1279. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1280. }
  1281. }
  1282. }
  1283. /**
  1284. * Draw an arrow from (ex, ey) -> (sx, sy).
  1285. * @param w width of the image
  1286. * @param h height of the image
  1287. * @param stride stride/linesize of the image
  1288. * @param color color of the arrow
  1289. */
  1290. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1291. int ey, int w, int h, int stride, int color, int tail, int direction)
  1292. {
  1293. int dx,dy;
  1294. if (direction) {
  1295. FFSWAP(int, sx, ex);
  1296. FFSWAP(int, sy, ey);
  1297. }
  1298. sx = av_clip(sx, -100, w + 100);
  1299. sy = av_clip(sy, -100, h + 100);
  1300. ex = av_clip(ex, -100, w + 100);
  1301. ey = av_clip(ey, -100, h + 100);
  1302. dx = ex - sx;
  1303. dy = ey - sy;
  1304. if (dx * dx + dy * dy > 3 * 3) {
  1305. int rx = dx + dy;
  1306. int ry = -dx + dy;
  1307. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1308. // FIXME subpixel accuracy
  1309. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1310. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1311. if (tail) {
  1312. rx = -rx;
  1313. ry = -ry;
  1314. }
  1315. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1316. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1317. }
  1318. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1319. }
  1320. #endif
  1321. static int add_mb(AVMotionVector *mb, uint32_t mb_type,
  1322. int dst_x, int dst_y,
  1323. int src_x, int src_y,
  1324. int direction)
  1325. {
  1326. mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
  1327. mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
  1328. mb->src_x = src_x;
  1329. mb->src_y = src_y;
  1330. mb->dst_x = dst_x;
  1331. mb->dst_y = dst_y;
  1332. mb->source = direction ? 1 : -1;
  1333. mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
  1334. return 1;
  1335. }
  1336. /**
  1337. * Print debugging info for the given picture.
  1338. */
  1339. void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
  1340. uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
  1341. int *low_delay,
  1342. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1343. {
  1344. if ((avctx->flags2 & CODEC_FLAG2_EXPORT_MVS) && mbtype_table && motion_val[0]) {
  1345. const int shift = 1 + quarter_sample;
  1346. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1347. const int mv_stride = (mb_width << mv_sample_log2) +
  1348. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1349. int mb_x, mb_y, mbcount = 0;
  1350. /* size is width * height * 2 * 4 where 2 is for directions and 4 is
  1351. * for the maximum number of MB (4 MB in case of IS_8x8) */
  1352. AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
  1353. if (!mvs)
  1354. return;
  1355. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1356. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1357. int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
  1358. for (direction = 0; direction < 2; direction++) {
  1359. if (!USES_LIST(mb_type, direction))
  1360. continue;
  1361. if (IS_8X8(mb_type)) {
  1362. for (i = 0; i < 4; i++) {
  1363. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1364. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1365. int xy = (mb_x * 2 + (i & 1) +
  1366. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1367. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1368. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1369. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
  1370. }
  1371. } else if (IS_16X8(mb_type)) {
  1372. for (i = 0; i < 2; i++) {
  1373. int sx = mb_x * 16 + 8;
  1374. int sy = mb_y * 16 + 4 + 8 * i;
  1375. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1376. int mx = (motion_val[direction][xy][0] >> shift);
  1377. int my = (motion_val[direction][xy][1] >> shift);
  1378. if (IS_INTERLACED(mb_type))
  1379. my *= 2;
  1380. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
  1381. }
  1382. } else if (IS_8X16(mb_type)) {
  1383. for (i = 0; i < 2; i++) {
  1384. int sx = mb_x * 16 + 4 + 8 * i;
  1385. int sy = mb_y * 16 + 8;
  1386. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1387. int mx = motion_val[direction][xy][0] >> shift;
  1388. int my = motion_val[direction][xy][1] >> shift;
  1389. if (IS_INTERLACED(mb_type))
  1390. my *= 2;
  1391. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
  1392. }
  1393. } else {
  1394. int sx = mb_x * 16 + 8;
  1395. int sy = mb_y * 16 + 8;
  1396. int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1397. int mx = (motion_val[direction][xy][0]>>shift) + sx;
  1398. int my = (motion_val[direction][xy][1]>>shift) + sy;
  1399. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
  1400. }
  1401. }
  1402. }
  1403. }
  1404. if (mbcount) {
  1405. AVFrameSideData *sd;
  1406. av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
  1407. sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
  1408. if (!sd) {
  1409. av_freep(&mvs);
  1410. return;
  1411. }
  1412. memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
  1413. }
  1414. av_freep(&mvs);
  1415. }
  1416. /* TODO: export all the following to make them accessible for users (and filters) */
  1417. if (avctx->hwaccel || !mbtype_table
  1418. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1419. return;
  1420. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1421. int x,y;
  1422. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1423. av_get_picture_type_char(pict->pict_type));
  1424. for (y = 0; y < mb_height; y++) {
  1425. for (x = 0; x < mb_width; x++) {
  1426. if (avctx->debug & FF_DEBUG_SKIP) {
  1427. int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
  1428. if (count > 9)
  1429. count = 9;
  1430. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1431. }
  1432. if (avctx->debug & FF_DEBUG_QP) {
  1433. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1434. qscale_table[x + y * mb_stride]);
  1435. }
  1436. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1437. int mb_type = mbtype_table[x + y * mb_stride];
  1438. // Type & MV direction
  1439. if (IS_PCM(mb_type))
  1440. av_log(avctx, AV_LOG_DEBUG, "P");
  1441. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1442. av_log(avctx, AV_LOG_DEBUG, "A");
  1443. else if (IS_INTRA4x4(mb_type))
  1444. av_log(avctx, AV_LOG_DEBUG, "i");
  1445. else if (IS_INTRA16x16(mb_type))
  1446. av_log(avctx, AV_LOG_DEBUG, "I");
  1447. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1448. av_log(avctx, AV_LOG_DEBUG, "d");
  1449. else if (IS_DIRECT(mb_type))
  1450. av_log(avctx, AV_LOG_DEBUG, "D");
  1451. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1452. av_log(avctx, AV_LOG_DEBUG, "g");
  1453. else if (IS_GMC(mb_type))
  1454. av_log(avctx, AV_LOG_DEBUG, "G");
  1455. else if (IS_SKIP(mb_type))
  1456. av_log(avctx, AV_LOG_DEBUG, "S");
  1457. else if (!USES_LIST(mb_type, 1))
  1458. av_log(avctx, AV_LOG_DEBUG, ">");
  1459. else if (!USES_LIST(mb_type, 0))
  1460. av_log(avctx, AV_LOG_DEBUG, "<");
  1461. else {
  1462. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1463. av_log(avctx, AV_LOG_DEBUG, "X");
  1464. }
  1465. // segmentation
  1466. if (IS_8X8(mb_type))
  1467. av_log(avctx, AV_LOG_DEBUG, "+");
  1468. else if (IS_16X8(mb_type))
  1469. av_log(avctx, AV_LOG_DEBUG, "-");
  1470. else if (IS_8X16(mb_type))
  1471. av_log(avctx, AV_LOG_DEBUG, "|");
  1472. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1473. av_log(avctx, AV_LOG_DEBUG, " ");
  1474. else
  1475. av_log(avctx, AV_LOG_DEBUG, "?");
  1476. if (IS_INTERLACED(mb_type))
  1477. av_log(avctx, AV_LOG_DEBUG, "=");
  1478. else
  1479. av_log(avctx, AV_LOG_DEBUG, " ");
  1480. }
  1481. }
  1482. av_log(avctx, AV_LOG_DEBUG, "\n");
  1483. }
  1484. }
  1485. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1486. (avctx->debug_mv)) {
  1487. int mb_y;
  1488. int i;
  1489. int h_chroma_shift, v_chroma_shift, block_height;
  1490. #if FF_API_VISMV
  1491. const int shift = 1 + quarter_sample;
  1492. uint8_t *ptr;
  1493. const int width = avctx->width;
  1494. const int height = avctx->height;
  1495. #endif
  1496. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1497. const int mv_stride = (mb_width << mv_sample_log2) +
  1498. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1499. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1500. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1501. av_frame_make_writable(pict);
  1502. pict->opaque = NULL;
  1503. #if FF_API_VISMV
  1504. ptr = pict->data[0];
  1505. #endif
  1506. block_height = 16 >> v_chroma_shift;
  1507. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1508. int mb_x;
  1509. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1510. const int mb_index = mb_x + mb_y * mb_stride;
  1511. #if FF_API_VISMV
  1512. if ((avctx->debug_mv) && motion_val[0]) {
  1513. int type;
  1514. for (type = 0; type < 3; type++) {
  1515. int direction = 0;
  1516. switch (type) {
  1517. case 0:
  1518. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1519. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1520. continue;
  1521. direction = 0;
  1522. break;
  1523. case 1:
  1524. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1525. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1526. continue;
  1527. direction = 0;
  1528. break;
  1529. case 2:
  1530. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1531. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1532. continue;
  1533. direction = 1;
  1534. break;
  1535. }
  1536. if (!USES_LIST(mbtype_table[mb_index], direction))
  1537. continue;
  1538. if (IS_8X8(mbtype_table[mb_index])) {
  1539. int i;
  1540. for (i = 0; i < 4; i++) {
  1541. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1542. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1543. int xy = (mb_x * 2 + (i & 1) +
  1544. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1545. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1546. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1547. draw_arrow(ptr, sx, sy, mx, my, width,
  1548. height, pict->linesize[0], 100, 0, direction);
  1549. }
  1550. } else if (IS_16X8(mbtype_table[mb_index])) {
  1551. int i;
  1552. for (i = 0; i < 2; i++) {
  1553. int sx = mb_x * 16 + 8;
  1554. int sy = mb_y * 16 + 4 + 8 * i;
  1555. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1556. int mx = (motion_val[direction][xy][0] >> shift);
  1557. int my = (motion_val[direction][xy][1] >> shift);
  1558. if (IS_INTERLACED(mbtype_table[mb_index]))
  1559. my *= 2;
  1560. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1561. height, pict->linesize[0], 100, 0, direction);
  1562. }
  1563. } else if (IS_8X16(mbtype_table[mb_index])) {
  1564. int i;
  1565. for (i = 0; i < 2; i++) {
  1566. int sx = mb_x * 16 + 4 + 8 * i;
  1567. int sy = mb_y * 16 + 8;
  1568. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1569. int mx = motion_val[direction][xy][0] >> shift;
  1570. int my = motion_val[direction][xy][1] >> shift;
  1571. if (IS_INTERLACED(mbtype_table[mb_index]))
  1572. my *= 2;
  1573. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1574. height, pict->linesize[0], 100, 0, direction);
  1575. }
  1576. } else {
  1577. int sx= mb_x * 16 + 8;
  1578. int sy= mb_y * 16 + 8;
  1579. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1580. int mx= (motion_val[direction][xy][0]>>shift) + sx;
  1581. int my= (motion_val[direction][xy][1]>>shift) + sy;
  1582. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100, 0, direction);
  1583. }
  1584. }
  1585. }
  1586. #endif
  1587. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1588. uint64_t c = (qscale_table[mb_index] * 128 / 31) *
  1589. 0x0101010101010101ULL;
  1590. int y;
  1591. for (y = 0; y < block_height; y++) {
  1592. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1593. (block_height * mb_y + y) *
  1594. pict->linesize[1]) = c;
  1595. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1596. (block_height * mb_y + y) *
  1597. pict->linesize[2]) = c;
  1598. }
  1599. }
  1600. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1601. motion_val[0]) {
  1602. int mb_type = mbtype_table[mb_index];
  1603. uint64_t u,v;
  1604. int y;
  1605. #define COLOR(theta, r) \
  1606. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1607. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1608. u = v = 128;
  1609. if (IS_PCM(mb_type)) {
  1610. COLOR(120, 48)
  1611. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1612. IS_INTRA16x16(mb_type)) {
  1613. COLOR(30, 48)
  1614. } else if (IS_INTRA4x4(mb_type)) {
  1615. COLOR(90, 48)
  1616. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1617. // COLOR(120, 48)
  1618. } else if (IS_DIRECT(mb_type)) {
  1619. COLOR(150, 48)
  1620. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1621. COLOR(170, 48)
  1622. } else if (IS_GMC(mb_type)) {
  1623. COLOR(190, 48)
  1624. } else if (IS_SKIP(mb_type)) {
  1625. // COLOR(180, 48)
  1626. } else if (!USES_LIST(mb_type, 1)) {
  1627. COLOR(240, 48)
  1628. } else if (!USES_LIST(mb_type, 0)) {
  1629. COLOR(0, 48)
  1630. } else {
  1631. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1632. COLOR(300,48)
  1633. }
  1634. u *= 0x0101010101010101ULL;
  1635. v *= 0x0101010101010101ULL;
  1636. for (y = 0; y < block_height; y++) {
  1637. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1638. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1639. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1640. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1641. }
  1642. // segmentation
  1643. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1644. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1645. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1646. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1647. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1648. }
  1649. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1650. for (y = 0; y < 16; y++)
  1651. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1652. pict->linesize[0]] ^= 0x80;
  1653. }
  1654. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1655. int dm = 1 << (mv_sample_log2 - 2);
  1656. for (i = 0; i < 4; i++) {
  1657. int sx = mb_x * 16 + 8 * (i & 1);
  1658. int sy = mb_y * 16 + 8 * (i >> 1);
  1659. int xy = (mb_x * 2 + (i & 1) +
  1660. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1661. // FIXME bidir
  1662. int32_t *mv = (int32_t *) &motion_val[0][xy];
  1663. if (mv[0] != mv[dm] ||
  1664. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1665. for (y = 0; y < 8; y++)
  1666. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1667. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1668. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1669. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1670. }
  1671. }
  1672. if (IS_INTERLACED(mb_type) &&
  1673. avctx->codec->id == AV_CODEC_ID_H264) {
  1674. // hmm
  1675. }
  1676. }
  1677. if (mbskip_table)
  1678. mbskip_table[mb_index] = 0;
  1679. }
  1680. }
  1681. }
  1682. }
  1683. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1684. {
  1685. ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
  1686. p->qscale_table, p->motion_val, &s->low_delay,
  1687. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1688. }
  1689. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1690. {
  1691. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1692. int offset = 2*s->mb_stride + 1;
  1693. if(!ref)
  1694. return AVERROR(ENOMEM);
  1695. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1696. ref->size -= offset;
  1697. ref->data += offset;
  1698. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1699. }
  1700. static inline int hpel_motion_lowres(MpegEncContext *s,
  1701. uint8_t *dest, uint8_t *src,
  1702. int field_based, int field_select,
  1703. int src_x, int src_y,
  1704. int width, int height, ptrdiff_t stride,
  1705. int h_edge_pos, int v_edge_pos,
  1706. int w, int h, h264_chroma_mc_func *pix_op,
  1707. int motion_x, int motion_y)
  1708. {
  1709. const int lowres = s->avctx->lowres;
  1710. const int op_index = FFMIN(lowres, 3);
  1711. const int s_mask = (2 << lowres) - 1;
  1712. int emu = 0;
  1713. int sx, sy;
  1714. if (s->quarter_sample) {
  1715. motion_x /= 2;
  1716. motion_y /= 2;
  1717. }
  1718. sx = motion_x & s_mask;
  1719. sy = motion_y & s_mask;
  1720. src_x += motion_x >> lowres + 1;
  1721. src_y += motion_y >> lowres + 1;
  1722. src += src_y * stride + src_x;
  1723. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1724. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1725. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
  1726. s->linesize, s->linesize,
  1727. w + 1, (h + 1) << field_based,
  1728. src_x, src_y << field_based,
  1729. h_edge_pos, v_edge_pos);
  1730. src = s->sc.edge_emu_buffer;
  1731. emu = 1;
  1732. }
  1733. sx = (sx << 2) >> lowres;
  1734. sy = (sy << 2) >> lowres;
  1735. if (field_select)
  1736. src += s->linesize;
  1737. pix_op[op_index](dest, src, stride, h, sx, sy);
  1738. return emu;
  1739. }
  1740. /* apply one mpeg motion vector to the three components */
  1741. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1742. uint8_t *dest_y,
  1743. uint8_t *dest_cb,
  1744. uint8_t *dest_cr,
  1745. int field_based,
  1746. int bottom_field,
  1747. int field_select,
  1748. uint8_t **ref_picture,
  1749. h264_chroma_mc_func *pix_op,
  1750. int motion_x, int motion_y,
  1751. int h, int mb_y)
  1752. {
  1753. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1754. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  1755. ptrdiff_t uvlinesize, linesize;
  1756. const int lowres = s->avctx->lowres;
  1757. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  1758. const int block_s = 8>>lowres;
  1759. const int s_mask = (2 << lowres) - 1;
  1760. const int h_edge_pos = s->h_edge_pos >> lowres;
  1761. const int v_edge_pos = s->v_edge_pos >> lowres;
  1762. linesize = s->current_picture.f->linesize[0] << field_based;
  1763. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  1764. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1765. if (s->quarter_sample) {
  1766. motion_x /= 2;
  1767. motion_y /= 2;
  1768. }
  1769. if(field_based){
  1770. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1771. }
  1772. sx = motion_x & s_mask;
  1773. sy = motion_y & s_mask;
  1774. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1775. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1776. if (s->out_format == FMT_H263) {
  1777. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1778. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1779. uvsrc_x = src_x >> 1;
  1780. uvsrc_y = src_y >> 1;
  1781. } else if (s->out_format == FMT_H261) {
  1782. // even chroma mv's are full pel in H261
  1783. mx = motion_x / 4;
  1784. my = motion_y / 4;
  1785. uvsx = (2 * mx) & s_mask;
  1786. uvsy = (2 * my) & s_mask;
  1787. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1788. uvsrc_y = mb_y * block_s + (my >> lowres);
  1789. } else {
  1790. if(s->chroma_y_shift){
  1791. mx = motion_x / 2;
  1792. my = motion_y / 2;
  1793. uvsx = mx & s_mask;
  1794. uvsy = my & s_mask;
  1795. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1796. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1797. } else {
  1798. if(s->chroma_x_shift){
  1799. //Chroma422
  1800. mx = motion_x / 2;
  1801. uvsx = mx & s_mask;
  1802. uvsy = motion_y & s_mask;
  1803. uvsrc_y = src_y;
  1804. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1805. } else {
  1806. //Chroma444
  1807. uvsx = motion_x & s_mask;
  1808. uvsy = motion_y & s_mask;
  1809. uvsrc_x = src_x;
  1810. uvsrc_y = src_y;
  1811. }
  1812. }
  1813. }
  1814. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1815. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1816. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1817. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  1818. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1819. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
  1820. linesize >> field_based, linesize >> field_based,
  1821. 17, 17 + field_based,
  1822. src_x, src_y << field_based, h_edge_pos,
  1823. v_edge_pos);
  1824. ptr_y = s->sc.edge_emu_buffer;
  1825. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  1826. uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
  1827. uint8_t *vbuf =ubuf + 9 * s->uvlinesize;
  1828. s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
  1829. uvlinesize >> field_based, uvlinesize >> field_based,
  1830. 9, 9 + field_based,
  1831. uvsrc_x, uvsrc_y << field_based,
  1832. h_edge_pos >> 1, v_edge_pos >> 1);
  1833. s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
  1834. uvlinesize >> field_based,uvlinesize >> field_based,
  1835. 9, 9 + field_based,
  1836. uvsrc_x, uvsrc_y << field_based,
  1837. h_edge_pos >> 1, v_edge_pos >> 1);
  1838. ptr_cb = ubuf;
  1839. ptr_cr = vbuf;
  1840. }
  1841. }
  1842. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
  1843. if (bottom_field) {
  1844. dest_y += s->linesize;
  1845. dest_cb += s->uvlinesize;
  1846. dest_cr += s->uvlinesize;
  1847. }
  1848. if (field_select) {
  1849. ptr_y += s->linesize;
  1850. ptr_cb += s->uvlinesize;
  1851. ptr_cr += s->uvlinesize;
  1852. }
  1853. sx = (sx << 2) >> lowres;
  1854. sy = (sy << 2) >> lowres;
  1855. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1856. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  1857. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  1858. uvsx = (uvsx << 2) >> lowres;
  1859. uvsy = (uvsy << 2) >> lowres;
  1860. if (hc) {
  1861. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  1862. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  1863. }
  1864. }
  1865. // FIXME h261 lowres loop filter
  1866. }
  1867. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1868. uint8_t *dest_cb, uint8_t *dest_cr,
  1869. uint8_t **ref_picture,
  1870. h264_chroma_mc_func * pix_op,
  1871. int mx, int my)
  1872. {
  1873. const int lowres = s->avctx->lowres;
  1874. const int op_index = FFMIN(lowres, 3);
  1875. const int block_s = 8 >> lowres;
  1876. const int s_mask = (2 << lowres) - 1;
  1877. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1878. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1879. int emu = 0, src_x, src_y, sx, sy;
  1880. ptrdiff_t offset;
  1881. uint8_t *ptr;
  1882. if (s->quarter_sample) {
  1883. mx /= 2;
  1884. my /= 2;
  1885. }
  1886. /* In case of 8X8, we construct a single chroma motion vector
  1887. with a special rounding */
  1888. mx = ff_h263_round_chroma(mx);
  1889. my = ff_h263_round_chroma(my);
  1890. sx = mx & s_mask;
  1891. sy = my & s_mask;
  1892. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  1893. src_y = s->mb_y * block_s + (my >> lowres + 1);
  1894. offset = src_y * s->uvlinesize + src_x;
  1895. ptr = ref_picture[1] + offset;
  1896. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  1897. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  1898. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1899. s->uvlinesize, s->uvlinesize,
  1900. 9, 9,
  1901. src_x, src_y, h_edge_pos, v_edge_pos);
  1902. ptr = s->sc.edge_emu_buffer;
  1903. emu = 1;
  1904. }
  1905. sx = (sx << 2) >> lowres;
  1906. sy = (sy << 2) >> lowres;
  1907. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1908. ptr = ref_picture[2] + offset;
  1909. if (emu) {
  1910. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1911. s->uvlinesize, s->uvlinesize,
  1912. 9, 9,
  1913. src_x, src_y, h_edge_pos, v_edge_pos);
  1914. ptr = s->sc.edge_emu_buffer;
  1915. }
  1916. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1917. }
  1918. /**
  1919. * motion compensation of a single macroblock
  1920. * @param s context
  1921. * @param dest_y luma destination pointer
  1922. * @param dest_cb chroma cb/u destination pointer
  1923. * @param dest_cr chroma cr/v destination pointer
  1924. * @param dir direction (0->forward, 1->backward)
  1925. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1926. * @param pix_op halfpel motion compensation function (average or put normally)
  1927. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1928. */
  1929. static inline void MPV_motion_lowres(MpegEncContext *s,
  1930. uint8_t *dest_y, uint8_t *dest_cb,
  1931. uint8_t *dest_cr,
  1932. int dir, uint8_t **ref_picture,
  1933. h264_chroma_mc_func *pix_op)
  1934. {
  1935. int mx, my;
  1936. int mb_x, mb_y, i;
  1937. const int lowres = s->avctx->lowres;
  1938. const int block_s = 8 >>lowres;
  1939. mb_x = s->mb_x;
  1940. mb_y = s->mb_y;
  1941. switch (s->mv_type) {
  1942. case MV_TYPE_16X16:
  1943. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1944. 0, 0, 0,
  1945. ref_picture, pix_op,
  1946. s->mv[dir][0][0], s->mv[dir][0][1],
  1947. 2 * block_s, mb_y);
  1948. break;
  1949. case MV_TYPE_8X8:
  1950. mx = 0;
  1951. my = 0;
  1952. for (i = 0; i < 4; i++) {
  1953. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  1954. s->linesize) * block_s,
  1955. ref_picture[0], 0, 0,
  1956. (2 * mb_x + (i & 1)) * block_s,
  1957. (2 * mb_y + (i >> 1)) * block_s,
  1958. s->width, s->height, s->linesize,
  1959. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1960. block_s, block_s, pix_op,
  1961. s->mv[dir][i][0], s->mv[dir][i][1]);
  1962. mx += s->mv[dir][i][0];
  1963. my += s->mv[dir][i][1];
  1964. }
  1965. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY))
  1966. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  1967. pix_op, mx, my);
  1968. break;
  1969. case MV_TYPE_FIELD:
  1970. if (s->picture_structure == PICT_FRAME) {
  1971. /* top field */
  1972. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1973. 1, 0, s->field_select[dir][0],
  1974. ref_picture, pix_op,
  1975. s->mv[dir][0][0], s->mv[dir][0][1],
  1976. block_s, mb_y);
  1977. /* bottom field */
  1978. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1979. 1, 1, s->field_select[dir][1],
  1980. ref_picture, pix_op,
  1981. s->mv[dir][1][0], s->mv[dir][1][1],
  1982. block_s, mb_y);
  1983. } else {
  1984. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  1985. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  1986. ref_picture = s->current_picture_ptr->f->data;
  1987. }
  1988. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1989. 0, 0, s->field_select[dir][0],
  1990. ref_picture, pix_op,
  1991. s->mv[dir][0][0],
  1992. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  1993. }
  1994. break;
  1995. case MV_TYPE_16X8:
  1996. for (i = 0; i < 2; i++) {
  1997. uint8_t **ref2picture;
  1998. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  1999. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2000. ref2picture = ref_picture;
  2001. } else {
  2002. ref2picture = s->current_picture_ptr->f->data;
  2003. }
  2004. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2005. 0, 0, s->field_select[dir][i],
  2006. ref2picture, pix_op,
  2007. s->mv[dir][i][0], s->mv[dir][i][1] +
  2008. 2 * block_s * i, block_s, mb_y >> 1);
  2009. dest_y += 2 * block_s * s->linesize;
  2010. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2011. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2012. }
  2013. break;
  2014. case MV_TYPE_DMV:
  2015. if (s->picture_structure == PICT_FRAME) {
  2016. for (i = 0; i < 2; i++) {
  2017. int j;
  2018. for (j = 0; j < 2; j++) {
  2019. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2020. 1, j, j ^ i,
  2021. ref_picture, pix_op,
  2022. s->mv[dir][2 * i + j][0],
  2023. s->mv[dir][2 * i + j][1],
  2024. block_s, mb_y);
  2025. }
  2026. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2027. }
  2028. } else {
  2029. for (i = 0; i < 2; i++) {
  2030. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2031. 0, 0, s->picture_structure != i + 1,
  2032. ref_picture, pix_op,
  2033. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2034. 2 * block_s, mb_y >> 1);
  2035. // after put we make avg of the same block
  2036. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2037. // opposite parity is always in the same
  2038. // frame if this is second field
  2039. if (!s->first_field) {
  2040. ref_picture = s->current_picture_ptr->f->data;
  2041. }
  2042. }
  2043. }
  2044. break;
  2045. default:
  2046. av_assert2(0);
  2047. }
  2048. }
  2049. /**
  2050. * find the lowest MB row referenced in the MVs
  2051. */
  2052. int ff_mpv_lowest_referenced_row(MpegEncContext *s, int dir)
  2053. {
  2054. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2055. int my, off, i, mvs;
  2056. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2057. goto unhandled;
  2058. switch (s->mv_type) {
  2059. case MV_TYPE_16X16:
  2060. mvs = 1;
  2061. break;
  2062. case MV_TYPE_16X8:
  2063. mvs = 2;
  2064. break;
  2065. case MV_TYPE_8X8:
  2066. mvs = 4;
  2067. break;
  2068. default:
  2069. goto unhandled;
  2070. }
  2071. for (i = 0; i < mvs; i++) {
  2072. my = s->mv[dir][i][1];
  2073. my_max = FFMAX(my_max, my);
  2074. my_min = FFMIN(my_min, my);
  2075. }
  2076. off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
  2077. return av_clip(s->mb_y + off, 0, s->mb_height - 1);
  2078. unhandled:
  2079. return s->mb_height-1;
  2080. }
  2081. /* put block[] to dest[] */
  2082. static inline void put_dct(MpegEncContext *s,
  2083. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2084. {
  2085. s->dct_unquantize_intra(s, block, i, qscale);
  2086. s->idsp.idct_put(dest, line_size, block);
  2087. }
  2088. /* add block[] to dest[] */
  2089. static inline void add_dct(MpegEncContext *s,
  2090. int16_t *block, int i, uint8_t *dest, int line_size)
  2091. {
  2092. if (s->block_last_index[i] >= 0) {
  2093. s->idsp.idct_add(dest, line_size, block);
  2094. }
  2095. }
  2096. static inline void add_dequant_dct(MpegEncContext *s,
  2097. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2098. {
  2099. if (s->block_last_index[i] >= 0) {
  2100. s->dct_unquantize_inter(s, block, i, qscale);
  2101. s->idsp.idct_add(dest, line_size, block);
  2102. }
  2103. }
  2104. /**
  2105. * Clean dc, ac, coded_block for the current non-intra MB.
  2106. */
  2107. void ff_clean_intra_table_entries(MpegEncContext *s)
  2108. {
  2109. int wrap = s->b8_stride;
  2110. int xy = s->block_index[0];
  2111. s->dc_val[0][xy ] =
  2112. s->dc_val[0][xy + 1 ] =
  2113. s->dc_val[0][xy + wrap] =
  2114. s->dc_val[0][xy + 1 + wrap] = 1024;
  2115. /* ac pred */
  2116. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2117. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2118. if (s->msmpeg4_version>=3) {
  2119. s->coded_block[xy ] =
  2120. s->coded_block[xy + 1 ] =
  2121. s->coded_block[xy + wrap] =
  2122. s->coded_block[xy + 1 + wrap] = 0;
  2123. }
  2124. /* chroma */
  2125. wrap = s->mb_stride;
  2126. xy = s->mb_x + s->mb_y * wrap;
  2127. s->dc_val[1][xy] =
  2128. s->dc_val[2][xy] = 1024;
  2129. /* ac pred */
  2130. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2131. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2132. s->mbintra_table[xy]= 0;
  2133. }
  2134. /* generic function called after a macroblock has been parsed by the
  2135. decoder or after it has been encoded by the encoder.
  2136. Important variables used:
  2137. s->mb_intra : true if intra macroblock
  2138. s->mv_dir : motion vector direction
  2139. s->mv_type : motion vector type
  2140. s->mv : motion vector
  2141. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2142. */
  2143. static av_always_inline
  2144. void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2145. int lowres_flag, int is_mpeg12)
  2146. {
  2147. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2148. if (CONFIG_XVMC &&
  2149. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2150. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2151. return;
  2152. }
  2153. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2154. /* print DCT coefficients */
  2155. int i,j;
  2156. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2157. for(i=0; i<6; i++){
  2158. for(j=0; j<64; j++){
  2159. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  2160. block[i][s->idsp.idct_permutation[j]]);
  2161. }
  2162. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2163. }
  2164. }
  2165. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2166. /* update DC predictors for P macroblocks */
  2167. if (!s->mb_intra) {
  2168. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2169. if(s->mbintra_table[mb_xy])
  2170. ff_clean_intra_table_entries(s);
  2171. } else {
  2172. s->last_dc[0] =
  2173. s->last_dc[1] =
  2174. s->last_dc[2] = 128 << s->intra_dc_precision;
  2175. }
  2176. }
  2177. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2178. s->mbintra_table[mb_xy]=1;
  2179. if ((s->avctx->flags & CODEC_FLAG_PSNR) || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor ||
  2180. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  2181. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  2182. uint8_t *dest_y, *dest_cb, *dest_cr;
  2183. int dct_linesize, dct_offset;
  2184. op_pixels_func (*op_pix)[4];
  2185. qpel_mc_func (*op_qpix)[16];
  2186. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2187. const int uvlinesize = s->current_picture.f->linesize[1];
  2188. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2189. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2190. /* avoid copy if macroblock skipped in last frame too */
  2191. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2192. if(!s->encoding){
  2193. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2194. if (s->mb_skipped) {
  2195. s->mb_skipped= 0;
  2196. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2197. *mbskip_ptr = 1;
  2198. } else if(!s->current_picture.reference) {
  2199. *mbskip_ptr = 1;
  2200. } else{
  2201. *mbskip_ptr = 0; /* not skipped */
  2202. }
  2203. }
  2204. dct_linesize = linesize << s->interlaced_dct;
  2205. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2206. if(readable){
  2207. dest_y= s->dest[0];
  2208. dest_cb= s->dest[1];
  2209. dest_cr= s->dest[2];
  2210. }else{
  2211. dest_y = s->sc.b_scratchpad;
  2212. dest_cb= s->sc.b_scratchpad+16*linesize;
  2213. dest_cr= s->sc.b_scratchpad+32*linesize;
  2214. }
  2215. if (!s->mb_intra) {
  2216. /* motion handling */
  2217. /* decoding or more than one mb_type (MC was already done otherwise) */
  2218. if(!s->encoding){
  2219. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2220. if (s->mv_dir & MV_DIR_FORWARD) {
  2221. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2222. ff_mpv_lowest_referenced_row(s, 0),
  2223. 0);
  2224. }
  2225. if (s->mv_dir & MV_DIR_BACKWARD) {
  2226. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2227. ff_mpv_lowest_referenced_row(s, 1),
  2228. 0);
  2229. }
  2230. }
  2231. if(lowres_flag){
  2232. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2233. if (s->mv_dir & MV_DIR_FORWARD) {
  2234. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
  2235. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2236. }
  2237. if (s->mv_dir & MV_DIR_BACKWARD) {
  2238. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
  2239. }
  2240. }else{
  2241. op_qpix = s->me.qpel_put;
  2242. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2243. op_pix = s->hdsp.put_pixels_tab;
  2244. }else{
  2245. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2246. }
  2247. if (s->mv_dir & MV_DIR_FORWARD) {
  2248. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  2249. op_pix = s->hdsp.avg_pixels_tab;
  2250. op_qpix= s->me.qpel_avg;
  2251. }
  2252. if (s->mv_dir & MV_DIR_BACKWARD) {
  2253. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  2254. }
  2255. }
  2256. }
  2257. /* skip dequant / idct if we are really late ;) */
  2258. if(s->avctx->skip_idct){
  2259. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2260. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2261. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2262. goto skip_idct;
  2263. }
  2264. /* add dct residue */
  2265. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2266. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2267. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2268. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2269. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2270. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2271. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2272. if (s->chroma_y_shift){
  2273. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2274. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2275. }else{
  2276. dct_linesize >>= 1;
  2277. dct_offset >>=1;
  2278. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2279. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2280. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2281. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2282. }
  2283. }
  2284. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2285. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2286. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2287. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2288. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2289. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2290. if(s->chroma_y_shift){//Chroma420
  2291. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2292. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2293. }else{
  2294. //chroma422
  2295. dct_linesize = uvlinesize << s->interlaced_dct;
  2296. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2297. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2298. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2299. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2300. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2301. if(!s->chroma_x_shift){//Chroma444
  2302. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2303. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2304. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2305. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2306. }
  2307. }
  2308. }//fi gray
  2309. }
  2310. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2311. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2312. }
  2313. } else {
  2314. /* dct only in intra block */
  2315. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2316. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2317. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2318. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2319. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2320. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2321. if(s->chroma_y_shift){
  2322. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2323. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2324. }else{
  2325. dct_offset >>=1;
  2326. dct_linesize >>=1;
  2327. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2328. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2329. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2330. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2331. }
  2332. }
  2333. }else{
  2334. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  2335. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2336. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  2337. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2338. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2339. if(s->chroma_y_shift){
  2340. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  2341. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  2342. }else{
  2343. dct_linesize = uvlinesize << s->interlaced_dct;
  2344. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2345. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  2346. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  2347. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2348. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2349. if(!s->chroma_x_shift){//Chroma444
  2350. s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2351. s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2352. s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2353. s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2354. }
  2355. }
  2356. }//gray
  2357. }
  2358. }
  2359. skip_idct:
  2360. if(!readable){
  2361. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2362. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  2363. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2364. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2365. }
  2366. }
  2367. }
  2368. }
  2369. void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
  2370. {
  2371. #if !CONFIG_SMALL
  2372. if(s->out_format == FMT_MPEG1) {
  2373. if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 1);
  2374. else mpv_decode_mb_internal(s, block, 0, 1);
  2375. } else
  2376. #endif
  2377. if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 0);
  2378. else mpv_decode_mb_internal(s, block, 0, 0);
  2379. }
  2380. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2381. {
  2382. ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
  2383. s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
  2384. s->first_field, s->low_delay);
  2385. }
  2386. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2387. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2388. const int uvlinesize = s->current_picture.f->linesize[1];
  2389. const int mb_size= 4 - s->avctx->lowres;
  2390. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2391. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2392. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2393. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2394. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2395. 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;
  2396. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2397. s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << mb_size);
  2398. s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
  2399. s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
  2400. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2401. {
  2402. if(s->picture_structure==PICT_FRAME){
  2403. s->dest[0] += s->mb_y * linesize << mb_size;
  2404. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2405. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2406. }else{
  2407. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2408. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2409. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2410. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2411. }
  2412. }
  2413. }
  2414. /**
  2415. * Permute an 8x8 block.
  2416. * @param block the block which will be permuted according to the given permutation vector
  2417. * @param permutation the permutation vector
  2418. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2419. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2420. * (inverse) permutated to scantable order!
  2421. */
  2422. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2423. {
  2424. int i;
  2425. int16_t temp[64];
  2426. if(last<=0) return;
  2427. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2428. for(i=0; i<=last; i++){
  2429. const int j= scantable[i];
  2430. temp[j]= block[j];
  2431. block[j]=0;
  2432. }
  2433. for(i=0; i<=last; i++){
  2434. const int j= scantable[i];
  2435. const int perm_j= permutation[j];
  2436. block[perm_j]= temp[j];
  2437. }
  2438. }
  2439. void ff_mpeg_flush(AVCodecContext *avctx){
  2440. int i;
  2441. MpegEncContext *s = avctx->priv_data;
  2442. if (!s || !s->picture)
  2443. return;
  2444. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2445. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  2446. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2447. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  2448. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  2449. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  2450. s->mb_x= s->mb_y= 0;
  2451. s->closed_gop= 0;
  2452. s->parse_context.state= -1;
  2453. s->parse_context.frame_start_found= 0;
  2454. s->parse_context.overread= 0;
  2455. s->parse_context.overread_index= 0;
  2456. s->parse_context.index= 0;
  2457. s->parse_context.last_index= 0;
  2458. s->bitstream_buffer_size=0;
  2459. s->pp_time=0;
  2460. }
  2461. /**
  2462. * set qscale and update qscale dependent variables.
  2463. */
  2464. void ff_set_qscale(MpegEncContext * s, int qscale)
  2465. {
  2466. if (qscale < 1)
  2467. qscale = 1;
  2468. else if (qscale > 31)
  2469. qscale = 31;
  2470. s->qscale = qscale;
  2471. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2472. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2473. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2474. }
  2475. void ff_mpv_report_decode_progress(MpegEncContext *s)
  2476. {
  2477. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2478. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2479. }