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.

2811 lines
104KB

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