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.

1825 lines
65KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; 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/timer.h"
  33. #include "avcodec.h"
  34. #include "blockdsp.h"
  35. #include "idctdsp.h"
  36. #include "internal.h"
  37. #include "mathops.h"
  38. #include "mpeg_er.h"
  39. #include "mpegutils.h"
  40. #include "mpegvideo.h"
  41. #include "mpegvideodata.h"
  42. #include "mjpegenc.h"
  43. #include "msmpeg4.h"
  44. #include "qpeldsp.h"
  45. #include "xvmc_internal.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. if (n < 4)
  56. block[0] = block[0] * s->y_dc_scale;
  57. else
  58. block[0] = block[0] * s->c_dc_scale;
  59. /* XXX: only MPEG-1 */
  60. quant_matrix = s->intra_matrix;
  61. for(i=1;i<=nCoeffs;i++) {
  62. int j= s->intra_scantable.permutated[i];
  63. level = block[j];
  64. if (level) {
  65. if (level < 0) {
  66. level = -level;
  67. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  68. level = (level - 1) | 1;
  69. level = -level;
  70. } else {
  71. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  72. level = (level - 1) | 1;
  73. }
  74. block[j] = level;
  75. }
  76. }
  77. }
  78. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  79. int16_t *block, int n, int qscale)
  80. {
  81. int i, level, nCoeffs;
  82. const uint16_t *quant_matrix;
  83. nCoeffs= s->block_last_index[n];
  84. quant_matrix = s->inter_matrix;
  85. for(i=0; i<=nCoeffs; i++) {
  86. int j= s->intra_scantable.permutated[i];
  87. level = block[j];
  88. if (level) {
  89. if (level < 0) {
  90. level = -level;
  91. level = (((level << 1) + 1) * qscale *
  92. ((int) (quant_matrix[j]))) >> 4;
  93. level = (level - 1) | 1;
  94. level = -level;
  95. } else {
  96. level = (((level << 1) + 1) * qscale *
  97. ((int) (quant_matrix[j]))) >> 4;
  98. level = (level - 1) | 1;
  99. }
  100. block[j] = level;
  101. }
  102. }
  103. }
  104. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  105. int16_t *block, int n, int qscale)
  106. {
  107. int i, level, nCoeffs;
  108. const uint16_t *quant_matrix;
  109. if(s->alternate_scan) nCoeffs= 63;
  110. else nCoeffs= s->block_last_index[n];
  111. if (n < 4)
  112. block[0] = block[0] * s->y_dc_scale;
  113. else
  114. block[0] = block[0] * s->c_dc_scale;
  115. quant_matrix = s->intra_matrix;
  116. for(i=1;i<=nCoeffs;i++) {
  117. int j= s->intra_scantable.permutated[i];
  118. level = block[j];
  119. if (level) {
  120. if (level < 0) {
  121. level = -level;
  122. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  123. level = -level;
  124. } else {
  125. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  126. }
  127. block[j] = level;
  128. }
  129. }
  130. }
  131. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  132. int16_t *block, int n, int qscale)
  133. {
  134. int i, level, nCoeffs;
  135. const uint16_t *quant_matrix;
  136. int sum=-1;
  137. if(s->alternate_scan) nCoeffs= 63;
  138. else nCoeffs= s->block_last_index[n];
  139. if (n < 4)
  140. block[0] = block[0] * s->y_dc_scale;
  141. else
  142. block[0] = block[0] * s->c_dc_scale;
  143. quant_matrix = s->intra_matrix;
  144. for(i=1;i<=nCoeffs;i++) {
  145. int j= s->intra_scantable.permutated[i];
  146. level = block[j];
  147. if (level) {
  148. if (level < 0) {
  149. level = -level;
  150. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  151. level = -level;
  152. } else {
  153. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  154. }
  155. block[j] = level;
  156. sum+=level;
  157. }
  158. }
  159. block[63]^=sum&1;
  160. }
  161. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  162. int16_t *block, int n, int qscale)
  163. {
  164. int i, level, nCoeffs;
  165. const uint16_t *quant_matrix;
  166. int sum=-1;
  167. if(s->alternate_scan) nCoeffs= 63;
  168. else nCoeffs= s->block_last_index[n];
  169. quant_matrix = s->inter_matrix;
  170. for(i=0; i<=nCoeffs; i++) {
  171. int j= s->intra_scantable.permutated[i];
  172. level = block[j];
  173. if (level) {
  174. if (level < 0) {
  175. level = -level;
  176. level = (((level << 1) + 1) * qscale *
  177. ((int) (quant_matrix[j]))) >> 4;
  178. level = -level;
  179. } else {
  180. level = (((level << 1) + 1) * qscale *
  181. ((int) (quant_matrix[j]))) >> 4;
  182. }
  183. block[j] = level;
  184. sum+=level;
  185. }
  186. }
  187. block[63]^=sum&1;
  188. }
  189. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  190. int16_t *block, int n, int qscale)
  191. {
  192. int i, level, qmul, qadd;
  193. int nCoeffs;
  194. assert(s->block_last_index[n]>=0);
  195. qmul = qscale << 1;
  196. if (!s->h263_aic) {
  197. if (n < 4)
  198. block[0] = block[0] * s->y_dc_scale;
  199. else
  200. block[0] = block[0] * s->c_dc_scale;
  201. qadd = (qscale - 1) | 1;
  202. }else{
  203. qadd = 0;
  204. }
  205. if(s->ac_pred)
  206. nCoeffs=63;
  207. else
  208. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  209. for(i=1; i<=nCoeffs; i++) {
  210. level = block[i];
  211. if (level) {
  212. if (level < 0) {
  213. level = level * qmul - qadd;
  214. } else {
  215. level = level * qmul + qadd;
  216. }
  217. block[i] = level;
  218. }
  219. }
  220. }
  221. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  222. int16_t *block, int n, int qscale)
  223. {
  224. int i, level, qmul, qadd;
  225. int nCoeffs;
  226. assert(s->block_last_index[n]>=0);
  227. qadd = (qscale - 1) | 1;
  228. qmul = qscale << 1;
  229. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  230. for(i=0; i<=nCoeffs; i++) {
  231. level = block[i];
  232. if (level) {
  233. if (level < 0) {
  234. level = level * qmul - qadd;
  235. } else {
  236. level = level * qmul + qadd;
  237. }
  238. block[i] = level;
  239. }
  240. }
  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_hpeldsp_init(&s->hdsp, s->avctx->flags);
  247. ff_mpegvideodsp_init(&s->mdsp);
  248. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  249. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  250. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  251. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  252. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  253. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  254. if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
  255. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  256. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  257. if (HAVE_INTRINSICS_NEON)
  258. ff_mpv_common_init_neon(s);
  259. if (ARCH_ARM)
  260. ff_mpv_common_init_arm(s);
  261. if (ARCH_PPC)
  262. ff_mpv_common_init_ppc(s);
  263. if (ARCH_X86)
  264. ff_mpv_common_init_x86(s);
  265. return 0;
  266. }
  267. av_cold void ff_mpv_idct_init(MpegEncContext *s)
  268. {
  269. ff_idctdsp_init(&s->idsp, s->avctx);
  270. /* load & permutate scantables
  271. * note: only wmv uses different ones
  272. */
  273. if (s->alternate_scan) {
  274. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  275. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  276. } else {
  277. ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  278. ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  279. }
  280. ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  281. ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  282. }
  283. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  284. {
  285. return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
  286. s->chroma_x_shift, s->chroma_y_shift, s->out_format,
  287. s->mb_stride, s->mb_height, s->b8_stride,
  288. &s->linesize, &s->uvlinesize);
  289. }
  290. static int init_duplicate_context(MpegEncContext *s)
  291. {
  292. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  293. int c_size = s->mb_stride * (s->mb_height + 1);
  294. int yc_size = y_size + 2 * c_size;
  295. int i;
  296. s->sc.edge_emu_buffer =
  297. s->me.scratchpad =
  298. s->me.temp =
  299. s->sc.rd_scratchpad =
  300. s->sc.b_scratchpad =
  301. s->sc.obmc_scratchpad = NULL;
  302. if (s->encoding) {
  303. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  304. ME_MAP_SIZE * sizeof(uint32_t), fail)
  305. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  306. ME_MAP_SIZE * sizeof(uint32_t), fail)
  307. if (s->noise_reduction) {
  308. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  309. 2 * 64 * sizeof(int), fail)
  310. }
  311. }
  312. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  313. s->block = s->blocks[0];
  314. for (i = 0; i < 12; i++) {
  315. s->pblocks[i] = &s->block[i];
  316. }
  317. if (s->avctx->codec_tag == AV_RL32("VCR2")) {
  318. // exchange uv
  319. int16_t (*tmp)[64];
  320. tmp = s->pblocks[4];
  321. s->pblocks[4] = s->pblocks[5];
  322. s->pblocks[5] = tmp;
  323. }
  324. if (s->out_format == FMT_H263) {
  325. /* ac values */
  326. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  327. yc_size * sizeof(int16_t) * 16, fail);
  328. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  329. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  330. s->ac_val[2] = s->ac_val[1] + c_size;
  331. }
  332. return 0;
  333. fail:
  334. return -1; // free() through ff_mpv_common_end()
  335. }
  336. static void free_duplicate_context(MpegEncContext *s)
  337. {
  338. if (!s)
  339. return;
  340. av_freep(&s->sc.edge_emu_buffer);
  341. av_freep(&s->me.scratchpad);
  342. s->me.temp =
  343. s->sc.rd_scratchpad =
  344. s->sc.b_scratchpad =
  345. s->sc.obmc_scratchpad = NULL;
  346. av_freep(&s->dct_error_sum);
  347. av_freep(&s->me.map);
  348. av_freep(&s->me.score_map);
  349. av_freep(&s->blocks);
  350. av_freep(&s->ac_val_base);
  351. s->block = NULL;
  352. }
  353. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  354. {
  355. #define COPY(a) bak->a = src->a
  356. COPY(sc.edge_emu_buffer);
  357. COPY(me.scratchpad);
  358. COPY(me.temp);
  359. COPY(sc.rd_scratchpad);
  360. COPY(sc.b_scratchpad);
  361. COPY(sc.obmc_scratchpad);
  362. COPY(me.map);
  363. COPY(me.score_map);
  364. COPY(blocks);
  365. COPY(block);
  366. COPY(start_mb_y);
  367. COPY(end_mb_y);
  368. COPY(me.map_generation);
  369. COPY(pb);
  370. COPY(dct_error_sum);
  371. COPY(dct_count[0]);
  372. COPY(dct_count[1]);
  373. COPY(ac_val_base);
  374. COPY(ac_val[0]);
  375. COPY(ac_val[1]);
  376. COPY(ac_val[2]);
  377. #undef COPY
  378. }
  379. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  380. {
  381. MpegEncContext bak;
  382. int i, ret;
  383. // FIXME copy only needed parts
  384. // START_TIMER
  385. backup_duplicate_context(&bak, dst);
  386. memcpy(dst, src, sizeof(MpegEncContext));
  387. backup_duplicate_context(dst, &bak);
  388. for (i = 0; i < 12; i++) {
  389. dst->pblocks[i] = &dst->block[i];
  390. }
  391. if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
  392. // exchange uv
  393. int16_t (*tmp)[64];
  394. tmp = dst->pblocks[4];
  395. dst->pblocks[4] = dst->pblocks[5];
  396. dst->pblocks[5] = tmp;
  397. }
  398. if (!dst->sc.edge_emu_buffer &&
  399. (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
  400. &dst->sc, dst->linesize)) < 0) {
  401. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  402. "scratch buffers.\n");
  403. return ret;
  404. }
  405. // STOP_TIMER("update_duplicate_context")
  406. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  407. return 0;
  408. }
  409. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  410. const AVCodecContext *src)
  411. {
  412. int i, ret;
  413. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  414. if (dst == src || !s1->context_initialized)
  415. return 0;
  416. // FIXME can parameters change on I-frames?
  417. // in that case dst may need a reinit
  418. if (!s->context_initialized) {
  419. int err;
  420. memcpy(s, s1, sizeof(MpegEncContext));
  421. s->avctx = dst;
  422. s->bitstream_buffer = NULL;
  423. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  424. ff_mpv_idct_init(s);
  425. if ((err = ff_mpv_common_init(s)) < 0)
  426. return err;
  427. }
  428. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  429. int err;
  430. s->context_reinit = 0;
  431. s->height = s1->height;
  432. s->width = s1->width;
  433. if ((err = ff_mpv_common_frame_size_change(s)) < 0)
  434. return err;
  435. }
  436. s->avctx->coded_height = s1->avctx->coded_height;
  437. s->avctx->coded_width = s1->avctx->coded_width;
  438. s->avctx->width = s1->avctx->width;
  439. s->avctx->height = s1->avctx->height;
  440. s->coded_picture_number = s1->coded_picture_number;
  441. s->picture_number = s1->picture_number;
  442. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  443. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  444. if (s1->picture[i].f->buf[0] &&
  445. (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
  446. return ret;
  447. }
  448. #define UPDATE_PICTURE(pic)\
  449. do {\
  450. ff_mpeg_unref_picture(s->avctx, &s->pic);\
  451. if (s1->pic.f->buf[0])\
  452. ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
  453. else\
  454. ret = ff_update_picture_tables(&s->pic, &s1->pic);\
  455. if (ret < 0)\
  456. return ret;\
  457. } while (0)
  458. UPDATE_PICTURE(current_picture);
  459. UPDATE_PICTURE(last_picture);
  460. UPDATE_PICTURE(next_picture);
  461. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  462. ((pic && pic >= old_ctx->picture && \
  463. pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
  464. &new_ctx->picture[pic - old_ctx->picture] : NULL)
  465. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  466. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  467. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  468. // Error/bug resilience
  469. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  470. s->workaround_bugs = s1->workaround_bugs;
  471. // MPEG-4 timing info
  472. memcpy(&s->last_time_base, &s1->last_time_base,
  473. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  474. (char *) &s1->last_time_base);
  475. // B-frame info
  476. s->max_b_frames = s1->max_b_frames;
  477. s->low_delay = s1->low_delay;
  478. s->droppable = s1->droppable;
  479. // DivX handling (doesn't work)
  480. s->divx_packed = s1->divx_packed;
  481. if (s1->bitstream_buffer) {
  482. if (s1->bitstream_buffer_size +
  483. AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  484. av_fast_malloc(&s->bitstream_buffer,
  485. &s->allocated_bitstream_buffer_size,
  486. s1->allocated_bitstream_buffer_size);
  487. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  488. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  489. s1->bitstream_buffer_size);
  490. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  491. AV_INPUT_BUFFER_PADDING_SIZE);
  492. }
  493. // linesize-dependent scratch buffer allocation
  494. if (!s->sc.edge_emu_buffer)
  495. if (s1->linesize) {
  496. if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
  497. &s->sc, s1->linesize) < 0) {
  498. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  499. "scratch buffers.\n");
  500. return AVERROR(ENOMEM);
  501. }
  502. } else {
  503. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  504. "be allocated due to unknown size.\n");
  505. return AVERROR_BUG;
  506. }
  507. // MPEG-2/interlacing info
  508. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  509. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  510. if (!s1->first_field) {
  511. s->last_pict_type = s1->pict_type;
  512. if (s1->current_picture_ptr)
  513. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  514. }
  515. return 0;
  516. }
  517. /**
  518. * Set the given MpegEncContext to common defaults
  519. * (same for encoding and decoding).
  520. * The changed fields will not depend upon the
  521. * prior state of the MpegEncContext.
  522. */
  523. void ff_mpv_common_defaults(MpegEncContext *s)
  524. {
  525. s->y_dc_scale_table =
  526. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  527. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  528. s->progressive_frame = 1;
  529. s->progressive_sequence = 1;
  530. s->picture_structure = PICT_FRAME;
  531. s->coded_picture_number = 0;
  532. s->picture_number = 0;
  533. s->f_code = 1;
  534. s->b_code = 1;
  535. s->slice_context_count = 1;
  536. }
  537. /**
  538. * Set the given MpegEncContext to defaults for decoding.
  539. * the changed fields will not depend upon
  540. * the prior state of the MpegEncContext.
  541. */
  542. void ff_mpv_decode_defaults(MpegEncContext *s)
  543. {
  544. ff_mpv_common_defaults(s);
  545. }
  546. /**
  547. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  548. */
  549. static int init_context_frame(MpegEncContext *s)
  550. {
  551. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  552. s->mb_width = (s->width + 15) / 16;
  553. s->mb_stride = s->mb_width + 1;
  554. s->b8_stride = s->mb_width * 2 + 1;
  555. mb_array_size = s->mb_height * s->mb_stride;
  556. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  557. /* set default edge pos, will be overridden
  558. * in decode_header if needed */
  559. s->h_edge_pos = s->mb_width * 16;
  560. s->v_edge_pos = s->mb_height * 16;
  561. s->mb_num = s->mb_width * s->mb_height;
  562. s->block_wrap[0] =
  563. s->block_wrap[1] =
  564. s->block_wrap[2] =
  565. s->block_wrap[3] = s->b8_stride;
  566. s->block_wrap[4] =
  567. s->block_wrap[5] = s->mb_stride;
  568. y_size = s->b8_stride * (2 * s->mb_height + 1);
  569. c_size = s->mb_stride * (s->mb_height + 1);
  570. yc_size = y_size + 2 * c_size;
  571. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  572. fail); // error resilience code looks cleaner with this
  573. for (y = 0; y < s->mb_height; y++)
  574. for (x = 0; x < s->mb_width; x++)
  575. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  576. s->mb_index2xy[s->mb_height * s->mb_width] =
  577. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  578. if (s->encoding) {
  579. /* Allocate MV tables */
  580. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  581. mv_table_size * 2 * sizeof(int16_t), fail);
  582. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  583. mv_table_size * 2 * sizeof(int16_t), fail);
  584. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  585. mv_table_size * 2 * sizeof(int16_t), fail);
  586. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  587. mv_table_size * 2 * sizeof(int16_t), fail);
  588. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  589. mv_table_size * 2 * sizeof(int16_t), fail);
  590. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  591. mv_table_size * 2 * sizeof(int16_t), fail);
  592. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  593. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  594. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  595. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  596. s->mb_stride + 1;
  597. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  598. s->mb_stride + 1;
  599. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  600. /* Allocate MB type table */
  601. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  602. sizeof(uint16_t), fail); // needed for encoding
  603. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  604. sizeof(int), fail);
  605. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  606. mb_array_size * sizeof(float), fail);
  607. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  608. mb_array_size * sizeof(float), fail);
  609. }
  610. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  611. (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
  612. /* interlaced direct mode decoding tables */
  613. for (i = 0; i < 2; i++) {
  614. int j, k;
  615. for (j = 0; j < 2; j++) {
  616. for (k = 0; k < 2; k++) {
  617. FF_ALLOCZ_OR_GOTO(s->avctx,
  618. s->b_field_mv_table_base[i][j][k],
  619. mv_table_size * 2 * sizeof(int16_t),
  620. fail);
  621. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  622. s->mb_stride + 1;
  623. }
  624. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  625. mb_array_size * 2 * sizeof(uint8_t), fail);
  626. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  627. mv_table_size * 2 * sizeof(int16_t), fail);
  628. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  629. + s->mb_stride + 1;
  630. }
  631. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  632. mb_array_size * 2 * sizeof(uint8_t), fail);
  633. }
  634. }
  635. if (s->out_format == FMT_H263) {
  636. /* cbp values */
  637. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  638. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  639. /* cbp, ac_pred, pred_dir */
  640. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  641. mb_array_size * sizeof(uint8_t), fail);
  642. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  643. mb_array_size * sizeof(uint8_t), fail);
  644. }
  645. if (s->h263_pred || s->h263_plus || !s->encoding) {
  646. /* dc values */
  647. // MN: we need these for error resilience of intra-frames
  648. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  649. yc_size * sizeof(int16_t), fail);
  650. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  651. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  652. s->dc_val[2] = s->dc_val[1] + c_size;
  653. for (i = 0; i < yc_size; i++)
  654. s->dc_val_base[i] = 1024;
  655. }
  656. /* which mb is a intra block */
  657. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  658. memset(s->mbintra_table, 1, mb_array_size);
  659. /* init macroblock skip table */
  660. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  661. // Note the + 1 is for a quicker MPEG-4 slice_end detection
  662. return ff_mpeg_er_init(s);
  663. fail:
  664. return AVERROR(ENOMEM);
  665. }
  666. /**
  667. * init common structure for both encoder and decoder.
  668. * this assumes that some variables like width/height are already set
  669. */
  670. av_cold int ff_mpv_common_init(MpegEncContext *s)
  671. {
  672. int i;
  673. int nb_slices = (HAVE_THREADS &&
  674. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  675. s->avctx->thread_count : 1;
  676. if (s->encoding && s->avctx->slices)
  677. nb_slices = s->avctx->slices;
  678. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  679. s->mb_height = (s->height + 31) / 32 * 2;
  680. else
  681. s->mb_height = (s->height + 15) / 16;
  682. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  683. av_log(s->avctx, AV_LOG_ERROR,
  684. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  685. return -1;
  686. }
  687. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  688. int max_slices;
  689. if (s->mb_height)
  690. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  691. else
  692. max_slices = MAX_THREADS;
  693. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  694. " reducing to %d\n", nb_slices, max_slices);
  695. nb_slices = max_slices;
  696. }
  697. if ((s->width || s->height) &&
  698. av_image_check_size(s->width, s->height, 0, s->avctx))
  699. return -1;
  700. dct_init(s);
  701. /* set chroma shifts */
  702. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  703. &s->chroma_x_shift,
  704. &s->chroma_y_shift);
  705. /* convert fourcc to upper case */
  706. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  707. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  708. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  709. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  710. s->picture[i].f = av_frame_alloc();
  711. if (!s->picture[i].f)
  712. goto fail;
  713. }
  714. memset(&s->next_picture, 0, sizeof(s->next_picture));
  715. memset(&s->last_picture, 0, sizeof(s->last_picture));
  716. memset(&s->current_picture, 0, sizeof(s->current_picture));
  717. memset(&s->new_picture, 0, sizeof(s->new_picture));
  718. s->next_picture.f = av_frame_alloc();
  719. if (!s->next_picture.f)
  720. goto fail;
  721. s->last_picture.f = av_frame_alloc();
  722. if (!s->last_picture.f)
  723. goto fail;
  724. s->current_picture.f = av_frame_alloc();
  725. if (!s->current_picture.f)
  726. goto fail;
  727. s->new_picture.f = av_frame_alloc();
  728. if (!s->new_picture.f)
  729. goto fail;
  730. if (s->width && s->height) {
  731. if (init_context_frame(s))
  732. goto fail;
  733. s->parse_context.state = -1;
  734. }
  735. s->context_initialized = 1;
  736. s->thread_context[0] = s;
  737. if (s->width && s->height) {
  738. if (nb_slices > 1) {
  739. for (i = 1; i < nb_slices; i++) {
  740. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  741. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  742. }
  743. for (i = 0; i < nb_slices; i++) {
  744. if (init_duplicate_context(s->thread_context[i]) < 0)
  745. goto fail;
  746. s->thread_context[i]->start_mb_y =
  747. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  748. s->thread_context[i]->end_mb_y =
  749. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  750. }
  751. } else {
  752. if (init_duplicate_context(s) < 0)
  753. goto fail;
  754. s->start_mb_y = 0;
  755. s->end_mb_y = s->mb_height;
  756. }
  757. s->slice_context_count = nb_slices;
  758. }
  759. return 0;
  760. fail:
  761. ff_mpv_common_end(s);
  762. return -1;
  763. }
  764. /**
  765. * Frees and resets MpegEncContext fields depending on the resolution.
  766. * Is used during resolution changes to avoid a full reinitialization of the
  767. * codec.
  768. */
  769. static void free_context_frame(MpegEncContext *s)
  770. {
  771. int i, j, k;
  772. av_freep(&s->mb_type);
  773. av_freep(&s->p_mv_table_base);
  774. av_freep(&s->b_forw_mv_table_base);
  775. av_freep(&s->b_back_mv_table_base);
  776. av_freep(&s->b_bidir_forw_mv_table_base);
  777. av_freep(&s->b_bidir_back_mv_table_base);
  778. av_freep(&s->b_direct_mv_table_base);
  779. s->p_mv_table = NULL;
  780. s->b_forw_mv_table = NULL;
  781. s->b_back_mv_table = NULL;
  782. s->b_bidir_forw_mv_table = NULL;
  783. s->b_bidir_back_mv_table = NULL;
  784. s->b_direct_mv_table = NULL;
  785. for (i = 0; i < 2; i++) {
  786. for (j = 0; j < 2; j++) {
  787. for (k = 0; k < 2; k++) {
  788. av_freep(&s->b_field_mv_table_base[i][j][k]);
  789. s->b_field_mv_table[i][j][k] = NULL;
  790. }
  791. av_freep(&s->b_field_select_table[i][j]);
  792. av_freep(&s->p_field_mv_table_base[i][j]);
  793. s->p_field_mv_table[i][j] = NULL;
  794. }
  795. av_freep(&s->p_field_select_table[i]);
  796. }
  797. av_freep(&s->dc_val_base);
  798. av_freep(&s->coded_block_base);
  799. av_freep(&s->mbintra_table);
  800. av_freep(&s->cbp_table);
  801. av_freep(&s->pred_dir_table);
  802. av_freep(&s->mbskip_table);
  803. av_freep(&s->er.error_status_table);
  804. av_freep(&s->er.er_temp_buffer);
  805. av_freep(&s->mb_index2xy);
  806. av_freep(&s->lambda_table);
  807. av_freep(&s->cplx_tab);
  808. av_freep(&s->bits_tab);
  809. s->linesize = s->uvlinesize = 0;
  810. }
  811. int ff_mpv_common_frame_size_change(MpegEncContext *s)
  812. {
  813. int i, err = 0;
  814. if (s->slice_context_count > 1) {
  815. for (i = 0; i < s->slice_context_count; i++) {
  816. free_duplicate_context(s->thread_context[i]);
  817. }
  818. for (i = 1; i < s->slice_context_count; i++) {
  819. av_freep(&s->thread_context[i]);
  820. }
  821. } else
  822. free_duplicate_context(s);
  823. free_context_frame(s);
  824. if (s->picture)
  825. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  826. s->picture[i].needs_realloc = 1;
  827. }
  828. s->last_picture_ptr =
  829. s->next_picture_ptr =
  830. s->current_picture_ptr = NULL;
  831. // init
  832. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  833. s->mb_height = (s->height + 31) / 32 * 2;
  834. else
  835. s->mb_height = (s->height + 15) / 16;
  836. if ((s->width || s->height) &&
  837. (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  838. goto fail;
  839. if ((err = init_context_frame(s)))
  840. goto fail;
  841. s->thread_context[0] = s;
  842. if (s->width && s->height) {
  843. int nb_slices = s->slice_context_count;
  844. if (nb_slices > 1) {
  845. for (i = 1; i < nb_slices; i++) {
  846. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  847. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  848. }
  849. for (i = 0; i < nb_slices; i++) {
  850. if ((err = init_duplicate_context(s->thread_context[i])) < 0)
  851. goto fail;
  852. s->thread_context[i]->start_mb_y =
  853. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  854. s->thread_context[i]->end_mb_y =
  855. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  856. }
  857. } else {
  858. if (init_duplicate_context(s) < 0)
  859. goto fail;
  860. s->start_mb_y = 0;
  861. s->end_mb_y = s->mb_height;
  862. }
  863. s->slice_context_count = nb_slices;
  864. }
  865. return 0;
  866. fail:
  867. ff_mpv_common_end(s);
  868. return err;
  869. }
  870. /* init common structure for both encoder and decoder */
  871. void ff_mpv_common_end(MpegEncContext *s)
  872. {
  873. int i;
  874. if (s->slice_context_count > 1) {
  875. for (i = 0; i < s->slice_context_count; i++) {
  876. free_duplicate_context(s->thread_context[i]);
  877. }
  878. for (i = 1; i < s->slice_context_count; i++) {
  879. av_freep(&s->thread_context[i]);
  880. }
  881. s->slice_context_count = 1;
  882. } else free_duplicate_context(s);
  883. av_freep(&s->parse_context.buffer);
  884. s->parse_context.buffer_size = 0;
  885. av_freep(&s->bitstream_buffer);
  886. s->allocated_bitstream_buffer_size = 0;
  887. if (s->picture) {
  888. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  889. ff_free_picture_tables(&s->picture[i]);
  890. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  891. av_frame_free(&s->picture[i].f);
  892. }
  893. }
  894. av_freep(&s->picture);
  895. ff_free_picture_tables(&s->last_picture);
  896. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  897. av_frame_free(&s->last_picture.f);
  898. ff_free_picture_tables(&s->current_picture);
  899. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  900. av_frame_free(&s->current_picture.f);
  901. ff_free_picture_tables(&s->next_picture);
  902. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  903. av_frame_free(&s->next_picture.f);
  904. ff_free_picture_tables(&s->new_picture);
  905. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  906. av_frame_free(&s->new_picture.f);
  907. free_context_frame(s);
  908. s->context_initialized = 0;
  909. s->last_picture_ptr =
  910. s->next_picture_ptr =
  911. s->current_picture_ptr = NULL;
  912. s->linesize = s->uvlinesize = 0;
  913. }
  914. /**
  915. * generic function called after decoding
  916. * the header and before a frame is decoded.
  917. */
  918. int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  919. {
  920. int i, ret;
  921. Picture *pic;
  922. s->mb_skipped = 0;
  923. /* mark & release old frames */
  924. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  925. s->last_picture_ptr != s->next_picture_ptr &&
  926. s->last_picture_ptr->f->buf[0]) {
  927. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  928. }
  929. /* release forgotten pictures */
  930. /* if (MPEG-124 / H.263) */
  931. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  932. if (&s->picture[i] != s->last_picture_ptr &&
  933. &s->picture[i] != s->next_picture_ptr &&
  934. s->picture[i].reference && !s->picture[i].needs_realloc) {
  935. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  936. }
  937. }
  938. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  939. /* release non reference frames */
  940. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  941. if (!s->picture[i].reference)
  942. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  943. }
  944. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  945. // we already have a unused image
  946. // (maybe it was set before reading the header)
  947. pic = s->current_picture_ptr;
  948. } else {
  949. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  950. if (i < 0) {
  951. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  952. return i;
  953. }
  954. pic = &s->picture[i];
  955. }
  956. pic->reference = 0;
  957. if (!s->droppable) {
  958. if (s->pict_type != AV_PICTURE_TYPE_B)
  959. pic->reference = 3;
  960. }
  961. pic->f->coded_picture_number = s->coded_picture_number++;
  962. if (alloc_picture(s, pic, 0) < 0)
  963. return -1;
  964. s->current_picture_ptr = pic;
  965. // FIXME use only the vars from current_pic
  966. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  967. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  968. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  969. if (s->picture_structure != PICT_FRAME)
  970. s->current_picture_ptr->f->top_field_first =
  971. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  972. }
  973. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  974. !s->progressive_sequence;
  975. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  976. s->current_picture_ptr->f->pict_type = s->pict_type;
  977. // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
  978. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  979. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  980. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  981. s->current_picture_ptr)) < 0)
  982. return ret;
  983. if (s->pict_type != AV_PICTURE_TYPE_B) {
  984. s->last_picture_ptr = s->next_picture_ptr;
  985. if (!s->droppable)
  986. s->next_picture_ptr = s->current_picture_ptr;
  987. }
  988. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  989. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  990. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  991. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  992. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  993. s->pict_type, s->droppable);
  994. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  995. (s->pict_type != AV_PICTURE_TYPE_I ||
  996. s->picture_structure != PICT_FRAME)) {
  997. int h_chroma_shift, v_chroma_shift;
  998. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  999. &h_chroma_shift, &v_chroma_shift);
  1000. if (s->pict_type != AV_PICTURE_TYPE_I)
  1001. av_log(avctx, AV_LOG_ERROR,
  1002. "warning: first frame is no keyframe\n");
  1003. else if (s->picture_structure != PICT_FRAME)
  1004. av_log(avctx, AV_LOG_INFO,
  1005. "allocate dummy last picture for field based first keyframe\n");
  1006. /* Allocate a dummy frame */
  1007. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1008. if (i < 0) {
  1009. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1010. return i;
  1011. }
  1012. s->last_picture_ptr = &s->picture[i];
  1013. s->last_picture_ptr->reference = 3;
  1014. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1015. if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1016. s->last_picture_ptr = NULL;
  1017. return -1;
  1018. }
  1019. memset(s->last_picture_ptr->f->data[0], 0,
  1020. avctx->height * s->last_picture_ptr->f->linesize[0]);
  1021. memset(s->last_picture_ptr->f->data[1], 0x80,
  1022. (avctx->height >> v_chroma_shift) *
  1023. s->last_picture_ptr->f->linesize[1]);
  1024. memset(s->last_picture_ptr->f->data[2], 0x80,
  1025. (avctx->height >> v_chroma_shift) *
  1026. s->last_picture_ptr->f->linesize[2]);
  1027. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1028. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1029. }
  1030. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1031. s->pict_type == AV_PICTURE_TYPE_B) {
  1032. /* Allocate a dummy frame */
  1033. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1034. if (i < 0) {
  1035. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1036. return i;
  1037. }
  1038. s->next_picture_ptr = &s->picture[i];
  1039. s->next_picture_ptr->reference = 3;
  1040. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1041. if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1042. s->next_picture_ptr = NULL;
  1043. return -1;
  1044. }
  1045. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1046. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1047. }
  1048. if (s->last_picture_ptr) {
  1049. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1050. if (s->last_picture_ptr->f->buf[0] &&
  1051. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1052. s->last_picture_ptr)) < 0)
  1053. return ret;
  1054. }
  1055. if (s->next_picture_ptr) {
  1056. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1057. if (s->next_picture_ptr->f->buf[0] &&
  1058. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1059. s->next_picture_ptr)) < 0)
  1060. return ret;
  1061. }
  1062. if (s->pict_type != AV_PICTURE_TYPE_I &&
  1063. !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
  1064. av_log(s, AV_LOG_ERROR,
  1065. "Non-reference picture received and no reference available\n");
  1066. return AVERROR_INVALIDDATA;
  1067. }
  1068. if (s->picture_structure!= PICT_FRAME) {
  1069. int i;
  1070. for (i = 0; i < 4; i++) {
  1071. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1072. s->current_picture.f->data[i] +=
  1073. s->current_picture.f->linesize[i];
  1074. }
  1075. s->current_picture.f->linesize[i] *= 2;
  1076. s->last_picture.f->linesize[i] *= 2;
  1077. s->next_picture.f->linesize[i] *= 2;
  1078. }
  1079. }
  1080. /* set dequantizer, we can't do it during init as
  1081. * it might change for MPEG-4 and we can't do it in the header
  1082. * decode as init is not called for MPEG-4 there yet */
  1083. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1084. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1085. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1086. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1087. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1088. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1089. } else {
  1090. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1091. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1092. }
  1093. #if FF_API_XVMC
  1094. FF_DISABLE_DEPRECATION_WARNINGS
  1095. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1096. return ff_xvmc_field_start(s, avctx);
  1097. FF_ENABLE_DEPRECATION_WARNINGS
  1098. #endif /* FF_API_XVMC */
  1099. return 0;
  1100. }
  1101. /* called after a frame has been decoded. */
  1102. void ff_mpv_frame_end(MpegEncContext *s)
  1103. {
  1104. #if FF_API_XVMC
  1105. FF_DISABLE_DEPRECATION_WARNINGS
  1106. /* redraw edges for the frame if decoding didn't complete */
  1107. // just to make sure that all data is rendered.
  1108. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1109. ff_xvmc_field_end(s);
  1110. } else
  1111. FF_ENABLE_DEPRECATION_WARNINGS
  1112. #endif /* FF_API_XVMC */
  1113. emms_c();
  1114. if (s->current_picture.reference)
  1115. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1116. }
  1117. /**
  1118. * Print debugging info for the given picture.
  1119. */
  1120. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1121. {
  1122. AVFrame *pict;
  1123. if (s->avctx->hwaccel || !p || !p->mb_type)
  1124. return;
  1125. pict = p->f;
  1126. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1127. int x,y;
  1128. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1129. switch (pict->pict_type) {
  1130. case AV_PICTURE_TYPE_I:
  1131. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1132. break;
  1133. case AV_PICTURE_TYPE_P:
  1134. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1135. break;
  1136. case AV_PICTURE_TYPE_B:
  1137. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1138. break;
  1139. case AV_PICTURE_TYPE_S:
  1140. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1141. break;
  1142. case AV_PICTURE_TYPE_SI:
  1143. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1144. break;
  1145. case AV_PICTURE_TYPE_SP:
  1146. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1147. break;
  1148. }
  1149. for (y = 0; y < s->mb_height; y++) {
  1150. for (x = 0; x < s->mb_width; x++) {
  1151. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1152. int count = s->mbskip_table[x + y * s->mb_stride];
  1153. if (count > 9)
  1154. count = 9;
  1155. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1156. }
  1157. if (s->avctx->debug & FF_DEBUG_QP) {
  1158. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1159. p->qscale_table[x + y * s->mb_stride]);
  1160. }
  1161. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1162. int mb_type = p->mb_type[x + y * s->mb_stride];
  1163. // Type & MV direction
  1164. if (IS_PCM(mb_type))
  1165. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1166. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1167. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1168. else if (IS_INTRA4x4(mb_type))
  1169. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1170. else if (IS_INTRA16x16(mb_type))
  1171. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1172. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1173. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1174. else if (IS_DIRECT(mb_type))
  1175. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1176. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1177. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1178. else if (IS_GMC(mb_type))
  1179. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1180. else if (IS_SKIP(mb_type))
  1181. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1182. else if (!USES_LIST(mb_type, 1))
  1183. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1184. else if (!USES_LIST(mb_type, 0))
  1185. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1186. else {
  1187. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1188. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1189. }
  1190. // segmentation
  1191. if (IS_8X8(mb_type))
  1192. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1193. else if (IS_16X8(mb_type))
  1194. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1195. else if (IS_8X16(mb_type))
  1196. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1197. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1198. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1199. else
  1200. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1201. if (IS_INTERLACED(mb_type))
  1202. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1203. else
  1204. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1205. }
  1206. }
  1207. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1208. }
  1209. }
  1210. }
  1211. /**
  1212. * find the lowest MB row referenced in the MVs
  1213. */
  1214. static int lowest_referenced_row(MpegEncContext *s, int dir)
  1215. {
  1216. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1217. int my, off, i, mvs;
  1218. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1219. goto unhandled;
  1220. switch (s->mv_type) {
  1221. case MV_TYPE_16X16:
  1222. mvs = 1;
  1223. break;
  1224. case MV_TYPE_16X8:
  1225. mvs = 2;
  1226. break;
  1227. case MV_TYPE_8X8:
  1228. mvs = 4;
  1229. break;
  1230. default:
  1231. goto unhandled;
  1232. }
  1233. for (i = 0; i < mvs; i++) {
  1234. my = s->mv[dir][i][1]<<qpel_shift;
  1235. my_max = FFMAX(my_max, my);
  1236. my_min = FFMIN(my_min, my);
  1237. }
  1238. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1239. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1240. unhandled:
  1241. return s->mb_height-1;
  1242. }
  1243. /* put block[] to dest[] */
  1244. static inline void put_dct(MpegEncContext *s,
  1245. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1246. {
  1247. s->dct_unquantize_intra(s, block, i, qscale);
  1248. s->idsp.idct_put(dest, line_size, block);
  1249. }
  1250. /* add block[] to dest[] */
  1251. static inline void add_dct(MpegEncContext *s,
  1252. int16_t *block, int i, uint8_t *dest, int line_size)
  1253. {
  1254. if (s->block_last_index[i] >= 0) {
  1255. s->idsp.idct_add(dest, line_size, block);
  1256. }
  1257. }
  1258. static inline void add_dequant_dct(MpegEncContext *s,
  1259. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1260. {
  1261. if (s->block_last_index[i] >= 0) {
  1262. s->dct_unquantize_inter(s, block, i, qscale);
  1263. s->idsp.idct_add(dest, line_size, block);
  1264. }
  1265. }
  1266. /**
  1267. * Clean dc, ac, coded_block for the current non-intra MB.
  1268. */
  1269. void ff_clean_intra_table_entries(MpegEncContext *s)
  1270. {
  1271. int wrap = s->b8_stride;
  1272. int xy = s->block_index[0];
  1273. s->dc_val[0][xy ] =
  1274. s->dc_val[0][xy + 1 ] =
  1275. s->dc_val[0][xy + wrap] =
  1276. s->dc_val[0][xy + 1 + wrap] = 1024;
  1277. /* ac pred */
  1278. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1279. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1280. if (s->msmpeg4_version>=3) {
  1281. s->coded_block[xy ] =
  1282. s->coded_block[xy + 1 ] =
  1283. s->coded_block[xy + wrap] =
  1284. s->coded_block[xy + 1 + wrap] = 0;
  1285. }
  1286. /* chroma */
  1287. wrap = s->mb_stride;
  1288. xy = s->mb_x + s->mb_y * wrap;
  1289. s->dc_val[1][xy] =
  1290. s->dc_val[2][xy] = 1024;
  1291. /* ac pred */
  1292. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1293. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1294. s->mbintra_table[xy]= 0;
  1295. }
  1296. /* generic function called after a macroblock has been parsed by the
  1297. decoder or after it has been encoded by the encoder.
  1298. Important variables used:
  1299. s->mb_intra : true if intra macroblock
  1300. s->mv_dir : motion vector direction
  1301. s->mv_type : motion vector type
  1302. s->mv : motion vector
  1303. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1304. */
  1305. static av_always_inline
  1306. void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1307. int is_mpeg12)
  1308. {
  1309. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1310. #if FF_API_XVMC
  1311. FF_DISABLE_DEPRECATION_WARNINGS
  1312. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1313. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1314. return;
  1315. }
  1316. FF_ENABLE_DEPRECATION_WARNINGS
  1317. #endif /* FF_API_XVMC */
  1318. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1319. /* print DCT coefficients */
  1320. int i,j;
  1321. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1322. for(i=0; i<6; i++){
  1323. for(j=0; j<64; j++){
  1324. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  1325. block[i][s->idsp.idct_permutation[j]]);
  1326. }
  1327. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1328. }
  1329. }
  1330. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1331. /* update DC predictors for P macroblocks */
  1332. if (!s->mb_intra) {
  1333. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1334. if(s->mbintra_table[mb_xy])
  1335. ff_clean_intra_table_entries(s);
  1336. } else {
  1337. s->last_dc[0] =
  1338. s->last_dc[1] =
  1339. s->last_dc[2] = 128 << s->intra_dc_precision;
  1340. }
  1341. }
  1342. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1343. s->mbintra_table[mb_xy]=1;
  1344. if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) ||
  1345. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  1346. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  1347. uint8_t *dest_y, *dest_cb, *dest_cr;
  1348. int dct_linesize, dct_offset;
  1349. op_pixels_func (*op_pix)[4];
  1350. qpel_mc_func (*op_qpix)[16];
  1351. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1352. const int uvlinesize = s->current_picture.f->linesize[1];
  1353. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1354. const int block_size = 8;
  1355. /* avoid copy if macroblock skipped in last frame too */
  1356. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1357. if(!s->encoding){
  1358. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1359. if (s->mb_skipped) {
  1360. s->mb_skipped= 0;
  1361. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1362. *mbskip_ptr = 1;
  1363. } else if(!s->current_picture.reference) {
  1364. *mbskip_ptr = 1;
  1365. } else{
  1366. *mbskip_ptr = 0; /* not skipped */
  1367. }
  1368. }
  1369. dct_linesize = linesize << s->interlaced_dct;
  1370. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1371. if(readable){
  1372. dest_y= s->dest[0];
  1373. dest_cb= s->dest[1];
  1374. dest_cr= s->dest[2];
  1375. }else{
  1376. dest_y = s->sc.b_scratchpad;
  1377. dest_cb= s->sc.b_scratchpad+16*linesize;
  1378. dest_cr= s->sc.b_scratchpad+32*linesize;
  1379. }
  1380. if (!s->mb_intra) {
  1381. /* motion handling */
  1382. /* decoding or more than one mb_type (MC was already done otherwise) */
  1383. if(!s->encoding){
  1384. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1385. if (s->mv_dir & MV_DIR_FORWARD) {
  1386. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1387. lowest_referenced_row(s, 0),
  1388. 0);
  1389. }
  1390. if (s->mv_dir & MV_DIR_BACKWARD) {
  1391. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1392. lowest_referenced_row(s, 1),
  1393. 0);
  1394. }
  1395. }
  1396. op_qpix= s->me.qpel_put;
  1397. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1398. op_pix = s->hdsp.put_pixels_tab;
  1399. }else{
  1400. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1401. }
  1402. if (s->mv_dir & MV_DIR_FORWARD) {
  1403. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  1404. op_pix = s->hdsp.avg_pixels_tab;
  1405. op_qpix= s->me.qpel_avg;
  1406. }
  1407. if (s->mv_dir & MV_DIR_BACKWARD) {
  1408. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  1409. }
  1410. }
  1411. /* skip dequant / idct if we are really late ;) */
  1412. if(s->avctx->skip_idct){
  1413. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1414. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1415. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1416. goto skip_idct;
  1417. }
  1418. /* add dct residue */
  1419. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1420. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1421. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1422. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1423. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1424. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1425. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1426. if (s->chroma_y_shift){
  1427. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1428. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1429. }else{
  1430. dct_linesize >>= 1;
  1431. dct_offset >>=1;
  1432. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1433. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1434. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1435. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1436. }
  1437. }
  1438. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1439. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1440. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1441. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1442. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1443. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1444. if(s->chroma_y_shift){//Chroma420
  1445. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1446. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1447. }else{
  1448. //chroma422
  1449. dct_linesize = uvlinesize << s->interlaced_dct;
  1450. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1451. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1452. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1453. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1454. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1455. if(!s->chroma_x_shift){//Chroma444
  1456. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1457. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1458. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1459. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1460. }
  1461. }
  1462. }//fi gray
  1463. }
  1464. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1465. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1466. }
  1467. } else {
  1468. /* dct only in intra block */
  1469. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1470. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1471. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1472. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1473. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1474. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1475. if(s->chroma_y_shift){
  1476. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1477. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1478. }else{
  1479. dct_offset >>=1;
  1480. dct_linesize >>=1;
  1481. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1482. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1483. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1484. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1485. }
  1486. }
  1487. }else{
  1488. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  1489. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1490. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  1491. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1492. if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1493. if(s->chroma_y_shift){
  1494. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  1495. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  1496. }else{
  1497. dct_linesize = uvlinesize << s->interlaced_dct;
  1498. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1499. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  1500. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  1501. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1502. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1503. if(!s->chroma_x_shift){//Chroma444
  1504. s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1505. s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1506. s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1507. s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1508. }
  1509. }
  1510. }//gray
  1511. }
  1512. }
  1513. skip_idct:
  1514. if(!readable){
  1515. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1516. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1517. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1518. }
  1519. }
  1520. }
  1521. void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
  1522. {
  1523. #if !CONFIG_SMALL
  1524. if(s->out_format == FMT_MPEG1) {
  1525. mpv_decode_mb_internal(s, block, 1);
  1526. } else
  1527. #endif
  1528. mpv_decode_mb_internal(s, block, 0);
  1529. }
  1530. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1531. {
  1532. ff_draw_horiz_band(s->avctx, s->current_picture.f,
  1533. s->last_picture.f, y, h, s->picture_structure,
  1534. s->first_field, s->low_delay);
  1535. }
  1536. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1537. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1538. const int uvlinesize = s->current_picture.f->linesize[1];
  1539. const int mb_size= 4;
  1540. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1541. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1542. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1543. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1544. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1545. 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;
  1546. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1547. s->dest[0] = s->current_picture.f->data[0] + (s->mb_x - 1) * (1 << mb_size);
  1548. s->dest[1] = s->current_picture.f->data[1] + (s->mb_x - 1) * (1 << (mb_size - s->chroma_x_shift));
  1549. s->dest[2] = s->current_picture.f->data[2] + (s->mb_x - 1) * (1 << (mb_size - s->chroma_x_shift));
  1550. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1551. {
  1552. if(s->picture_structure==PICT_FRAME){
  1553. s->dest[0] += s->mb_y * linesize << mb_size;
  1554. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1555. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1556. }else{
  1557. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  1558. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1559. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1560. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  1561. }
  1562. }
  1563. }
  1564. void ff_mpeg_flush(AVCodecContext *avctx){
  1565. int i;
  1566. MpegEncContext *s = avctx->priv_data;
  1567. if (!s || !s->picture)
  1568. return;
  1569. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  1570. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1571. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1572. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1573. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1574. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1575. s->mb_x= s->mb_y= 0;
  1576. s->parse_context.state= -1;
  1577. s->parse_context.frame_start_found= 0;
  1578. s->parse_context.overread= 0;
  1579. s->parse_context.overread_index= 0;
  1580. s->parse_context.index= 0;
  1581. s->parse_context.last_index= 0;
  1582. s->bitstream_buffer_size=0;
  1583. s->pp_time=0;
  1584. }
  1585. /**
  1586. * set qscale and update qscale dependent variables.
  1587. */
  1588. void ff_set_qscale(MpegEncContext * s, int qscale)
  1589. {
  1590. if (qscale < 1)
  1591. qscale = 1;
  1592. else if (qscale > 31)
  1593. qscale = 31;
  1594. s->qscale = qscale;
  1595. s->chroma_qscale= s->chroma_qscale_table[qscale];
  1596. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  1597. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  1598. }
  1599. void ff_mpv_report_decode_progress(MpegEncContext *s)
  1600. {
  1601. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  1602. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  1603. }