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