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.

1798 lines
64KB

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