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.

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