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.

1921 lines
68KB

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