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.

2803 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. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1043. av_log(avctx, AV_LOG_ERROR,
  1044. "releasing zombie picture\n");
  1045. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1046. }
  1047. }
  1048. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1049. /* release non reference frames */
  1050. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1051. if (!s->picture[i].reference)
  1052. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1053. }
  1054. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  1055. // we already have a unused image
  1056. // (maybe it was set before reading the header)
  1057. pic = s->current_picture_ptr;
  1058. } else {
  1059. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1060. if (i < 0) {
  1061. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1062. return i;
  1063. }
  1064. pic = &s->picture[i];
  1065. }
  1066. pic->reference = 0;
  1067. if (!s->droppable) {
  1068. if (s->pict_type != AV_PICTURE_TYPE_B)
  1069. pic->reference = 3;
  1070. }
  1071. pic->f->coded_picture_number = s->coded_picture_number++;
  1072. if (alloc_picture(s, pic, 0) < 0)
  1073. return -1;
  1074. s->current_picture_ptr = pic;
  1075. // FIXME use only the vars from current_pic
  1076. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1077. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1078. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1079. if (s->picture_structure != PICT_FRAME)
  1080. s->current_picture_ptr->f->top_field_first =
  1081. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1082. }
  1083. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1084. !s->progressive_sequence;
  1085. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1086. s->current_picture_ptr->f->pict_type = s->pict_type;
  1087. // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
  1088. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1089. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1090. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1091. s->current_picture_ptr)) < 0)
  1092. return ret;
  1093. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1094. s->last_picture_ptr = s->next_picture_ptr;
  1095. if (!s->droppable)
  1096. s->next_picture_ptr = s->current_picture_ptr;
  1097. }
  1098. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1099. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1100. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1101. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1102. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1103. s->pict_type, s->droppable);
  1104. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  1105. (s->pict_type != AV_PICTURE_TYPE_I ||
  1106. s->picture_structure != PICT_FRAME)) {
  1107. int h_chroma_shift, v_chroma_shift;
  1108. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1109. &h_chroma_shift, &v_chroma_shift);
  1110. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
  1111. av_log(avctx, AV_LOG_DEBUG,
  1112. "allocating dummy last picture for B frame\n");
  1113. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1114. av_log(avctx, AV_LOG_ERROR,
  1115. "warning: first frame is no keyframe\n");
  1116. else if (s->picture_structure != PICT_FRAME)
  1117. av_log(avctx, AV_LOG_DEBUG,
  1118. "allocate dummy last picture for field based first keyframe\n");
  1119. /* Allocate a dummy frame */
  1120. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1121. if (i < 0) {
  1122. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1123. return i;
  1124. }
  1125. s->last_picture_ptr = &s->picture[i];
  1126. s->last_picture_ptr->reference = 3;
  1127. s->last_picture_ptr->f->key_frame = 0;
  1128. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1129. if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1130. s->last_picture_ptr = NULL;
  1131. return -1;
  1132. }
  1133. if (!avctx->hwaccel && !(avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU)) {
  1134. for(i=0; i<avctx->height; i++)
  1135. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
  1136. 0x80, avctx->width);
  1137. if (s->last_picture_ptr->f->data[2]) {
  1138. for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1139. memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
  1140. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1141. memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
  1142. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1143. }
  1144. }
  1145. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1146. for(i=0; i<avctx->height; i++)
  1147. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
  1148. }
  1149. }
  1150. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1151. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1152. }
  1153. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1154. s->pict_type == AV_PICTURE_TYPE_B) {
  1155. /* Allocate a dummy frame */
  1156. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1157. if (i < 0) {
  1158. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1159. return i;
  1160. }
  1161. s->next_picture_ptr = &s->picture[i];
  1162. s->next_picture_ptr->reference = 3;
  1163. s->next_picture_ptr->f->key_frame = 0;
  1164. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1165. if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1166. s->next_picture_ptr = NULL;
  1167. return -1;
  1168. }
  1169. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1170. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1171. }
  1172. #if 0 // BUFREF-FIXME
  1173. memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
  1174. memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
  1175. #endif
  1176. if (s->last_picture_ptr) {
  1177. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1178. if (s->last_picture_ptr->f->buf[0] &&
  1179. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1180. s->last_picture_ptr)) < 0)
  1181. return ret;
  1182. }
  1183. if (s->next_picture_ptr) {
  1184. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1185. if (s->next_picture_ptr->f->buf[0] &&
  1186. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1187. s->next_picture_ptr)) < 0)
  1188. return ret;
  1189. }
  1190. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1191. s->last_picture_ptr->f->buf[0]));
  1192. if (s->picture_structure!= PICT_FRAME) {
  1193. int i;
  1194. for (i = 0; i < 4; i++) {
  1195. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1196. s->current_picture.f->data[i] +=
  1197. s->current_picture.f->linesize[i];
  1198. }
  1199. s->current_picture.f->linesize[i] *= 2;
  1200. s->last_picture.f->linesize[i] *= 2;
  1201. s->next_picture.f->linesize[i] *= 2;
  1202. }
  1203. }
  1204. /* set dequantizer, we can't do it during init as
  1205. * it might change for mpeg4 and we can't do it in the header
  1206. * decode as init is not called for mpeg4 there yet */
  1207. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1208. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1209. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1210. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1211. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1212. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1213. } else {
  1214. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1215. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1216. }
  1217. if (s->avctx->debug & FF_DEBUG_NOMC) {
  1218. gray_frame(s->current_picture_ptr->f);
  1219. }
  1220. return 0;
  1221. }
  1222. /* called after a frame has been decoded. */
  1223. void ff_mpv_frame_end(MpegEncContext *s)
  1224. {
  1225. emms_c();
  1226. if (s->current_picture.reference)
  1227. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1228. }
  1229. #if FF_API_VISMV
  1230. static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
  1231. {
  1232. if(*sx > *ex)
  1233. return clip_line(ex, ey, sx, sy, maxx);
  1234. if (*sx < 0) {
  1235. if (*ex < 0)
  1236. return 1;
  1237. *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
  1238. *sx = 0;
  1239. }
  1240. if (*ex > maxx) {
  1241. if (*sx > maxx)
  1242. return 1;
  1243. *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
  1244. *ex = maxx;
  1245. }
  1246. return 0;
  1247. }
  1248. /**
  1249. * Draw a line from (ex, ey) -> (sx, sy).
  1250. * @param w width of the image
  1251. * @param h height of the image
  1252. * @param stride stride/linesize of the image
  1253. * @param color color of the arrow
  1254. */
  1255. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1256. int w, int h, int stride, int color)
  1257. {
  1258. int x, y, fr, f;
  1259. if (clip_line(&sx, &sy, &ex, &ey, w - 1))
  1260. return;
  1261. if (clip_line(&sy, &sx, &ey, &ex, h - 1))
  1262. return;
  1263. sx = av_clip(sx, 0, w - 1);
  1264. sy = av_clip(sy, 0, h - 1);
  1265. ex = av_clip(ex, 0, w - 1);
  1266. ey = av_clip(ey, 0, h - 1);
  1267. buf[sy * stride + sx] += color;
  1268. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1269. if (sx > ex) {
  1270. FFSWAP(int, sx, ex);
  1271. FFSWAP(int, sy, ey);
  1272. }
  1273. buf += sx + sy * stride;
  1274. ex -= sx;
  1275. f = ((ey - sy) << 16) / ex;
  1276. for (x = 0; x <= ex; x++) {
  1277. y = (x * f) >> 16;
  1278. fr = (x * f) & 0xFFFF;
  1279. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1280. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1281. }
  1282. } else {
  1283. if (sy > ey) {
  1284. FFSWAP(int, sx, ex);
  1285. FFSWAP(int, sy, ey);
  1286. }
  1287. buf += sx + sy * stride;
  1288. ey -= sy;
  1289. if (ey)
  1290. f = ((ex - sx) << 16) / ey;
  1291. else
  1292. f = 0;
  1293. for(y= 0; y <= ey; y++){
  1294. x = (y*f) >> 16;
  1295. fr = (y*f) & 0xFFFF;
  1296. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1297. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1298. }
  1299. }
  1300. }
  1301. /**
  1302. * Draw an arrow from (ex, ey) -> (sx, sy).
  1303. * @param w width of the image
  1304. * @param h height of the image
  1305. * @param stride stride/linesize of the image
  1306. * @param color color of the arrow
  1307. */
  1308. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1309. int ey, int w, int h, int stride, int color, int tail, int direction)
  1310. {
  1311. int dx,dy;
  1312. if (direction) {
  1313. FFSWAP(int, sx, ex);
  1314. FFSWAP(int, sy, ey);
  1315. }
  1316. sx = av_clip(sx, -100, w + 100);
  1317. sy = av_clip(sy, -100, h + 100);
  1318. ex = av_clip(ex, -100, w + 100);
  1319. ey = av_clip(ey, -100, h + 100);
  1320. dx = ex - sx;
  1321. dy = ey - sy;
  1322. if (dx * dx + dy * dy > 3 * 3) {
  1323. int rx = dx + dy;
  1324. int ry = -dx + dy;
  1325. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1326. // FIXME subpixel accuracy
  1327. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1328. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1329. if (tail) {
  1330. rx = -rx;
  1331. ry = -ry;
  1332. }
  1333. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1334. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1335. }
  1336. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1337. }
  1338. #endif
  1339. static int add_mb(AVMotionVector *mb, uint32_t mb_type,
  1340. int dst_x, int dst_y,
  1341. int src_x, int src_y,
  1342. int direction)
  1343. {
  1344. mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
  1345. mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
  1346. mb->src_x = src_x;
  1347. mb->src_y = src_y;
  1348. mb->dst_x = dst_x;
  1349. mb->dst_y = dst_y;
  1350. mb->source = direction ? 1 : -1;
  1351. mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
  1352. return 1;
  1353. }
  1354. /**
  1355. * Print debugging info for the given picture.
  1356. */
  1357. void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
  1358. uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
  1359. int *low_delay,
  1360. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1361. {
  1362. if ((avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) && mbtype_table && motion_val[0]) {
  1363. const int shift = 1 + quarter_sample;
  1364. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1365. const int mv_stride = (mb_width << mv_sample_log2) +
  1366. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1367. int mb_x, mb_y, mbcount = 0;
  1368. /* size is width * height * 2 * 4 where 2 is for directions and 4 is
  1369. * for the maximum number of MB (4 MB in case of IS_8x8) */
  1370. AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
  1371. if (!mvs)
  1372. return;
  1373. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1374. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1375. int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
  1376. for (direction = 0; direction < 2; direction++) {
  1377. if (!USES_LIST(mb_type, direction))
  1378. continue;
  1379. if (IS_8X8(mb_type)) {
  1380. for (i = 0; i < 4; i++) {
  1381. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1382. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1383. int xy = (mb_x * 2 + (i & 1) +
  1384. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1385. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1386. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1387. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
  1388. }
  1389. } else if (IS_16X8(mb_type)) {
  1390. for (i = 0; i < 2; i++) {
  1391. int sx = mb_x * 16 + 8;
  1392. int sy = mb_y * 16 + 4 + 8 * i;
  1393. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1394. int mx = (motion_val[direction][xy][0] >> shift);
  1395. int my = (motion_val[direction][xy][1] >> shift);
  1396. if (IS_INTERLACED(mb_type))
  1397. my *= 2;
  1398. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
  1399. }
  1400. } else if (IS_8X16(mb_type)) {
  1401. for (i = 0; i < 2; i++) {
  1402. int sx = mb_x * 16 + 4 + 8 * i;
  1403. int sy = mb_y * 16 + 8;
  1404. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1405. int mx = motion_val[direction][xy][0] >> shift;
  1406. int my = motion_val[direction][xy][1] >> shift;
  1407. if (IS_INTERLACED(mb_type))
  1408. my *= 2;
  1409. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
  1410. }
  1411. } else {
  1412. int sx = mb_x * 16 + 8;
  1413. int sy = mb_y * 16 + 8;
  1414. int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1415. int mx = (motion_val[direction][xy][0]>>shift) + sx;
  1416. int my = (motion_val[direction][xy][1]>>shift) + sy;
  1417. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
  1418. }
  1419. }
  1420. }
  1421. }
  1422. if (mbcount) {
  1423. AVFrameSideData *sd;
  1424. av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
  1425. sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
  1426. if (!sd) {
  1427. av_freep(&mvs);
  1428. return;
  1429. }
  1430. memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
  1431. }
  1432. av_freep(&mvs);
  1433. }
  1434. /* TODO: export all the following to make them accessible for users (and filters) */
  1435. if (avctx->hwaccel || !mbtype_table
  1436. || (avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU))
  1437. return;
  1438. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1439. int x,y;
  1440. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1441. av_get_picture_type_char(pict->pict_type));
  1442. for (y = 0; y < mb_height; y++) {
  1443. for (x = 0; x < mb_width; x++) {
  1444. if (avctx->debug & FF_DEBUG_SKIP) {
  1445. int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
  1446. if (count > 9)
  1447. count = 9;
  1448. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1449. }
  1450. if (avctx->debug & FF_DEBUG_QP) {
  1451. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1452. qscale_table[x + y * mb_stride]);
  1453. }
  1454. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1455. int mb_type = mbtype_table[x + y * mb_stride];
  1456. // Type & MV direction
  1457. if (IS_PCM(mb_type))
  1458. av_log(avctx, AV_LOG_DEBUG, "P");
  1459. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1460. av_log(avctx, AV_LOG_DEBUG, "A");
  1461. else if (IS_INTRA4x4(mb_type))
  1462. av_log(avctx, AV_LOG_DEBUG, "i");
  1463. else if (IS_INTRA16x16(mb_type))
  1464. av_log(avctx, AV_LOG_DEBUG, "I");
  1465. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1466. av_log(avctx, AV_LOG_DEBUG, "d");
  1467. else if (IS_DIRECT(mb_type))
  1468. av_log(avctx, AV_LOG_DEBUG, "D");
  1469. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1470. av_log(avctx, AV_LOG_DEBUG, "g");
  1471. else if (IS_GMC(mb_type))
  1472. av_log(avctx, AV_LOG_DEBUG, "G");
  1473. else if (IS_SKIP(mb_type))
  1474. av_log(avctx, AV_LOG_DEBUG, "S");
  1475. else if (!USES_LIST(mb_type, 1))
  1476. av_log(avctx, AV_LOG_DEBUG, ">");
  1477. else if (!USES_LIST(mb_type, 0))
  1478. av_log(avctx, AV_LOG_DEBUG, "<");
  1479. else {
  1480. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1481. av_log(avctx, AV_LOG_DEBUG, "X");
  1482. }
  1483. // segmentation
  1484. if (IS_8X8(mb_type))
  1485. av_log(avctx, AV_LOG_DEBUG, "+");
  1486. else if (IS_16X8(mb_type))
  1487. av_log(avctx, AV_LOG_DEBUG, "-");
  1488. else if (IS_8X16(mb_type))
  1489. av_log(avctx, AV_LOG_DEBUG, "|");
  1490. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1491. av_log(avctx, AV_LOG_DEBUG, " ");
  1492. else
  1493. av_log(avctx, AV_LOG_DEBUG, "?");
  1494. if (IS_INTERLACED(mb_type))
  1495. av_log(avctx, AV_LOG_DEBUG, "=");
  1496. else
  1497. av_log(avctx, AV_LOG_DEBUG, " ");
  1498. }
  1499. }
  1500. av_log(avctx, AV_LOG_DEBUG, "\n");
  1501. }
  1502. }
  1503. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1504. (avctx->debug_mv)) {
  1505. int mb_y;
  1506. int i;
  1507. int h_chroma_shift, v_chroma_shift, block_height;
  1508. #if FF_API_VISMV
  1509. const int shift = 1 + quarter_sample;
  1510. uint8_t *ptr;
  1511. const int width = avctx->width;
  1512. const int height = avctx->height;
  1513. #endif
  1514. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1515. const int mv_stride = (mb_width << mv_sample_log2) +
  1516. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1517. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1518. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1519. av_frame_make_writable(pict);
  1520. pict->opaque = NULL;
  1521. #if FF_API_VISMV
  1522. ptr = pict->data[0];
  1523. #endif
  1524. block_height = 16 >> v_chroma_shift;
  1525. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1526. int mb_x;
  1527. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1528. const int mb_index = mb_x + mb_y * mb_stride;
  1529. #if FF_API_VISMV
  1530. if ((avctx->debug_mv) && motion_val[0]) {
  1531. int type;
  1532. for (type = 0; type < 3; type++) {
  1533. int direction = 0;
  1534. switch (type) {
  1535. case 0:
  1536. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1537. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1538. continue;
  1539. direction = 0;
  1540. break;
  1541. case 1:
  1542. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1543. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1544. continue;
  1545. direction = 0;
  1546. break;
  1547. case 2:
  1548. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1549. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1550. continue;
  1551. direction = 1;
  1552. break;
  1553. }
  1554. if (!USES_LIST(mbtype_table[mb_index], direction))
  1555. continue;
  1556. if (IS_8X8(mbtype_table[mb_index])) {
  1557. int i;
  1558. for (i = 0; i < 4; i++) {
  1559. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1560. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1561. int xy = (mb_x * 2 + (i & 1) +
  1562. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1563. int mx = (motion_val[direction][xy][0] >> shift) + sx;
  1564. int my = (motion_val[direction][xy][1] >> shift) + sy;
  1565. draw_arrow(ptr, sx, sy, mx, my, width,
  1566. height, pict->linesize[0], 100, 0, direction);
  1567. }
  1568. } else if (IS_16X8(mbtype_table[mb_index])) {
  1569. int i;
  1570. for (i = 0; i < 2; i++) {
  1571. int sx = mb_x * 16 + 8;
  1572. int sy = mb_y * 16 + 4 + 8 * i;
  1573. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1574. int mx = (motion_val[direction][xy][0] >> shift);
  1575. int my = (motion_val[direction][xy][1] >> shift);
  1576. if (IS_INTERLACED(mbtype_table[mb_index]))
  1577. my *= 2;
  1578. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1579. height, pict->linesize[0], 100, 0, direction);
  1580. }
  1581. } else if (IS_8X16(mbtype_table[mb_index])) {
  1582. int i;
  1583. for (i = 0; i < 2; i++) {
  1584. int sx = mb_x * 16 + 4 + 8 * i;
  1585. int sy = mb_y * 16 + 8;
  1586. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1587. int mx = motion_val[direction][xy][0] >> shift;
  1588. int my = motion_val[direction][xy][1] >> shift;
  1589. if (IS_INTERLACED(mbtype_table[mb_index]))
  1590. my *= 2;
  1591. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1592. height, pict->linesize[0], 100, 0, direction);
  1593. }
  1594. } else {
  1595. int sx= mb_x * 16 + 8;
  1596. int sy= mb_y * 16 + 8;
  1597. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1598. int mx= (motion_val[direction][xy][0]>>shift) + sx;
  1599. int my= (motion_val[direction][xy][1]>>shift) + sy;
  1600. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100, 0, direction);
  1601. }
  1602. }
  1603. }
  1604. #endif
  1605. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1606. uint64_t c = (qscale_table[mb_index] * 128 / 31) *
  1607. 0x0101010101010101ULL;
  1608. int y;
  1609. for (y = 0; y < block_height; y++) {
  1610. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1611. (block_height * mb_y + y) *
  1612. pict->linesize[1]) = c;
  1613. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1614. (block_height * mb_y + y) *
  1615. pict->linesize[2]) = c;
  1616. }
  1617. }
  1618. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1619. motion_val[0]) {
  1620. int mb_type = mbtype_table[mb_index];
  1621. uint64_t u,v;
  1622. int y;
  1623. #define COLOR(theta, r) \
  1624. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1625. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1626. u = v = 128;
  1627. if (IS_PCM(mb_type)) {
  1628. COLOR(120, 48)
  1629. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1630. IS_INTRA16x16(mb_type)) {
  1631. COLOR(30, 48)
  1632. } else if (IS_INTRA4x4(mb_type)) {
  1633. COLOR(90, 48)
  1634. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1635. // COLOR(120, 48)
  1636. } else if (IS_DIRECT(mb_type)) {
  1637. COLOR(150, 48)
  1638. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1639. COLOR(170, 48)
  1640. } else if (IS_GMC(mb_type)) {
  1641. COLOR(190, 48)
  1642. } else if (IS_SKIP(mb_type)) {
  1643. // COLOR(180, 48)
  1644. } else if (!USES_LIST(mb_type, 1)) {
  1645. COLOR(240, 48)
  1646. } else if (!USES_LIST(mb_type, 0)) {
  1647. COLOR(0, 48)
  1648. } else {
  1649. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1650. COLOR(300,48)
  1651. }
  1652. u *= 0x0101010101010101ULL;
  1653. v *= 0x0101010101010101ULL;
  1654. for (y = 0; y < block_height; y++) {
  1655. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1656. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1657. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1658. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1659. }
  1660. // segmentation
  1661. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1662. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1663. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1664. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1665. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1666. }
  1667. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1668. for (y = 0; y < 16; y++)
  1669. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1670. pict->linesize[0]] ^= 0x80;
  1671. }
  1672. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1673. int dm = 1 << (mv_sample_log2 - 2);
  1674. for (i = 0; i < 4; i++) {
  1675. int sx = mb_x * 16 + 8 * (i & 1);
  1676. int sy = mb_y * 16 + 8 * (i >> 1);
  1677. int xy = (mb_x * 2 + (i & 1) +
  1678. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1679. // FIXME bidir
  1680. int32_t *mv = (int32_t *) &motion_val[0][xy];
  1681. if (mv[0] != mv[dm] ||
  1682. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1683. for (y = 0; y < 8; y++)
  1684. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1685. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1686. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1687. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1688. }
  1689. }
  1690. if (IS_INTERLACED(mb_type) &&
  1691. avctx->codec->id == AV_CODEC_ID_H264) {
  1692. // hmm
  1693. }
  1694. }
  1695. if (mbskip_table)
  1696. mbskip_table[mb_index] = 0;
  1697. }
  1698. }
  1699. }
  1700. }
  1701. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1702. {
  1703. ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
  1704. p->qscale_table, p->motion_val, &s->low_delay,
  1705. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1706. }
  1707. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1708. {
  1709. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1710. int offset = 2*s->mb_stride + 1;
  1711. if(!ref)
  1712. return AVERROR(ENOMEM);
  1713. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1714. ref->size -= offset;
  1715. ref->data += offset;
  1716. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1717. }
  1718. static inline int hpel_motion_lowres(MpegEncContext *s,
  1719. uint8_t *dest, uint8_t *src,
  1720. int field_based, int field_select,
  1721. int src_x, int src_y,
  1722. int width, int height, ptrdiff_t stride,
  1723. int h_edge_pos, int v_edge_pos,
  1724. int w, int h, h264_chroma_mc_func *pix_op,
  1725. int motion_x, int motion_y)
  1726. {
  1727. const int lowres = s->avctx->lowres;
  1728. const int op_index = FFMIN(lowres, 3);
  1729. const int s_mask = (2 << lowres) - 1;
  1730. int emu = 0;
  1731. int sx, sy;
  1732. if (s->quarter_sample) {
  1733. motion_x /= 2;
  1734. motion_y /= 2;
  1735. }
  1736. sx = motion_x & s_mask;
  1737. sy = motion_y & s_mask;
  1738. src_x += motion_x >> lowres + 1;
  1739. src_y += motion_y >> lowres + 1;
  1740. src += src_y * stride + src_x;
  1741. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1742. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1743. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
  1744. s->linesize, s->linesize,
  1745. w + 1, (h + 1) << field_based,
  1746. src_x, src_y << field_based,
  1747. h_edge_pos, v_edge_pos);
  1748. src = s->sc.edge_emu_buffer;
  1749. emu = 1;
  1750. }
  1751. sx = (sx << 2) >> lowres;
  1752. sy = (sy << 2) >> lowres;
  1753. if (field_select)
  1754. src += s->linesize;
  1755. pix_op[op_index](dest, src, stride, h, sx, sy);
  1756. return emu;
  1757. }
  1758. /* apply one mpeg motion vector to the three components */
  1759. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1760. uint8_t *dest_y,
  1761. uint8_t *dest_cb,
  1762. uint8_t *dest_cr,
  1763. int field_based,
  1764. int bottom_field,
  1765. int field_select,
  1766. uint8_t **ref_picture,
  1767. h264_chroma_mc_func *pix_op,
  1768. int motion_x, int motion_y,
  1769. int h, int mb_y)
  1770. {
  1771. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1772. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  1773. ptrdiff_t uvlinesize, linesize;
  1774. const int lowres = s->avctx->lowres;
  1775. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  1776. const int block_s = 8>>lowres;
  1777. const int s_mask = (2 << lowres) - 1;
  1778. const int h_edge_pos = s->h_edge_pos >> lowres;
  1779. const int v_edge_pos = s->v_edge_pos >> lowres;
  1780. linesize = s->current_picture.f->linesize[0] << field_based;
  1781. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  1782. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1783. if (s->quarter_sample) {
  1784. motion_x /= 2;
  1785. motion_y /= 2;
  1786. }
  1787. if(field_based){
  1788. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1789. }
  1790. sx = motion_x & s_mask;
  1791. sy = motion_y & s_mask;
  1792. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1793. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1794. if (s->out_format == FMT_H263) {
  1795. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1796. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1797. uvsrc_x = src_x >> 1;
  1798. uvsrc_y = src_y >> 1;
  1799. } else if (s->out_format == FMT_H261) {
  1800. // even chroma mv's are full pel in H261
  1801. mx = motion_x / 4;
  1802. my = motion_y / 4;
  1803. uvsx = (2 * mx) & s_mask;
  1804. uvsy = (2 * my) & s_mask;
  1805. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1806. uvsrc_y = mb_y * block_s + (my >> lowres);
  1807. } else {
  1808. if(s->chroma_y_shift){
  1809. mx = motion_x / 2;
  1810. my = motion_y / 2;
  1811. uvsx = mx & s_mask;
  1812. uvsy = my & s_mask;
  1813. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1814. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1815. } else {
  1816. if(s->chroma_x_shift){
  1817. //Chroma422
  1818. mx = motion_x / 2;
  1819. uvsx = mx & s_mask;
  1820. uvsy = motion_y & s_mask;
  1821. uvsrc_y = src_y;
  1822. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1823. } else {
  1824. //Chroma444
  1825. uvsx = motion_x & s_mask;
  1826. uvsy = motion_y & s_mask;
  1827. uvsrc_x = src_x;
  1828. uvsrc_y = src_y;
  1829. }
  1830. }
  1831. }
  1832. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1833. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1834. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1835. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  1836. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1837. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
  1838. linesize >> field_based, linesize >> field_based,
  1839. 17, 17 + field_based,
  1840. src_x, src_y << field_based, h_edge_pos,
  1841. v_edge_pos);
  1842. ptr_y = s->sc.edge_emu_buffer;
  1843. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1844. uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
  1845. uint8_t *vbuf =ubuf + 9 * s->uvlinesize;
  1846. s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
  1847. uvlinesize >> field_based, uvlinesize >> field_based,
  1848. 9, 9 + field_based,
  1849. uvsrc_x, uvsrc_y << field_based,
  1850. h_edge_pos >> 1, v_edge_pos >> 1);
  1851. s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
  1852. uvlinesize >> field_based,uvlinesize >> field_based,
  1853. 9, 9 + field_based,
  1854. uvsrc_x, uvsrc_y << field_based,
  1855. h_edge_pos >> 1, v_edge_pos >> 1);
  1856. ptr_cb = ubuf;
  1857. ptr_cr = vbuf;
  1858. }
  1859. }
  1860. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
  1861. if (bottom_field) {
  1862. dest_y += s->linesize;
  1863. dest_cb += s->uvlinesize;
  1864. dest_cr += s->uvlinesize;
  1865. }
  1866. if (field_select) {
  1867. ptr_y += s->linesize;
  1868. ptr_cb += s->uvlinesize;
  1869. ptr_cr += s->uvlinesize;
  1870. }
  1871. sx = (sx << 2) >> lowres;
  1872. sy = (sy << 2) >> lowres;
  1873. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1874. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1875. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  1876. uvsx = (uvsx << 2) >> lowres;
  1877. uvsy = (uvsy << 2) >> lowres;
  1878. if (hc) {
  1879. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  1880. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  1881. }
  1882. }
  1883. // FIXME h261 lowres loop filter
  1884. }
  1885. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1886. uint8_t *dest_cb, uint8_t *dest_cr,
  1887. uint8_t **ref_picture,
  1888. h264_chroma_mc_func * pix_op,
  1889. int mx, int my)
  1890. {
  1891. const int lowres = s->avctx->lowres;
  1892. const int op_index = FFMIN(lowres, 3);
  1893. const int block_s = 8 >> lowres;
  1894. const int s_mask = (2 << lowres) - 1;
  1895. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1896. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1897. int emu = 0, src_x, src_y, sx, sy;
  1898. ptrdiff_t offset;
  1899. uint8_t *ptr;
  1900. if (s->quarter_sample) {
  1901. mx /= 2;
  1902. my /= 2;
  1903. }
  1904. /* In case of 8X8, we construct a single chroma motion vector
  1905. with a special rounding */
  1906. mx = ff_h263_round_chroma(mx);
  1907. my = ff_h263_round_chroma(my);
  1908. sx = mx & s_mask;
  1909. sy = my & s_mask;
  1910. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  1911. src_y = s->mb_y * block_s + (my >> lowres + 1);
  1912. offset = src_y * s->uvlinesize + src_x;
  1913. ptr = ref_picture[1] + offset;
  1914. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  1915. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  1916. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1917. s->uvlinesize, s->uvlinesize,
  1918. 9, 9,
  1919. src_x, src_y, h_edge_pos, v_edge_pos);
  1920. ptr = s->sc.edge_emu_buffer;
  1921. emu = 1;
  1922. }
  1923. sx = (sx << 2) >> lowres;
  1924. sy = (sy << 2) >> lowres;
  1925. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1926. ptr = ref_picture[2] + offset;
  1927. if (emu) {
  1928. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1929. s->uvlinesize, s->uvlinesize,
  1930. 9, 9,
  1931. src_x, src_y, h_edge_pos, v_edge_pos);
  1932. ptr = s->sc.edge_emu_buffer;
  1933. }
  1934. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1935. }
  1936. /**
  1937. * motion compensation of a single macroblock
  1938. * @param s context
  1939. * @param dest_y luma destination pointer
  1940. * @param dest_cb chroma cb/u destination pointer
  1941. * @param dest_cr chroma cr/v destination pointer
  1942. * @param dir direction (0->forward, 1->backward)
  1943. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1944. * @param pix_op halfpel motion compensation function (average or put normally)
  1945. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1946. */
  1947. static inline void MPV_motion_lowres(MpegEncContext *s,
  1948. uint8_t *dest_y, uint8_t *dest_cb,
  1949. uint8_t *dest_cr,
  1950. int dir, uint8_t **ref_picture,
  1951. h264_chroma_mc_func *pix_op)
  1952. {
  1953. int mx, my;
  1954. int mb_x, mb_y, i;
  1955. const int lowres = s->avctx->lowres;
  1956. const int block_s = 8 >>lowres;
  1957. mb_x = s->mb_x;
  1958. mb_y = s->mb_y;
  1959. switch (s->mv_type) {
  1960. case MV_TYPE_16X16:
  1961. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1962. 0, 0, 0,
  1963. ref_picture, pix_op,
  1964. s->mv[dir][0][0], s->mv[dir][0][1],
  1965. 2 * block_s, mb_y);
  1966. break;
  1967. case MV_TYPE_8X8:
  1968. mx = 0;
  1969. my = 0;
  1970. for (i = 0; i < 4; i++) {
  1971. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  1972. s->linesize) * block_s,
  1973. ref_picture[0], 0, 0,
  1974. (2 * mb_x + (i & 1)) * block_s,
  1975. (2 * mb_y + (i >> 1)) * block_s,
  1976. s->width, s->height, s->linesize,
  1977. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1978. block_s, block_s, pix_op,
  1979. s->mv[dir][i][0], s->mv[dir][i][1]);
  1980. mx += s->mv[dir][i][0];
  1981. my += s->mv[dir][i][1];
  1982. }
  1983. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
  1984. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  1985. pix_op, mx, my);
  1986. break;
  1987. case MV_TYPE_FIELD:
  1988. if (s->picture_structure == PICT_FRAME) {
  1989. /* top field */
  1990. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1991. 1, 0, s->field_select[dir][0],
  1992. ref_picture, pix_op,
  1993. s->mv[dir][0][0], s->mv[dir][0][1],
  1994. block_s, mb_y);
  1995. /* bottom field */
  1996. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1997. 1, 1, s->field_select[dir][1],
  1998. ref_picture, pix_op,
  1999. s->mv[dir][1][0], s->mv[dir][1][1],
  2000. block_s, mb_y);
  2001. } else {
  2002. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2003. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2004. ref_picture = s->current_picture_ptr->f->data;
  2005. }
  2006. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2007. 0, 0, s->field_select[dir][0],
  2008. ref_picture, pix_op,
  2009. s->mv[dir][0][0],
  2010. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2011. }
  2012. break;
  2013. case MV_TYPE_16X8:
  2014. for (i = 0; i < 2; i++) {
  2015. uint8_t **ref2picture;
  2016. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2017. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2018. ref2picture = ref_picture;
  2019. } else {
  2020. ref2picture = s->current_picture_ptr->f->data;
  2021. }
  2022. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2023. 0, 0, s->field_select[dir][i],
  2024. ref2picture, pix_op,
  2025. s->mv[dir][i][0], s->mv[dir][i][1] +
  2026. 2 * block_s * i, block_s, mb_y >> 1);
  2027. dest_y += 2 * block_s * s->linesize;
  2028. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2029. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2030. }
  2031. break;
  2032. case MV_TYPE_DMV:
  2033. if (s->picture_structure == PICT_FRAME) {
  2034. for (i = 0; i < 2; i++) {
  2035. int j;
  2036. for (j = 0; j < 2; j++) {
  2037. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2038. 1, j, j ^ i,
  2039. ref_picture, pix_op,
  2040. s->mv[dir][2 * i + j][0],
  2041. s->mv[dir][2 * i + j][1],
  2042. block_s, mb_y);
  2043. }
  2044. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2045. }
  2046. } else {
  2047. for (i = 0; i < 2; i++) {
  2048. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2049. 0, 0, s->picture_structure != i + 1,
  2050. ref_picture, pix_op,
  2051. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2052. 2 * block_s, mb_y >> 1);
  2053. // after put we make avg of the same block
  2054. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2055. // opposite parity is always in the same
  2056. // frame if this is second field
  2057. if (!s->first_field) {
  2058. ref_picture = s->current_picture_ptr->f->data;
  2059. }
  2060. }
  2061. }
  2062. break;
  2063. default:
  2064. av_assert2(0);
  2065. }
  2066. }
  2067. /**
  2068. * find the lowest MB row referenced in the MVs
  2069. */
  2070. static int lowest_referenced_row(MpegEncContext *s, int dir)
  2071. {
  2072. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2073. int my, off, i, mvs;
  2074. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2075. goto unhandled;
  2076. switch (s->mv_type) {
  2077. case MV_TYPE_16X16:
  2078. mvs = 1;
  2079. break;
  2080. case MV_TYPE_16X8:
  2081. mvs = 2;
  2082. break;
  2083. case MV_TYPE_8X8:
  2084. mvs = 4;
  2085. break;
  2086. default:
  2087. goto unhandled;
  2088. }
  2089. for (i = 0; i < mvs; i++) {
  2090. my = s->mv[dir][i][1];
  2091. my_max = FFMAX(my_max, my);
  2092. my_min = FFMIN(my_min, my);
  2093. }
  2094. off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
  2095. return av_clip(s->mb_y + off, 0, s->mb_height - 1);
  2096. unhandled:
  2097. return s->mb_height-1;
  2098. }
  2099. /* put block[] to dest[] */
  2100. static inline void put_dct(MpegEncContext *s,
  2101. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2102. {
  2103. s->dct_unquantize_intra(s, block, i, qscale);
  2104. s->idsp.idct_put(dest, line_size, block);
  2105. }
  2106. /* add block[] to dest[] */
  2107. static inline void add_dct(MpegEncContext *s,
  2108. int16_t *block, int i, uint8_t *dest, int line_size)
  2109. {
  2110. if (s->block_last_index[i] >= 0) {
  2111. s->idsp.idct_add(dest, line_size, block);
  2112. }
  2113. }
  2114. static inline void add_dequant_dct(MpegEncContext *s,
  2115. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2116. {
  2117. if (s->block_last_index[i] >= 0) {
  2118. s->dct_unquantize_inter(s, block, i, qscale);
  2119. s->idsp.idct_add(dest, line_size, block);
  2120. }
  2121. }
  2122. /**
  2123. * Clean dc, ac, coded_block for the current non-intra MB.
  2124. */
  2125. void ff_clean_intra_table_entries(MpegEncContext *s)
  2126. {
  2127. int wrap = s->b8_stride;
  2128. int xy = s->block_index[0];
  2129. s->dc_val[0][xy ] =
  2130. s->dc_val[0][xy + 1 ] =
  2131. s->dc_val[0][xy + wrap] =
  2132. s->dc_val[0][xy + 1 + wrap] = 1024;
  2133. /* ac pred */
  2134. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2135. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2136. if (s->msmpeg4_version>=3) {
  2137. s->coded_block[xy ] =
  2138. s->coded_block[xy + 1 ] =
  2139. s->coded_block[xy + wrap] =
  2140. s->coded_block[xy + 1 + wrap] = 0;
  2141. }
  2142. /* chroma */
  2143. wrap = s->mb_stride;
  2144. xy = s->mb_x + s->mb_y * wrap;
  2145. s->dc_val[1][xy] =
  2146. s->dc_val[2][xy] = 1024;
  2147. /* ac pred */
  2148. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2149. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2150. s->mbintra_table[xy]= 0;
  2151. }
  2152. /* generic function called after a macroblock has been parsed by the
  2153. decoder or after it has been encoded by the encoder.
  2154. Important variables used:
  2155. s->mb_intra : true if intra macroblock
  2156. s->mv_dir : motion vector direction
  2157. s->mv_type : motion vector type
  2158. s->mv : motion vector
  2159. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2160. */
  2161. static av_always_inline
  2162. void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2163. int lowres_flag, int is_mpeg12)
  2164. {
  2165. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2166. if (CONFIG_XVMC &&
  2167. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2168. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2169. return;
  2170. }
  2171. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2172. /* print DCT coefficients */
  2173. int i,j;
  2174. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2175. for(i=0; i<6; i++){
  2176. for(j=0; j<64; j++){
  2177. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  2178. block[i][s->idsp.idct_permutation[j]]);
  2179. }
  2180. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2181. }
  2182. }
  2183. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2184. /* update DC predictors for P macroblocks */
  2185. if (!s->mb_intra) {
  2186. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2187. if(s->mbintra_table[mb_xy])
  2188. ff_clean_intra_table_entries(s);
  2189. } else {
  2190. s->last_dc[0] =
  2191. s->last_dc[1] =
  2192. s->last_dc[2] = 128 << s->intra_dc_precision;
  2193. }
  2194. }
  2195. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2196. s->mbintra_table[mb_xy]=1;
  2197. if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor ||
  2198. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  2199. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  2200. uint8_t *dest_y, *dest_cb, *dest_cr;
  2201. int dct_linesize, dct_offset;
  2202. op_pixels_func (*op_pix)[4];
  2203. qpel_mc_func (*op_qpix)[16];
  2204. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2205. const int uvlinesize = s->current_picture.f->linesize[1];
  2206. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2207. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2208. /* avoid copy if macroblock skipped in last frame too */
  2209. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2210. if(!s->encoding){
  2211. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2212. if (s->mb_skipped) {
  2213. s->mb_skipped= 0;
  2214. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2215. *mbskip_ptr = 1;
  2216. } else if(!s->current_picture.reference) {
  2217. *mbskip_ptr = 1;
  2218. } else{
  2219. *mbskip_ptr = 0; /* not skipped */
  2220. }
  2221. }
  2222. dct_linesize = linesize << s->interlaced_dct;
  2223. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2224. if(readable){
  2225. dest_y= s->dest[0];
  2226. dest_cb= s->dest[1];
  2227. dest_cr= s->dest[2];
  2228. }else{
  2229. dest_y = s->sc.b_scratchpad;
  2230. dest_cb= s->sc.b_scratchpad+16*linesize;
  2231. dest_cr= s->sc.b_scratchpad+32*linesize;
  2232. }
  2233. if (!s->mb_intra) {
  2234. /* motion handling */
  2235. /* decoding or more than one mb_type (MC was already done otherwise) */
  2236. if(!s->encoding){
  2237. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2238. if (s->mv_dir & MV_DIR_FORWARD) {
  2239. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2240. lowest_referenced_row(s, 0),
  2241. 0);
  2242. }
  2243. if (s->mv_dir & MV_DIR_BACKWARD) {
  2244. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2245. lowest_referenced_row(s, 1),
  2246. 0);
  2247. }
  2248. }
  2249. if(lowres_flag){
  2250. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2251. if (s->mv_dir & MV_DIR_FORWARD) {
  2252. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
  2253. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2254. }
  2255. if (s->mv_dir & MV_DIR_BACKWARD) {
  2256. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
  2257. }
  2258. }else{
  2259. op_qpix = s->me.qpel_put;
  2260. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2261. op_pix = s->hdsp.put_pixels_tab;
  2262. }else{
  2263. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2264. }
  2265. if (s->mv_dir & MV_DIR_FORWARD) {
  2266. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  2267. op_pix = s->hdsp.avg_pixels_tab;
  2268. op_qpix= s->me.qpel_avg;
  2269. }
  2270. if (s->mv_dir & MV_DIR_BACKWARD) {
  2271. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  2272. }
  2273. }
  2274. }
  2275. /* skip dequant / idct if we are really late ;) */
  2276. if(s->avctx->skip_idct){
  2277. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2278. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2279. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2280. goto skip_idct;
  2281. }
  2282. /* add dct residue */
  2283. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2284. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2285. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2286. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2287. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2288. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2289. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  2290. if (s->chroma_y_shift){
  2291. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2292. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2293. }else{
  2294. dct_linesize >>= 1;
  2295. dct_offset >>=1;
  2296. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2297. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2298. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2299. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2300. }
  2301. }
  2302. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2303. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2304. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2305. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2306. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2307. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  2308. if(s->chroma_y_shift){//Chroma420
  2309. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2310. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2311. }else{
  2312. //chroma422
  2313. dct_linesize = uvlinesize << s->interlaced_dct;
  2314. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2315. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2316. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2317. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2318. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2319. if(!s->chroma_x_shift){//Chroma444
  2320. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2321. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2322. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2323. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2324. }
  2325. }
  2326. }//fi gray
  2327. }
  2328. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2329. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2330. }
  2331. } else {
  2332. /* dct only in intra block */
  2333. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2334. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2335. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2336. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2337. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2338. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  2339. if(s->chroma_y_shift){
  2340. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2341. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2342. }else{
  2343. dct_offset >>=1;
  2344. dct_linesize >>=1;
  2345. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2346. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2347. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2348. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2349. }
  2350. }
  2351. }else{
  2352. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  2353. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2354. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  2355. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2356. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  2357. if(s->chroma_y_shift){
  2358. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  2359. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  2360. }else{
  2361. dct_linesize = uvlinesize << s->interlaced_dct;
  2362. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2363. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  2364. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  2365. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2366. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2367. if(!s->chroma_x_shift){//Chroma444
  2368. s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2369. s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2370. s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2371. s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2372. }
  2373. }
  2374. }//gray
  2375. }
  2376. }
  2377. skip_idct:
  2378. if(!readable){
  2379. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2380. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  2381. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2382. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2383. }
  2384. }
  2385. }
  2386. }
  2387. void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
  2388. {
  2389. #if !CONFIG_SMALL
  2390. if(s->out_format == FMT_MPEG1) {
  2391. if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 1);
  2392. else mpv_decode_mb_internal(s, block, 0, 1);
  2393. } else
  2394. #endif
  2395. if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 0);
  2396. else mpv_decode_mb_internal(s, block, 0, 0);
  2397. }
  2398. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2399. {
  2400. ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
  2401. s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
  2402. s->first_field, s->low_delay);
  2403. }
  2404. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2405. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  2406. const int uvlinesize = s->current_picture.f->linesize[1];
  2407. const int mb_size= 4 - s->avctx->lowres;
  2408. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2409. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2410. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2411. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2412. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2413. 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;
  2414. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2415. s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << mb_size);
  2416. s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
  2417. s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
  2418. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2419. {
  2420. if(s->picture_structure==PICT_FRAME){
  2421. s->dest[0] += s->mb_y * linesize << mb_size;
  2422. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2423. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2424. }else{
  2425. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2426. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2427. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2428. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2429. }
  2430. }
  2431. }
  2432. void ff_mpeg_flush(AVCodecContext *avctx){
  2433. int i;
  2434. MpegEncContext *s = avctx->priv_data;
  2435. if (!s || !s->picture)
  2436. return;
  2437. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2438. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  2439. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2440. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  2441. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  2442. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  2443. s->mb_x= s->mb_y= 0;
  2444. s->closed_gop= 0;
  2445. s->parse_context.state= -1;
  2446. s->parse_context.frame_start_found= 0;
  2447. s->parse_context.overread= 0;
  2448. s->parse_context.overread_index= 0;
  2449. s->parse_context.index= 0;
  2450. s->parse_context.last_index= 0;
  2451. s->bitstream_buffer_size=0;
  2452. s->pp_time=0;
  2453. }
  2454. /**
  2455. * set qscale and update qscale dependent variables.
  2456. */
  2457. void ff_set_qscale(MpegEncContext * s, int qscale)
  2458. {
  2459. if (qscale < 1)
  2460. qscale = 1;
  2461. else if (qscale > 31)
  2462. qscale = 31;
  2463. s->qscale = qscale;
  2464. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2465. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2466. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2467. }
  2468. void ff_mpv_report_decode_progress(MpegEncContext *s)
  2469. {
  2470. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2471. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2472. }