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.

2349 lines
86KB

  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 "avcodec.h"
  34. #include "blockdsp.h"
  35. #include "h264chroma.h"
  36. #include "idctdsp.h"
  37. #include "internal.h"
  38. #include "mathops.h"
  39. #include "mpeg_er.h"
  40. #include "mpegutils.h"
  41. #include "mpegvideo.h"
  42. #include "mpegvideodata.h"
  43. #include "mjpegenc.h"
  44. #include "msmpeg4.h"
  45. #include "qpeldsp.h"
  46. #include "thread.h"
  47. #include "wmv2.h"
  48. #include <limits.h>
  49. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  50. int16_t *block, int n, int qscale)
  51. {
  52. int i, level, nCoeffs;
  53. const uint16_t *quant_matrix;
  54. nCoeffs= s->block_last_index[n];
  55. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  56. /* XXX: only MPEG-1 */
  57. quant_matrix = s->intra_matrix;
  58. for(i=1;i<=nCoeffs;i++) {
  59. int j= s->intra_scantable.permutated[i];
  60. level = block[j];
  61. if (level) {
  62. if (level < 0) {
  63. level = -level;
  64. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  65. level = (level - 1) | 1;
  66. level = -level;
  67. } else {
  68. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  69. level = (level - 1) | 1;
  70. }
  71. block[j] = level;
  72. }
  73. }
  74. }
  75. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  76. int16_t *block, int n, int qscale)
  77. {
  78. int i, level, nCoeffs;
  79. const uint16_t *quant_matrix;
  80. nCoeffs= s->block_last_index[n];
  81. quant_matrix = s->inter_matrix;
  82. for(i=0; i<=nCoeffs; i++) {
  83. int j= s->intra_scantable.permutated[i];
  84. level = block[j];
  85. if (level) {
  86. if (level < 0) {
  87. level = -level;
  88. level = (((level << 1) + 1) * qscale *
  89. ((int) (quant_matrix[j]))) >> 4;
  90. level = (level - 1) | 1;
  91. level = -level;
  92. } else {
  93. level = (((level << 1) + 1) * qscale *
  94. ((int) (quant_matrix[j]))) >> 4;
  95. level = (level - 1) | 1;
  96. }
  97. block[j] = level;
  98. }
  99. }
  100. }
  101. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  102. int16_t *block, int n, int qscale)
  103. {
  104. int i, level, nCoeffs;
  105. const uint16_t *quant_matrix;
  106. if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
  107. else qscale <<= 1;
  108. if(s->alternate_scan) nCoeffs= 63;
  109. else nCoeffs= s->block_last_index[n];
  110. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  111. quant_matrix = s->intra_matrix;
  112. for(i=1;i<=nCoeffs;i++) {
  113. int j= s->intra_scantable.permutated[i];
  114. level = block[j];
  115. if (level) {
  116. if (level < 0) {
  117. level = -level;
  118. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  119. level = -level;
  120. } else {
  121. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  122. }
  123. block[j] = level;
  124. }
  125. }
  126. }
  127. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  128. int16_t *block, int n, int qscale)
  129. {
  130. int i, level, nCoeffs;
  131. const uint16_t *quant_matrix;
  132. int sum=-1;
  133. if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
  134. else qscale <<= 1;
  135. if(s->alternate_scan) nCoeffs= 63;
  136. else nCoeffs= s->block_last_index[n];
  137. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  138. sum += block[0];
  139. quant_matrix = s->intra_matrix;
  140. for(i=1;i<=nCoeffs;i++) {
  141. int j= s->intra_scantable.permutated[i];
  142. level = block[j];
  143. if (level) {
  144. if (level < 0) {
  145. level = -level;
  146. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  147. level = -level;
  148. } else {
  149. level = (int)(level * qscale * quant_matrix[j]) >> 4;
  150. }
  151. block[j] = level;
  152. sum+=level;
  153. }
  154. }
  155. block[63]^=sum&1;
  156. }
  157. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  158. int16_t *block, int n, int qscale)
  159. {
  160. int i, level, nCoeffs;
  161. const uint16_t *quant_matrix;
  162. int sum=-1;
  163. if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
  164. else qscale <<= 1;
  165. if(s->alternate_scan) nCoeffs= 63;
  166. else nCoeffs= s->block_last_index[n];
  167. quant_matrix = s->inter_matrix;
  168. for(i=0; i<=nCoeffs; i++) {
  169. int j= s->intra_scantable.permutated[i];
  170. level = block[j];
  171. if (level) {
  172. if (level < 0) {
  173. level = -level;
  174. level = (((level << 1) + 1) * qscale *
  175. ((int) (quant_matrix[j]))) >> 5;
  176. level = -level;
  177. } else {
  178. level = (((level << 1) + 1) * qscale *
  179. ((int) (quant_matrix[j]))) >> 5;
  180. }
  181. block[j] = level;
  182. sum+=level;
  183. }
  184. }
  185. block[63]^=sum&1;
  186. }
  187. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  188. int16_t *block, int n, int qscale)
  189. {
  190. int i, level, qmul, qadd;
  191. int nCoeffs;
  192. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  193. qmul = qscale << 1;
  194. if (!s->h263_aic) {
  195. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  196. qadd = (qscale - 1) | 1;
  197. }else{
  198. qadd = 0;
  199. }
  200. if(s->ac_pred)
  201. nCoeffs=63;
  202. else
  203. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  204. for(i=1; i<=nCoeffs; i++) {
  205. level = block[i];
  206. if (level) {
  207. if (level < 0) {
  208. level = level * qmul - qadd;
  209. } else {
  210. level = level * qmul + qadd;
  211. }
  212. block[i] = level;
  213. }
  214. }
  215. }
  216. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  217. int16_t *block, int n, int qscale)
  218. {
  219. int i, level, qmul, qadd;
  220. int nCoeffs;
  221. av_assert2(s->block_last_index[n]>=0);
  222. qadd = (qscale - 1) | 1;
  223. qmul = qscale << 1;
  224. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  225. for(i=0; i<=nCoeffs; i++) {
  226. level = block[i];
  227. if (level) {
  228. if (level < 0) {
  229. level = level * qmul - qadd;
  230. } else {
  231. level = level * qmul + qadd;
  232. }
  233. block[i] = level;
  234. }
  235. }
  236. }
  237. static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
  238. {
  239. while(h--)
  240. memset(dst + h*linesize, 128, 16);
  241. }
  242. static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
  243. {
  244. while(h--)
  245. memset(dst + h*linesize, 128, 8);
  246. }
  247. /* init common dct for both encoder and decoder */
  248. static av_cold int dct_init(MpegEncContext *s)
  249. {
  250. ff_blockdsp_init(&s->bdsp, s->avctx);
  251. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  252. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  253. ff_mpegvideodsp_init(&s->mdsp);
  254. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  255. if (s->avctx->debug & FF_DEBUG_NOMC) {
  256. int i;
  257. for (i=0; i<4; i++) {
  258. s->hdsp.avg_pixels_tab[0][i] = gray16;
  259. s->hdsp.put_pixels_tab[0][i] = gray16;
  260. s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
  261. s->hdsp.avg_pixels_tab[1][i] = gray8;
  262. s->hdsp.put_pixels_tab[1][i] = gray8;
  263. s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
  264. }
  265. }
  266. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  267. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  268. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  269. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  270. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  271. if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
  272. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  273. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  274. if (HAVE_INTRINSICS_NEON)
  275. ff_mpv_common_init_neon(s);
  276. if (ARCH_ALPHA)
  277. ff_mpv_common_init_axp(s);
  278. if (ARCH_ARM)
  279. ff_mpv_common_init_arm(s);
  280. if (ARCH_PPC)
  281. ff_mpv_common_init_ppc(s);
  282. if (ARCH_X86)
  283. ff_mpv_common_init_x86(s);
  284. if (ARCH_MIPS)
  285. ff_mpv_common_init_mips(s);
  286. return 0;
  287. }
  288. av_cold void ff_mpv_idct_init(MpegEncContext *s)
  289. {
  290. if (s->codec_id == AV_CODEC_ID_MPEG4)
  291. s->idsp.mpeg4_studio_profile = s->studio_profile;
  292. ff_idctdsp_init(&s->idsp, s->avctx);
  293. /* load & permutate scantables
  294. * note: only wmv uses different ones
  295. */
  296. if (s->alternate_scan) {
  297. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  298. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  299. } else {
  300. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  301. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  302. }
  303. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  304. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  305. }
  306. static int alloc_picture(MpegEncContext *s, Picture *pic)
  307. {
  308. return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, 0,
  309. s->chroma_x_shift, s->chroma_y_shift, s->out_format,
  310. s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
  311. &s->linesize, &s->uvlinesize);
  312. }
  313. static int init_duplicate_context(MpegEncContext *s)
  314. {
  315. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  316. int c_size = s->mb_stride * (s->mb_height + 1);
  317. int yc_size = y_size + 2 * c_size;
  318. int i;
  319. if (s->mb_height & 1)
  320. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  321. s->sc.edge_emu_buffer =
  322. s->me.scratchpad =
  323. s->me.temp =
  324. s->sc.rd_scratchpad =
  325. s->sc.b_scratchpad =
  326. s->sc.obmc_scratchpad = NULL;
  327. if (s->encoding) {
  328. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  329. ME_MAP_SIZE * sizeof(uint32_t), fail)
  330. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  331. ME_MAP_SIZE * sizeof(uint32_t), fail)
  332. if (s->noise_reduction) {
  333. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  334. 2 * 64 * sizeof(int), fail)
  335. }
  336. }
  337. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  338. s->block = s->blocks[0];
  339. for (i = 0; i < 12; i++) {
  340. s->pblocks[i] = &s->block[i];
  341. }
  342. FF_ALLOCZ_OR_GOTO(s->avctx, s->block32, sizeof(*s->block32), fail)
  343. s->dpcm_direction = 0;
  344. FF_ALLOCZ_OR_GOTO(s->avctx, s->dpcm_macroblock, sizeof(*s->dpcm_macroblock), fail)
  345. if (s->avctx->codec_tag == AV_RL32("VCR2")) {
  346. // exchange uv
  347. FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
  348. }
  349. if (s->out_format == FMT_H263) {
  350. /* ac values */
  351. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  352. yc_size * sizeof(int16_t) * 16, fail);
  353. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  354. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  355. s->ac_val[2] = s->ac_val[1] + c_size;
  356. }
  357. return 0;
  358. fail:
  359. return AVERROR(ENOMEM); // free() through ff_mpv_common_end()
  360. }
  361. static void free_duplicate_context(MpegEncContext *s)
  362. {
  363. if (!s)
  364. return;
  365. av_freep(&s->sc.edge_emu_buffer);
  366. av_freep(&s->me.scratchpad);
  367. s->me.temp =
  368. s->sc.rd_scratchpad =
  369. s->sc.b_scratchpad =
  370. s->sc.obmc_scratchpad = NULL;
  371. av_freep(&s->dct_error_sum);
  372. av_freep(&s->me.map);
  373. av_freep(&s->me.score_map);
  374. av_freep(&s->blocks);
  375. av_freep(&s->block32);
  376. av_freep(&s->dpcm_macroblock);
  377. av_freep(&s->ac_val_base);
  378. s->block = NULL;
  379. }
  380. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  381. {
  382. #define COPY(a) bak->a = src->a
  383. COPY(sc.edge_emu_buffer);
  384. COPY(me.scratchpad);
  385. COPY(me.temp);
  386. COPY(sc.rd_scratchpad);
  387. COPY(sc.b_scratchpad);
  388. COPY(sc.obmc_scratchpad);
  389. COPY(me.map);
  390. COPY(me.score_map);
  391. COPY(blocks);
  392. COPY(block);
  393. COPY(block32);
  394. COPY(dpcm_macroblock);
  395. COPY(dpcm_direction);
  396. COPY(start_mb_y);
  397. COPY(end_mb_y);
  398. COPY(me.map_generation);
  399. COPY(pb);
  400. COPY(dct_error_sum);
  401. COPY(dct_count[0]);
  402. COPY(dct_count[1]);
  403. COPY(ac_val_base);
  404. COPY(ac_val[0]);
  405. COPY(ac_val[1]);
  406. COPY(ac_val[2]);
  407. #undef COPY
  408. }
  409. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  410. {
  411. MpegEncContext bak;
  412. int i, ret;
  413. // FIXME copy only needed parts
  414. backup_duplicate_context(&bak, dst);
  415. memcpy(dst, src, sizeof(MpegEncContext));
  416. backup_duplicate_context(dst, &bak);
  417. for (i = 0; i < 12; i++) {
  418. dst->pblocks[i] = &dst->block[i];
  419. }
  420. if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
  421. // exchange uv
  422. FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
  423. }
  424. if (!dst->sc.edge_emu_buffer &&
  425. (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
  426. &dst->sc, dst->linesize)) < 0) {
  427. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  428. "scratch buffers.\n");
  429. return ret;
  430. }
  431. return 0;
  432. }
  433. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  434. const AVCodecContext *src)
  435. {
  436. int i, ret;
  437. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  438. if (dst == src)
  439. return 0;
  440. av_assert0(s != s1);
  441. // FIXME can parameters change on I-frames?
  442. // in that case dst may need a reinit
  443. if (!s->context_initialized) {
  444. int err;
  445. memcpy(s, s1, sizeof(MpegEncContext));
  446. s->avctx = dst;
  447. s->bitstream_buffer = NULL;
  448. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  449. if (s1->context_initialized){
  450. // s->picture_range_start += MAX_PICTURE_COUNT;
  451. // s->picture_range_end += MAX_PICTURE_COUNT;
  452. ff_mpv_idct_init(s);
  453. if((err = ff_mpv_common_init(s)) < 0){
  454. memset(s, 0, sizeof(MpegEncContext));
  455. s->avctx = dst;
  456. return err;
  457. }
  458. }
  459. }
  460. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  461. s->context_reinit = 0;
  462. s->height = s1->height;
  463. s->width = s1->width;
  464. if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
  465. return ret;
  466. }
  467. s->avctx->coded_height = s1->avctx->coded_height;
  468. s->avctx->coded_width = s1->avctx->coded_width;
  469. s->avctx->width = s1->avctx->width;
  470. s->avctx->height = s1->avctx->height;
  471. s->quarter_sample = s1->quarter_sample;
  472. s->coded_picture_number = s1->coded_picture_number;
  473. s->picture_number = s1->picture_number;
  474. av_assert0(!s->picture || s->picture != s1->picture);
  475. if(s->picture)
  476. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  477. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  478. if (s1->picture && s1->picture[i].f->buf[0] &&
  479. (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
  480. return ret;
  481. }
  482. #define UPDATE_PICTURE(pic)\
  483. do {\
  484. ff_mpeg_unref_picture(s->avctx, &s->pic);\
  485. if (s1->pic.f && s1->pic.f->buf[0])\
  486. ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
  487. else\
  488. ret = ff_update_picture_tables(&s->pic, &s1->pic);\
  489. if (ret < 0)\
  490. return ret;\
  491. } while (0)
  492. UPDATE_PICTURE(current_picture);
  493. UPDATE_PICTURE(last_picture);
  494. UPDATE_PICTURE(next_picture);
  495. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  496. ((pic && pic >= old_ctx->picture && \
  497. pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
  498. &new_ctx->picture[pic - old_ctx->picture] : NULL)
  499. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  500. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  501. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  502. // Error/bug resilience
  503. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  504. s->workaround_bugs = s1->workaround_bugs;
  505. s->padding_bug_score = s1->padding_bug_score;
  506. // MPEG-4 timing info
  507. memcpy(&s->last_time_base, &s1->last_time_base,
  508. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  509. (char *) &s1->last_time_base);
  510. // B-frame info
  511. s->max_b_frames = s1->max_b_frames;
  512. s->low_delay = s1->low_delay;
  513. s->droppable = s1->droppable;
  514. // DivX handling (doesn't work)
  515. s->divx_packed = s1->divx_packed;
  516. if (s1->bitstream_buffer) {
  517. if (s1->bitstream_buffer_size +
  518. AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) {
  519. av_fast_malloc(&s->bitstream_buffer,
  520. &s->allocated_bitstream_buffer_size,
  521. s1->allocated_bitstream_buffer_size);
  522. if (!s->bitstream_buffer) {
  523. s->bitstream_buffer_size = 0;
  524. return AVERROR(ENOMEM);
  525. }
  526. }
  527. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  528. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  529. s1->bitstream_buffer_size);
  530. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  531. AV_INPUT_BUFFER_PADDING_SIZE);
  532. }
  533. // linesize-dependent scratch buffer allocation
  534. if (!s->sc.edge_emu_buffer)
  535. if (s1->linesize) {
  536. if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
  537. &s->sc, s1->linesize) < 0) {
  538. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  539. "scratch buffers.\n");
  540. return AVERROR(ENOMEM);
  541. }
  542. } else {
  543. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  544. "be allocated due to unknown size.\n");
  545. }
  546. // MPEG-2/interlacing info
  547. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  548. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  549. if (!s1->first_field) {
  550. s->last_pict_type = s1->pict_type;
  551. if (s1->current_picture_ptr)
  552. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  553. }
  554. return 0;
  555. }
  556. /**
  557. * Set the given MpegEncContext to common defaults
  558. * (same for encoding and decoding).
  559. * The changed fields will not depend upon the
  560. * prior state of the MpegEncContext.
  561. */
  562. void ff_mpv_common_defaults(MpegEncContext *s)
  563. {
  564. s->y_dc_scale_table =
  565. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  566. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  567. s->progressive_frame = 1;
  568. s->progressive_sequence = 1;
  569. s->picture_structure = PICT_FRAME;
  570. s->coded_picture_number = 0;
  571. s->picture_number = 0;
  572. s->f_code = 1;
  573. s->b_code = 1;
  574. s->slice_context_count = 1;
  575. }
  576. /**
  577. * Set the given MpegEncContext to defaults for decoding.
  578. * the changed fields will not depend upon
  579. * the prior state of the MpegEncContext.
  580. */
  581. void ff_mpv_decode_defaults(MpegEncContext *s)
  582. {
  583. ff_mpv_common_defaults(s);
  584. }
  585. void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
  586. {
  587. s->avctx = avctx;
  588. s->width = avctx->coded_width;
  589. s->height = avctx->coded_height;
  590. s->codec_id = avctx->codec->id;
  591. s->workaround_bugs = avctx->workaround_bugs;
  592. /* convert fourcc to upper case */
  593. s->codec_tag = avpriv_toupper4(avctx->codec_tag);
  594. }
  595. /**
  596. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  597. */
  598. static int init_context_frame(MpegEncContext *s)
  599. {
  600. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  601. s->mb_width = (s->width + 15) / 16;
  602. s->mb_stride = s->mb_width + 1;
  603. s->b8_stride = s->mb_width * 2 + 1;
  604. mb_array_size = s->mb_height * s->mb_stride;
  605. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  606. /* set default edge pos, will be overridden
  607. * in decode_header if needed */
  608. s->h_edge_pos = s->mb_width * 16;
  609. s->v_edge_pos = s->mb_height * 16;
  610. s->mb_num = s->mb_width * s->mb_height;
  611. s->block_wrap[0] =
  612. s->block_wrap[1] =
  613. s->block_wrap[2] =
  614. s->block_wrap[3] = s->b8_stride;
  615. s->block_wrap[4] =
  616. s->block_wrap[5] = s->mb_stride;
  617. y_size = s->b8_stride * (2 * s->mb_height + 1);
  618. c_size = s->mb_stride * (s->mb_height + 1);
  619. yc_size = y_size + 2 * c_size;
  620. if (s->mb_height & 1)
  621. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  622. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  623. fail); // error resilience code looks cleaner with this
  624. for (y = 0; y < s->mb_height; y++)
  625. for (x = 0; x < s->mb_width; x++)
  626. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  627. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  628. if (s->encoding) {
  629. /* Allocate MV tables */
  630. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  631. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  632. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  633. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  634. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  635. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  636. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  637. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  638. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  639. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  640. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  641. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  642. /* Allocate MB type table */
  643. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  644. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  645. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  646. mb_array_size * sizeof(float), fail);
  647. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  648. mb_array_size * sizeof(float), fail);
  649. }
  650. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  651. (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
  652. /* interlaced direct mode decoding tables */
  653. for (i = 0; i < 2; i++) {
  654. int j, k;
  655. for (j = 0; j < 2; j++) {
  656. for (k = 0; k < 2; k++) {
  657. FF_ALLOCZ_OR_GOTO(s->avctx,
  658. s->b_field_mv_table_base[i][j][k],
  659. mv_table_size * 2 * sizeof(int16_t),
  660. fail);
  661. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  662. s->mb_stride + 1;
  663. }
  664. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  665. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  666. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  667. }
  668. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  669. }
  670. }
  671. if (s->out_format == FMT_H263) {
  672. /* cbp values */
  673. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  674. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  675. /* cbp, ac_pred, pred_dir */
  676. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  677. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  678. }
  679. if (s->h263_pred || s->h263_plus || !s->encoding) {
  680. /* dc values */
  681. // MN: we need these for error resilience of intra-frames
  682. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  683. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  684. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  685. s->dc_val[2] = s->dc_val[1] + c_size;
  686. for (i = 0; i < yc_size; i++)
  687. s->dc_val_base[i] = 1024;
  688. }
  689. /* which mb is an intra block */
  690. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  691. memset(s->mbintra_table, 1, mb_array_size);
  692. /* init macroblock skip table */
  693. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  694. // Note the + 1 is for a quicker MPEG-4 slice_end detection
  695. return ff_mpeg_er_init(s);
  696. fail:
  697. return AVERROR(ENOMEM);
  698. }
  699. static void clear_context(MpegEncContext *s)
  700. {
  701. int i, j, k;
  702. memset(&s->next_picture, 0, sizeof(s->next_picture));
  703. memset(&s->last_picture, 0, sizeof(s->last_picture));
  704. memset(&s->current_picture, 0, sizeof(s->current_picture));
  705. memset(&s->new_picture, 0, sizeof(s->new_picture));
  706. memset(s->thread_context, 0, sizeof(s->thread_context));
  707. s->me.map = NULL;
  708. s->me.score_map = NULL;
  709. s->dct_error_sum = NULL;
  710. s->block = NULL;
  711. s->blocks = NULL;
  712. s->block32 = NULL;
  713. memset(s->pblocks, 0, sizeof(s->pblocks));
  714. s->dpcm_direction = 0;
  715. s->dpcm_macroblock = NULL;
  716. s->ac_val_base = NULL;
  717. s->ac_val[0] =
  718. s->ac_val[1] =
  719. s->ac_val[2] =NULL;
  720. s->sc.edge_emu_buffer = NULL;
  721. s->me.scratchpad = NULL;
  722. s->me.temp =
  723. s->sc.rd_scratchpad =
  724. s->sc.b_scratchpad =
  725. s->sc.obmc_scratchpad = NULL;
  726. s->bitstream_buffer = NULL;
  727. s->allocated_bitstream_buffer_size = 0;
  728. s->picture = NULL;
  729. s->mb_type = NULL;
  730. s->p_mv_table_base = NULL;
  731. s->b_forw_mv_table_base = NULL;
  732. s->b_back_mv_table_base = NULL;
  733. s->b_bidir_forw_mv_table_base = NULL;
  734. s->b_bidir_back_mv_table_base = NULL;
  735. s->b_direct_mv_table_base = NULL;
  736. s->p_mv_table = NULL;
  737. s->b_forw_mv_table = NULL;
  738. s->b_back_mv_table = NULL;
  739. s->b_bidir_forw_mv_table = NULL;
  740. s->b_bidir_back_mv_table = NULL;
  741. s->b_direct_mv_table = NULL;
  742. for (i = 0; i < 2; i++) {
  743. for (j = 0; j < 2; j++) {
  744. for (k = 0; k < 2; k++) {
  745. s->b_field_mv_table_base[i][j][k] = NULL;
  746. s->b_field_mv_table[i][j][k] = NULL;
  747. }
  748. s->b_field_select_table[i][j] = NULL;
  749. s->p_field_mv_table_base[i][j] = NULL;
  750. s->p_field_mv_table[i][j] = NULL;
  751. }
  752. s->p_field_select_table[i] = NULL;
  753. }
  754. s->dc_val_base = NULL;
  755. s->coded_block_base = NULL;
  756. s->mbintra_table = NULL;
  757. s->cbp_table = NULL;
  758. s->pred_dir_table = NULL;
  759. s->mbskip_table = NULL;
  760. s->er.error_status_table = NULL;
  761. s->er.er_temp_buffer = NULL;
  762. s->mb_index2xy = NULL;
  763. s->lambda_table = NULL;
  764. s->cplx_tab = NULL;
  765. s->bits_tab = NULL;
  766. }
  767. /**
  768. * init common structure for both encoder and decoder.
  769. * this assumes that some variables like width/height are already set
  770. */
  771. av_cold int ff_mpv_common_init(MpegEncContext *s)
  772. {
  773. int i, ret;
  774. int nb_slices = (HAVE_THREADS &&
  775. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  776. s->avctx->thread_count : 1;
  777. clear_context(s);
  778. if (s->encoding && s->avctx->slices)
  779. nb_slices = s->avctx->slices;
  780. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  781. s->mb_height = (s->height + 31) / 32 * 2;
  782. else
  783. s->mb_height = (s->height + 15) / 16;
  784. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  785. av_log(s->avctx, AV_LOG_ERROR,
  786. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  787. return AVERROR(EINVAL);
  788. }
  789. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  790. int max_slices;
  791. if (s->mb_height)
  792. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  793. else
  794. max_slices = MAX_THREADS;
  795. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  796. " reducing to %d\n", nb_slices, max_slices);
  797. nb_slices = max_slices;
  798. }
  799. if ((s->width || s->height) &&
  800. av_image_check_size(s->width, s->height, 0, s->avctx))
  801. return AVERROR(EINVAL);
  802. dct_init(s);
  803. /* set chroma shifts */
  804. ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  805. &s->chroma_x_shift,
  806. &s->chroma_y_shift);
  807. if (ret)
  808. return ret;
  809. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  810. MAX_PICTURE_COUNT * sizeof(Picture), fail_nomem);
  811. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  812. s->picture[i].f = av_frame_alloc();
  813. if (!s->picture[i].f)
  814. goto fail_nomem;
  815. }
  816. if (!(s->next_picture.f = av_frame_alloc()) ||
  817. !(s->last_picture.f = av_frame_alloc()) ||
  818. !(s->current_picture.f = av_frame_alloc()) ||
  819. !(s->new_picture.f = av_frame_alloc()))
  820. goto fail_nomem;
  821. if ((ret = init_context_frame(s)))
  822. goto fail_nomem;
  823. s->parse_context.state = -1;
  824. s->context_initialized = 1;
  825. memset(s->thread_context, 0, sizeof(s->thread_context));
  826. s->thread_context[0] = s;
  827. // if (s->width && s->height) {
  828. if (nb_slices > 1) {
  829. for (i = 0; i < nb_slices; i++) {
  830. if (i) {
  831. s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
  832. if (!s->thread_context[i])
  833. goto fail_nomem;
  834. }
  835. if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
  836. goto fail;
  837. s->thread_context[i]->start_mb_y =
  838. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  839. s->thread_context[i]->end_mb_y =
  840. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  841. }
  842. } else {
  843. if ((ret = init_duplicate_context(s)) < 0)
  844. goto fail;
  845. s->start_mb_y = 0;
  846. s->end_mb_y = s->mb_height;
  847. }
  848. s->slice_context_count = nb_slices;
  849. // }
  850. return 0;
  851. fail_nomem:
  852. ret = AVERROR(ENOMEM);
  853. fail:
  854. return ret;
  855. }
  856. /**
  857. * Frees and resets MpegEncContext fields depending on the resolution.
  858. * Is used during resolution changes to avoid a full reinitialization of the
  859. * codec.
  860. */
  861. static void free_context_frame(MpegEncContext *s)
  862. {
  863. int i, j, k;
  864. av_freep(&s->mb_type);
  865. av_freep(&s->p_mv_table_base);
  866. av_freep(&s->b_forw_mv_table_base);
  867. av_freep(&s->b_back_mv_table_base);
  868. av_freep(&s->b_bidir_forw_mv_table_base);
  869. av_freep(&s->b_bidir_back_mv_table_base);
  870. av_freep(&s->b_direct_mv_table_base);
  871. s->p_mv_table = NULL;
  872. s->b_forw_mv_table = NULL;
  873. s->b_back_mv_table = NULL;
  874. s->b_bidir_forw_mv_table = NULL;
  875. s->b_bidir_back_mv_table = NULL;
  876. s->b_direct_mv_table = NULL;
  877. for (i = 0; i < 2; i++) {
  878. for (j = 0; j < 2; j++) {
  879. for (k = 0; k < 2; k++) {
  880. av_freep(&s->b_field_mv_table_base[i][j][k]);
  881. s->b_field_mv_table[i][j][k] = NULL;
  882. }
  883. av_freep(&s->b_field_select_table[i][j]);
  884. av_freep(&s->p_field_mv_table_base[i][j]);
  885. s->p_field_mv_table[i][j] = NULL;
  886. }
  887. av_freep(&s->p_field_select_table[i]);
  888. }
  889. av_freep(&s->dc_val_base);
  890. av_freep(&s->coded_block_base);
  891. av_freep(&s->mbintra_table);
  892. av_freep(&s->cbp_table);
  893. av_freep(&s->pred_dir_table);
  894. av_freep(&s->mbskip_table);
  895. av_freep(&s->er.error_status_table);
  896. av_freep(&s->er.er_temp_buffer);
  897. av_freep(&s->mb_index2xy);
  898. av_freep(&s->lambda_table);
  899. av_freep(&s->cplx_tab);
  900. av_freep(&s->bits_tab);
  901. s->linesize = s->uvlinesize = 0;
  902. }
  903. int ff_mpv_common_frame_size_change(MpegEncContext *s)
  904. {
  905. int i, err = 0;
  906. if (!s->context_initialized)
  907. return AVERROR(EINVAL);
  908. if (s->slice_context_count > 1) {
  909. for (i = 0; i < s->slice_context_count; i++) {
  910. free_duplicate_context(s->thread_context[i]);
  911. }
  912. for (i = 1; i < s->slice_context_count; i++) {
  913. av_freep(&s->thread_context[i]);
  914. }
  915. } else
  916. free_duplicate_context(s);
  917. free_context_frame(s);
  918. if (s->picture)
  919. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  920. s->picture[i].needs_realloc = 1;
  921. }
  922. s->last_picture_ptr =
  923. s->next_picture_ptr =
  924. s->current_picture_ptr = NULL;
  925. // init
  926. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  927. s->mb_height = (s->height + 31) / 32 * 2;
  928. else
  929. s->mb_height = (s->height + 15) / 16;
  930. if ((s->width || s->height) &&
  931. (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  932. goto fail;
  933. if ((err = init_context_frame(s)))
  934. goto fail;
  935. memset(s->thread_context, 0, sizeof(s->thread_context));
  936. s->thread_context[0] = s;
  937. if (s->width && s->height) {
  938. int nb_slices = s->slice_context_count;
  939. if (nb_slices > 1) {
  940. for (i = 0; i < nb_slices; i++) {
  941. if (i) {
  942. s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
  943. if (!s->thread_context[i]) {
  944. err = AVERROR(ENOMEM);
  945. goto fail;
  946. }
  947. }
  948. if ((err = init_duplicate_context(s->thread_context[i])) < 0)
  949. goto fail;
  950. s->thread_context[i]->start_mb_y =
  951. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  952. s->thread_context[i]->end_mb_y =
  953. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  954. }
  955. } else {
  956. err = init_duplicate_context(s);
  957. if (err < 0)
  958. goto fail;
  959. s->start_mb_y = 0;
  960. s->end_mb_y = s->mb_height;
  961. }
  962. s->slice_context_count = nb_slices;
  963. }
  964. return 0;
  965. fail:
  966. return err;
  967. }
  968. /* init common structure for both encoder and decoder */
  969. void ff_mpv_common_end(MpegEncContext *s)
  970. {
  971. int i;
  972. if (!s)
  973. return ;
  974. if (s->slice_context_count > 1) {
  975. for (i = 0; i < s->slice_context_count; i++) {
  976. free_duplicate_context(s->thread_context[i]);
  977. }
  978. for (i = 1; i < s->slice_context_count; i++) {
  979. av_freep(&s->thread_context[i]);
  980. }
  981. s->slice_context_count = 1;
  982. } else free_duplicate_context(s);
  983. av_freep(&s->parse_context.buffer);
  984. s->parse_context.buffer_size = 0;
  985. av_freep(&s->bitstream_buffer);
  986. s->allocated_bitstream_buffer_size = 0;
  987. if (!s->avctx)
  988. return;
  989. if (s->picture) {
  990. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  991. ff_free_picture_tables(&s->picture[i]);
  992. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  993. av_frame_free(&s->picture[i].f);
  994. }
  995. }
  996. av_freep(&s->picture);
  997. ff_free_picture_tables(&s->last_picture);
  998. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  999. av_frame_free(&s->last_picture.f);
  1000. ff_free_picture_tables(&s->current_picture);
  1001. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1002. av_frame_free(&s->current_picture.f);
  1003. ff_free_picture_tables(&s->next_picture);
  1004. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1005. av_frame_free(&s->next_picture.f);
  1006. ff_free_picture_tables(&s->new_picture);
  1007. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  1008. av_frame_free(&s->new_picture.f);
  1009. free_context_frame(s);
  1010. s->context_initialized = 0;
  1011. s->last_picture_ptr =
  1012. s->next_picture_ptr =
  1013. s->current_picture_ptr = NULL;
  1014. s->linesize = s->uvlinesize = 0;
  1015. }
  1016. static void gray_frame(AVFrame *frame)
  1017. {
  1018. int i, h_chroma_shift, v_chroma_shift;
  1019. av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
  1020. for(i=0; i<frame->height; i++)
  1021. memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
  1022. for(i=0; i<AV_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
  1023. memset(frame->data[1] + frame->linesize[1]*i,
  1024. 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1025. memset(frame->data[2] + frame->linesize[2]*i,
  1026. 0x80, AV_CEIL_RSHIFT(frame->width, h_chroma_shift));
  1027. }
  1028. }
  1029. /**
  1030. * generic function called after decoding
  1031. * the header and before a frame is decoded.
  1032. */
  1033. int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1034. {
  1035. int i, ret;
  1036. Picture *pic;
  1037. s->mb_skipped = 0;
  1038. if (!ff_thread_can_start_frame(avctx)) {
  1039. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1040. return -1;
  1041. }
  1042. /* mark & release old frames */
  1043. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1044. s->last_picture_ptr != s->next_picture_ptr &&
  1045. s->last_picture_ptr->f->buf[0]) {
  1046. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1047. }
  1048. /* release forgotten pictures */
  1049. /* if (MPEG-124 / H.263) */
  1050. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1051. if (&s->picture[i] != s->last_picture_ptr &&
  1052. &s->picture[i] != s->next_picture_ptr &&
  1053. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1054. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1055. }
  1056. }
  1057. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1058. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1059. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1060. /* release non reference frames */
  1061. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1062. if (!s->picture[i].reference)
  1063. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1064. }
  1065. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  1066. // we already have an unused image
  1067. // (maybe it was set before reading the header)
  1068. pic = s->current_picture_ptr;
  1069. } else {
  1070. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1071. if (i < 0) {
  1072. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1073. return i;
  1074. }
  1075. pic = &s->picture[i];
  1076. }
  1077. pic->reference = 0;
  1078. if (!s->droppable) {
  1079. if (s->pict_type != AV_PICTURE_TYPE_B)
  1080. pic->reference = 3;
  1081. }
  1082. pic->f->coded_picture_number = s->coded_picture_number++;
  1083. if (alloc_picture(s, pic) < 0)
  1084. return -1;
  1085. s->current_picture_ptr = pic;
  1086. // FIXME use only the vars from current_pic
  1087. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1088. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1089. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1090. if (s->picture_structure != PICT_FRAME)
  1091. s->current_picture_ptr->f->top_field_first =
  1092. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1093. }
  1094. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1095. !s->progressive_sequence;
  1096. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1097. s->current_picture_ptr->f->pict_type = s->pict_type;
  1098. // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
  1099. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1100. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1101. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1102. s->current_picture_ptr)) < 0)
  1103. return ret;
  1104. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1105. s->last_picture_ptr = s->next_picture_ptr;
  1106. if (!s->droppable)
  1107. s->next_picture_ptr = s->current_picture_ptr;
  1108. }
  1109. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1110. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1111. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1112. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1113. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1114. s->pict_type, s->droppable);
  1115. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  1116. (s->pict_type != AV_PICTURE_TYPE_I)) {
  1117. int h_chroma_shift, v_chroma_shift;
  1118. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1119. &h_chroma_shift, &v_chroma_shift);
  1120. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f->buf[0])
  1121. av_log(avctx, AV_LOG_DEBUG,
  1122. "allocating dummy last picture for B frame\n");
  1123. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1124. av_log(avctx, AV_LOG_ERROR,
  1125. "warning: first frame is no keyframe\n");
  1126. /* Allocate a dummy frame */
  1127. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1128. if (i < 0) {
  1129. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1130. return i;
  1131. }
  1132. s->last_picture_ptr = &s->picture[i];
  1133. s->last_picture_ptr->reference = 3;
  1134. s->last_picture_ptr->f->key_frame = 0;
  1135. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1136. if (alloc_picture(s, s->last_picture_ptr) < 0) {
  1137. s->last_picture_ptr = NULL;
  1138. return -1;
  1139. }
  1140. if (!avctx->hwaccel) {
  1141. for(i=0; i<avctx->height; i++)
  1142. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
  1143. 0x80, avctx->width);
  1144. if (s->last_picture_ptr->f->data[2]) {
  1145. for(i=0; i<AV_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1146. memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
  1147. 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1148. memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
  1149. 0x80, AV_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1150. }
  1151. }
  1152. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1153. for(i=0; i<avctx->height; i++)
  1154. memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
  1155. }
  1156. }
  1157. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1158. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1159. }
  1160. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1161. s->pict_type == AV_PICTURE_TYPE_B) {
  1162. /* Allocate a dummy frame */
  1163. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1164. if (i < 0) {
  1165. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1166. return i;
  1167. }
  1168. s->next_picture_ptr = &s->picture[i];
  1169. s->next_picture_ptr->reference = 3;
  1170. s->next_picture_ptr->f->key_frame = 0;
  1171. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P;
  1172. if (alloc_picture(s, s->next_picture_ptr) < 0) {
  1173. s->next_picture_ptr = NULL;
  1174. return -1;
  1175. }
  1176. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1177. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1178. }
  1179. #if 0 // BUFREF-FIXME
  1180. memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
  1181. memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
  1182. #endif
  1183. if (s->last_picture_ptr) {
  1184. if (s->last_picture_ptr->f->buf[0] &&
  1185. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1186. s->last_picture_ptr)) < 0)
  1187. return ret;
  1188. }
  1189. if (s->next_picture_ptr) {
  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 MPEG-4 and we can't do it in the header
  1211. * decode as init is not called for MPEG-4 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. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1235. {
  1236. ff_print_debug_info2(s->avctx, pict, s->mbskip_table, p->mb_type,
  1237. p->qscale_table, p->motion_val, &s->low_delay,
  1238. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1239. }
  1240. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1241. {
  1242. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1243. int offset = 2*s->mb_stride + 1;
  1244. if(!ref)
  1245. return AVERROR(ENOMEM);
  1246. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1247. ref->size -= offset;
  1248. ref->data += offset;
  1249. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1250. }
  1251. static inline int hpel_motion_lowres(MpegEncContext *s,
  1252. uint8_t *dest, uint8_t *src,
  1253. int field_based, int field_select,
  1254. int src_x, int src_y,
  1255. int width, int height, ptrdiff_t stride,
  1256. int h_edge_pos, int v_edge_pos,
  1257. int w, int h, h264_chroma_mc_func *pix_op,
  1258. int motion_x, int motion_y)
  1259. {
  1260. const int lowres = s->avctx->lowres;
  1261. const int op_index = FFMIN(lowres, 3);
  1262. const int s_mask = (2 << lowres) - 1;
  1263. int emu = 0;
  1264. int sx, sy;
  1265. if (s->quarter_sample) {
  1266. motion_x /= 2;
  1267. motion_y /= 2;
  1268. }
  1269. sx = motion_x & s_mask;
  1270. sy = motion_y & s_mask;
  1271. src_x += motion_x >> lowres + 1;
  1272. src_y += motion_y >> lowres + 1;
  1273. src += src_y * stride + src_x;
  1274. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1275. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1276. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
  1277. s->linesize, s->linesize,
  1278. w + 1, (h + 1) << field_based,
  1279. src_x, src_y << field_based,
  1280. h_edge_pos, v_edge_pos);
  1281. src = s->sc.edge_emu_buffer;
  1282. emu = 1;
  1283. }
  1284. sx = (sx << 2) >> lowres;
  1285. sy = (sy << 2) >> lowres;
  1286. if (field_select)
  1287. src += s->linesize;
  1288. pix_op[op_index](dest, src, stride, h, sx, sy);
  1289. return emu;
  1290. }
  1291. /* apply one mpeg motion vector to the three components */
  1292. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1293. uint8_t *dest_y,
  1294. uint8_t *dest_cb,
  1295. uint8_t *dest_cr,
  1296. int field_based,
  1297. int bottom_field,
  1298. int field_select,
  1299. uint8_t **ref_picture,
  1300. h264_chroma_mc_func *pix_op,
  1301. int motion_x, int motion_y,
  1302. int h, int mb_y)
  1303. {
  1304. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1305. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  1306. ptrdiff_t uvlinesize, linesize;
  1307. const int lowres = s->avctx->lowres;
  1308. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  1309. const int block_s = 8>>lowres;
  1310. const int s_mask = (2 << lowres) - 1;
  1311. const int h_edge_pos = s->h_edge_pos >> lowres;
  1312. const int v_edge_pos = s->v_edge_pos >> lowres;
  1313. linesize = s->current_picture.f->linesize[0] << field_based;
  1314. uvlinesize = s->current_picture.f->linesize[1] << field_based;
  1315. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1316. if (s->quarter_sample) {
  1317. motion_x /= 2;
  1318. motion_y /= 2;
  1319. }
  1320. if(field_based){
  1321. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1322. }
  1323. sx = motion_x & s_mask;
  1324. sy = motion_y & s_mask;
  1325. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1326. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1327. if (s->out_format == FMT_H263) {
  1328. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1329. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1330. uvsrc_x = src_x >> 1;
  1331. uvsrc_y = src_y >> 1;
  1332. } else if (s->out_format == FMT_H261) {
  1333. // even chroma mv's are full pel in H261
  1334. mx = motion_x / 4;
  1335. my = motion_y / 4;
  1336. uvsx = (2 * mx) & s_mask;
  1337. uvsy = (2 * my) & s_mask;
  1338. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1339. uvsrc_y = mb_y * block_s + (my >> lowres);
  1340. } else {
  1341. if(s->chroma_y_shift){
  1342. mx = motion_x / 2;
  1343. my = motion_y / 2;
  1344. uvsx = mx & s_mask;
  1345. uvsy = my & s_mask;
  1346. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1347. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1348. } else {
  1349. if(s->chroma_x_shift){
  1350. //Chroma422
  1351. mx = motion_x / 2;
  1352. uvsx = mx & s_mask;
  1353. uvsy = motion_y & s_mask;
  1354. uvsrc_y = src_y;
  1355. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1356. } else {
  1357. //Chroma444
  1358. uvsx = motion_x & s_mask;
  1359. uvsy = motion_y & s_mask;
  1360. uvsrc_x = src_x;
  1361. uvsrc_y = src_y;
  1362. }
  1363. }
  1364. }
  1365. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1366. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1367. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1368. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  1369. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1370. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
  1371. linesize >> field_based, linesize >> field_based,
  1372. 17, 17 + field_based,
  1373. src_x, src_y << field_based, h_edge_pos,
  1374. v_edge_pos);
  1375. ptr_y = s->sc.edge_emu_buffer;
  1376. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1377. uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
  1378. uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
  1379. if (s->workaround_bugs & FF_BUG_IEDGE)
  1380. vbuf -= s->uvlinesize;
  1381. s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
  1382. uvlinesize >> field_based, uvlinesize >> field_based,
  1383. 9, 9 + field_based,
  1384. uvsrc_x, uvsrc_y << field_based,
  1385. h_edge_pos >> 1, v_edge_pos >> 1);
  1386. s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
  1387. uvlinesize >> field_based,uvlinesize >> field_based,
  1388. 9, 9 + field_based,
  1389. uvsrc_x, uvsrc_y << field_based,
  1390. h_edge_pos >> 1, v_edge_pos >> 1);
  1391. ptr_cb = ubuf;
  1392. ptr_cr = vbuf;
  1393. }
  1394. }
  1395. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
  1396. if (bottom_field) {
  1397. dest_y += s->linesize;
  1398. dest_cb += s->uvlinesize;
  1399. dest_cr += s->uvlinesize;
  1400. }
  1401. if (field_select) {
  1402. ptr_y += s->linesize;
  1403. ptr_cb += s->uvlinesize;
  1404. ptr_cr += s->uvlinesize;
  1405. }
  1406. sx = (sx << 2) >> lowres;
  1407. sy = (sy << 2) >> lowres;
  1408. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1409. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1410. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  1411. uvsx = (uvsx << 2) >> lowres;
  1412. uvsy = (uvsy << 2) >> lowres;
  1413. if (hc) {
  1414. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  1415. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  1416. }
  1417. }
  1418. // FIXME h261 lowres loop filter
  1419. }
  1420. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1421. uint8_t *dest_cb, uint8_t *dest_cr,
  1422. uint8_t **ref_picture,
  1423. h264_chroma_mc_func * pix_op,
  1424. int mx, int my)
  1425. {
  1426. const int lowres = s->avctx->lowres;
  1427. const int op_index = FFMIN(lowres, 3);
  1428. const int block_s = 8 >> lowres;
  1429. const int s_mask = (2 << lowres) - 1;
  1430. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1431. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1432. int emu = 0, src_x, src_y, sx, sy;
  1433. ptrdiff_t offset;
  1434. uint8_t *ptr;
  1435. if (s->quarter_sample) {
  1436. mx /= 2;
  1437. my /= 2;
  1438. }
  1439. /* In case of 8X8, we construct a single chroma motion vector
  1440. with a special rounding */
  1441. mx = ff_h263_round_chroma(mx);
  1442. my = ff_h263_round_chroma(my);
  1443. sx = mx & s_mask;
  1444. sy = my & s_mask;
  1445. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  1446. src_y = s->mb_y * block_s + (my >> lowres + 1);
  1447. offset = src_y * s->uvlinesize + src_x;
  1448. ptr = ref_picture[1] + offset;
  1449. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  1450. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  1451. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1452. s->uvlinesize, s->uvlinesize,
  1453. 9, 9,
  1454. src_x, src_y, h_edge_pos, v_edge_pos);
  1455. ptr = s->sc.edge_emu_buffer;
  1456. emu = 1;
  1457. }
  1458. sx = (sx << 2) >> lowres;
  1459. sy = (sy << 2) >> lowres;
  1460. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1461. ptr = ref_picture[2] + offset;
  1462. if (emu) {
  1463. s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
  1464. s->uvlinesize, s->uvlinesize,
  1465. 9, 9,
  1466. src_x, src_y, h_edge_pos, v_edge_pos);
  1467. ptr = s->sc.edge_emu_buffer;
  1468. }
  1469. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1470. }
  1471. /**
  1472. * motion compensation of a single macroblock
  1473. * @param s context
  1474. * @param dest_y luma destination pointer
  1475. * @param dest_cb chroma cb/u destination pointer
  1476. * @param dest_cr chroma cr/v destination pointer
  1477. * @param dir direction (0->forward, 1->backward)
  1478. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1479. * @param pix_op halfpel motion compensation function (average or put normally)
  1480. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1481. */
  1482. static inline void MPV_motion_lowres(MpegEncContext *s,
  1483. uint8_t *dest_y, uint8_t *dest_cb,
  1484. uint8_t *dest_cr,
  1485. int dir, uint8_t **ref_picture,
  1486. h264_chroma_mc_func *pix_op)
  1487. {
  1488. int mx, my;
  1489. int mb_x, mb_y, i;
  1490. const int lowres = s->avctx->lowres;
  1491. const int block_s = 8 >>lowres;
  1492. mb_x = s->mb_x;
  1493. mb_y = s->mb_y;
  1494. switch (s->mv_type) {
  1495. case MV_TYPE_16X16:
  1496. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1497. 0, 0, 0,
  1498. ref_picture, pix_op,
  1499. s->mv[dir][0][0], s->mv[dir][0][1],
  1500. 2 * block_s, mb_y);
  1501. break;
  1502. case MV_TYPE_8X8:
  1503. mx = 0;
  1504. my = 0;
  1505. for (i = 0; i < 4; i++) {
  1506. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  1507. s->linesize) * block_s,
  1508. ref_picture[0], 0, 0,
  1509. (2 * mb_x + (i & 1)) * block_s,
  1510. (2 * mb_y + (i >> 1)) * block_s,
  1511. s->width, s->height, s->linesize,
  1512. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1513. block_s, block_s, pix_op,
  1514. s->mv[dir][i][0], s->mv[dir][i][1]);
  1515. mx += s->mv[dir][i][0];
  1516. my += s->mv[dir][i][1];
  1517. }
  1518. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
  1519. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  1520. pix_op, mx, my);
  1521. break;
  1522. case MV_TYPE_FIELD:
  1523. if (s->picture_structure == PICT_FRAME) {
  1524. /* top field */
  1525. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1526. 1, 0, s->field_select[dir][0],
  1527. ref_picture, pix_op,
  1528. s->mv[dir][0][0], s->mv[dir][0][1],
  1529. block_s, mb_y);
  1530. /* bottom field */
  1531. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1532. 1, 1, s->field_select[dir][1],
  1533. ref_picture, pix_op,
  1534. s->mv[dir][1][0], s->mv[dir][1][1],
  1535. block_s, mb_y);
  1536. } else {
  1537. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  1538. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  1539. ref_picture = s->current_picture_ptr->f->data;
  1540. }
  1541. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1542. 0, 0, s->field_select[dir][0],
  1543. ref_picture, pix_op,
  1544. s->mv[dir][0][0],
  1545. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  1546. }
  1547. break;
  1548. case MV_TYPE_16X8:
  1549. for (i = 0; i < 2; i++) {
  1550. uint8_t **ref2picture;
  1551. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  1552. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  1553. ref2picture = ref_picture;
  1554. } else {
  1555. ref2picture = s->current_picture_ptr->f->data;
  1556. }
  1557. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1558. 0, 0, s->field_select[dir][i],
  1559. ref2picture, pix_op,
  1560. s->mv[dir][i][0], s->mv[dir][i][1] +
  1561. 2 * block_s * i, block_s, mb_y >> 1);
  1562. dest_y += 2 * block_s * s->linesize;
  1563. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  1564. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  1565. }
  1566. break;
  1567. case MV_TYPE_DMV:
  1568. if (s->picture_structure == PICT_FRAME) {
  1569. for (i = 0; i < 2; i++) {
  1570. int j;
  1571. for (j = 0; j < 2; j++) {
  1572. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1573. 1, j, j ^ i,
  1574. ref_picture, pix_op,
  1575. s->mv[dir][2 * i + j][0],
  1576. s->mv[dir][2 * i + j][1],
  1577. block_s, mb_y);
  1578. }
  1579. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  1580. }
  1581. } else {
  1582. for (i = 0; i < 2; i++) {
  1583. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1584. 0, 0, s->picture_structure != i + 1,
  1585. ref_picture, pix_op,
  1586. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  1587. 2 * block_s, mb_y >> 1);
  1588. // after put we make avg of the same block
  1589. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  1590. // opposite parity is always in the same
  1591. // frame if this is second field
  1592. if (!s->first_field) {
  1593. ref_picture = s->current_picture_ptr->f->data;
  1594. }
  1595. }
  1596. }
  1597. break;
  1598. default:
  1599. av_assert2(0);
  1600. }
  1601. }
  1602. /**
  1603. * find the lowest MB row referenced in the MVs
  1604. */
  1605. static int lowest_referenced_row(MpegEncContext *s, int dir)
  1606. {
  1607. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1608. int my, off, i, mvs;
  1609. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1610. goto unhandled;
  1611. switch (s->mv_type) {
  1612. case MV_TYPE_16X16:
  1613. mvs = 1;
  1614. break;
  1615. case MV_TYPE_16X8:
  1616. mvs = 2;
  1617. break;
  1618. case MV_TYPE_8X8:
  1619. mvs = 4;
  1620. break;
  1621. default:
  1622. goto unhandled;
  1623. }
  1624. for (i = 0; i < mvs; i++) {
  1625. my = s->mv[dir][i][1];
  1626. my_max = FFMAX(my_max, my);
  1627. my_min = FFMIN(my_min, my);
  1628. }
  1629. off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
  1630. return av_clip(s->mb_y + off, 0, s->mb_height - 1);
  1631. unhandled:
  1632. return s->mb_height-1;
  1633. }
  1634. /* put block[] to dest[] */
  1635. static inline void put_dct(MpegEncContext *s,
  1636. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1637. {
  1638. s->dct_unquantize_intra(s, block, i, qscale);
  1639. s->idsp.idct_put(dest, line_size, block);
  1640. }
  1641. /* add block[] to dest[] */
  1642. static inline void add_dct(MpegEncContext *s,
  1643. int16_t *block, int i, uint8_t *dest, int line_size)
  1644. {
  1645. if (s->block_last_index[i] >= 0) {
  1646. s->idsp.idct_add(dest, line_size, block);
  1647. }
  1648. }
  1649. static inline void add_dequant_dct(MpegEncContext *s,
  1650. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1651. {
  1652. if (s->block_last_index[i] >= 0) {
  1653. s->dct_unquantize_inter(s, block, i, qscale);
  1654. s->idsp.idct_add(dest, line_size, block);
  1655. }
  1656. }
  1657. /**
  1658. * Clean dc, ac, coded_block for the current non-intra MB.
  1659. */
  1660. void ff_clean_intra_table_entries(MpegEncContext *s)
  1661. {
  1662. int wrap = s->b8_stride;
  1663. int xy = s->block_index[0];
  1664. s->dc_val[0][xy ] =
  1665. s->dc_val[0][xy + 1 ] =
  1666. s->dc_val[0][xy + wrap] =
  1667. s->dc_val[0][xy + 1 + wrap] = 1024;
  1668. /* ac pred */
  1669. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1670. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1671. if (s->msmpeg4_version>=3) {
  1672. s->coded_block[xy ] =
  1673. s->coded_block[xy + 1 ] =
  1674. s->coded_block[xy + wrap] =
  1675. s->coded_block[xy + 1 + wrap] = 0;
  1676. }
  1677. /* chroma */
  1678. wrap = s->mb_stride;
  1679. xy = s->mb_x + s->mb_y * wrap;
  1680. s->dc_val[1][xy] =
  1681. s->dc_val[2][xy] = 1024;
  1682. /* ac pred */
  1683. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1684. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1685. s->mbintra_table[xy]= 0;
  1686. }
  1687. /* generic function called after a macroblock has been parsed by the
  1688. decoder or after it has been encoded by the encoder.
  1689. Important variables used:
  1690. s->mb_intra : true if intra macroblock
  1691. s->mv_dir : motion vector direction
  1692. s->mv_type : motion vector type
  1693. s->mv : motion vector
  1694. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1695. */
  1696. static av_always_inline
  1697. void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1698. int lowres_flag, int is_mpeg12)
  1699. {
  1700. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1701. if (CONFIG_XVMC &&
  1702. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  1703. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  1704. return;
  1705. }
  1706. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1707. /* print DCT coefficients */
  1708. int i,j;
  1709. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1710. for(i=0; i<6; i++){
  1711. for(j=0; j<64; j++){
  1712. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  1713. block[i][s->idsp.idct_permutation[j]]);
  1714. }
  1715. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1716. }
  1717. }
  1718. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1719. /* update DC predictors for P macroblocks */
  1720. if (!s->mb_intra) {
  1721. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1722. if(s->mbintra_table[mb_xy])
  1723. ff_clean_intra_table_entries(s);
  1724. } else {
  1725. s->last_dc[0] =
  1726. s->last_dc[1] =
  1727. s->last_dc[2] = 128 << s->intra_dc_precision;
  1728. }
  1729. }
  1730. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1731. s->mbintra_table[mb_xy]=1;
  1732. if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->frame_skip_threshold || s->frame_skip_factor ||
  1733. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  1734. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  1735. uint8_t *dest_y, *dest_cb, *dest_cr;
  1736. int dct_linesize, dct_offset;
  1737. op_pixels_func (*op_pix)[4];
  1738. qpel_mc_func (*op_qpix)[16];
  1739. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1740. const int uvlinesize = s->current_picture.f->linesize[1];
  1741. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1742. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1743. /* avoid copy if macroblock skipped in last frame too */
  1744. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1745. if(!s->encoding){
  1746. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1747. if (s->mb_skipped) {
  1748. s->mb_skipped= 0;
  1749. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  1750. *mbskip_ptr = 1;
  1751. } else if(!s->current_picture.reference) {
  1752. *mbskip_ptr = 1;
  1753. } else{
  1754. *mbskip_ptr = 0; /* not skipped */
  1755. }
  1756. }
  1757. dct_linesize = linesize << s->interlaced_dct;
  1758. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1759. if(readable){
  1760. dest_y= s->dest[0];
  1761. dest_cb= s->dest[1];
  1762. dest_cr= s->dest[2];
  1763. }else{
  1764. dest_y = s->sc.b_scratchpad;
  1765. dest_cb= s->sc.b_scratchpad+16*linesize;
  1766. dest_cr= s->sc.b_scratchpad+32*linesize;
  1767. }
  1768. if (!s->mb_intra) {
  1769. /* motion handling */
  1770. /* decoding or more than one mb_type (MC was already done otherwise) */
  1771. if(!s->encoding){
  1772. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1773. if (s->mv_dir & MV_DIR_FORWARD) {
  1774. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1775. lowest_referenced_row(s, 0),
  1776. 0);
  1777. }
  1778. if (s->mv_dir & MV_DIR_BACKWARD) {
  1779. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1780. lowest_referenced_row(s, 1),
  1781. 0);
  1782. }
  1783. }
  1784. if(lowres_flag){
  1785. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  1786. if (s->mv_dir & MV_DIR_FORWARD) {
  1787. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
  1788. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  1789. }
  1790. if (s->mv_dir & MV_DIR_BACKWARD) {
  1791. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
  1792. }
  1793. }else{
  1794. op_qpix = s->me.qpel_put;
  1795. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1796. op_pix = s->hdsp.put_pixels_tab;
  1797. }else{
  1798. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1799. }
  1800. if (s->mv_dir & MV_DIR_FORWARD) {
  1801. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  1802. op_pix = s->hdsp.avg_pixels_tab;
  1803. op_qpix= s->me.qpel_avg;
  1804. }
  1805. if (s->mv_dir & MV_DIR_BACKWARD) {
  1806. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  1807. }
  1808. }
  1809. }
  1810. /* skip dequant / idct if we are really late ;) */
  1811. if(s->avctx->skip_idct){
  1812. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1813. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1814. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1815. goto skip_idct;
  1816. }
  1817. /* add dct residue */
  1818. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1819. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1820. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1821. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1822. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1823. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1824. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1825. if (s->chroma_y_shift){
  1826. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1827. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1828. }else{
  1829. dct_linesize >>= 1;
  1830. dct_offset >>=1;
  1831. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1832. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1833. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1834. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1835. }
  1836. }
  1837. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1838. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1839. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1840. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1841. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1842. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1843. if(s->chroma_y_shift){//Chroma420
  1844. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1845. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1846. }else{
  1847. //chroma422
  1848. dct_linesize = uvlinesize << s->interlaced_dct;
  1849. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1850. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1851. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1852. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1853. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1854. if(!s->chroma_x_shift){//Chroma444
  1855. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  1856. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  1857. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  1858. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  1859. }
  1860. }
  1861. }//fi gray
  1862. }
  1863. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1864. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1865. }
  1866. } else {
  1867. /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
  1868. TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
  1869. if (s->avctx->bits_per_raw_sample > 8){
  1870. const int act_block_size = block_size * 2;
  1871. if(s->dpcm_direction == 0) {
  1872. s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)(*s->block32)[0]);
  1873. s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)(*s->block32)[1]);
  1874. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)(*s->block32)[2]);
  1875. s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)(*s->block32)[3]);
  1876. dct_linesize = uvlinesize << s->interlaced_dct;
  1877. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1878. s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)(*s->block32)[4]);
  1879. s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)(*s->block32)[5]);
  1880. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)(*s->block32)[6]);
  1881. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)(*s->block32)[7]);
  1882. if(!s->chroma_x_shift){//Chroma444
  1883. s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)(*s->block32)[8]);
  1884. s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)(*s->block32)[9]);
  1885. s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[10]);
  1886. s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[11]);
  1887. }
  1888. } else if(s->dpcm_direction == 1) {
  1889. int i, w, h;
  1890. uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
  1891. int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
  1892. for(i = 0; i < 3; i++) {
  1893. int idx = 0;
  1894. int vsub = i ? s->chroma_y_shift : 0;
  1895. int hsub = i ? s->chroma_x_shift : 0;
  1896. for(h = 0; h < (16 >> vsub); h++){
  1897. for(w = 0; w < (16 >> hsub); w++)
  1898. dest_pcm[i][w] = (*s->dpcm_macroblock)[i][idx++];
  1899. dest_pcm[i] += linesize[i] / 2;
  1900. }
  1901. }
  1902. } else if(s->dpcm_direction == -1) {
  1903. int i, w, h;
  1904. uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
  1905. int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
  1906. for(i = 0; i < 3; i++) {
  1907. int idx = 0;
  1908. int vsub = i ? s->chroma_y_shift : 0;
  1909. int hsub = i ? s->chroma_x_shift : 0;
  1910. dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub) - 1);
  1911. for(h = (16 >> vsub)-1; h >= 1; h--){
  1912. for(w = (16 >> hsub)-1; w >= 1; w--)
  1913. dest_pcm[i][w] = (*s->dpcm_macroblock)[i][idx++];
  1914. dest_pcm[i] -= linesize[i] / 2;
  1915. }
  1916. }
  1917. }
  1918. }
  1919. /* dct only in intra block */
  1920. else if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1921. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1922. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1923. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1924. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1925. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1926. if(s->chroma_y_shift){
  1927. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1928. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1929. }else{
  1930. dct_offset >>=1;
  1931. dct_linesize >>=1;
  1932. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1933. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1934. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1935. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1936. }
  1937. }
  1938. }else{
  1939. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  1940. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1941. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  1942. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1943. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1944. if(s->chroma_y_shift){
  1945. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  1946. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  1947. }else{
  1948. dct_linesize = uvlinesize << s->interlaced_dct;
  1949. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  1950. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  1951. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  1952. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1953. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1954. if(!s->chroma_x_shift){//Chroma444
  1955. s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  1956. s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  1957. s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  1958. s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  1959. }
  1960. }
  1961. }//gray
  1962. }
  1963. }
  1964. skip_idct:
  1965. if(!readable){
  1966. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1967. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1968. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1969. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1970. }
  1971. }
  1972. }
  1973. }
  1974. void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
  1975. {
  1976. #if !CONFIG_SMALL
  1977. if(s->out_format == FMT_MPEG1) {
  1978. if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 1);
  1979. else mpv_reconstruct_mb_internal(s, block, 0, 1);
  1980. } else
  1981. #endif
  1982. if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 0);
  1983. else mpv_reconstruct_mb_internal(s, block, 0, 0);
  1984. }
  1985. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1986. {
  1987. ff_draw_horiz_band(s->avctx, s->current_picture_ptr->f,
  1988. s->last_picture_ptr ? s->last_picture_ptr->f : NULL, y, h, s->picture_structure,
  1989. s->first_field, s->low_delay);
  1990. }
  1991. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1992. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1993. const int uvlinesize = s->current_picture.f->linesize[1];
  1994. const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
  1995. const int height_of_mb = 4 - s->avctx->lowres;
  1996. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1997. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1998. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1999. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2000. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2001. 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;
  2002. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2003. s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << width_of_mb);
  2004. s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
  2005. s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
  2006. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2007. {
  2008. if(s->picture_structure==PICT_FRAME){
  2009. s->dest[0] += s->mb_y * linesize << height_of_mb;
  2010. s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2011. s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2012. }else{
  2013. s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
  2014. s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2015. s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
  2016. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2017. }
  2018. }
  2019. }
  2020. void ff_mpeg_flush(AVCodecContext *avctx){
  2021. int i;
  2022. MpegEncContext *s = avctx->priv_data;
  2023. if (!s || !s->picture)
  2024. return;
  2025. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2026. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  2027. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2028. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  2029. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  2030. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  2031. s->mb_x= s->mb_y= 0;
  2032. s->closed_gop= 0;
  2033. s->parse_context.state= -1;
  2034. s->parse_context.frame_start_found= 0;
  2035. s->parse_context.overread= 0;
  2036. s->parse_context.overread_index= 0;
  2037. s->parse_context.index= 0;
  2038. s->parse_context.last_index= 0;
  2039. s->bitstream_buffer_size=0;
  2040. s->pp_time=0;
  2041. }
  2042. /**
  2043. * set qscale and update qscale dependent variables.
  2044. */
  2045. void ff_set_qscale(MpegEncContext * s, int qscale)
  2046. {
  2047. if (qscale < 1)
  2048. qscale = 1;
  2049. else if (qscale > 31)
  2050. qscale = 31;
  2051. s->qscale = qscale;
  2052. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2053. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2054. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2055. }
  2056. void ff_mpv_report_decode_progress(MpegEncContext *s)
  2057. {
  2058. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2059. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2060. }