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.

2821 lines
105KB

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