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.

2816 lines
105KB

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