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.

2807 lines
104KB

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