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.

2301 lines
78KB

  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 frame_size_alloc(MpegEncContext *s, int linesize)
  306. {
  307. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  308. // edge emu needs blocksize + filter length - 1
  309. // (= 17x17 for halfpel / 21x21 for h264)
  310. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  311. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  312. // linesize * interlaced * MBsize
  313. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
  314. fail);
  315. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
  316. fail)
  317. s->me.temp = s->me.scratchpad;
  318. s->rd_scratchpad = s->me.scratchpad;
  319. s->b_scratchpad = s->me.scratchpad;
  320. s->obmc_scratchpad = s->me.scratchpad + 16;
  321. return 0;
  322. fail:
  323. av_freep(&s->edge_emu_buffer);
  324. return AVERROR(ENOMEM);
  325. }
  326. /**
  327. * Allocate a frame buffer
  328. */
  329. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  330. {
  331. int edges_needed = av_codec_is_encoder(s->avctx->codec);
  332. int r, ret;
  333. pic->tf.f = pic->f;
  334. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  335. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  336. s->codec_id != AV_CODEC_ID_MSS2) {
  337. if (edges_needed) {
  338. pic->f->width = s->avctx->width + 2 * EDGE_WIDTH;
  339. pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
  340. }
  341. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  342. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  343. } else {
  344. pic->f->width = s->avctx->width;
  345. pic->f->height = s->avctx->height;
  346. pic->f->format = s->avctx->pix_fmt;
  347. r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
  348. }
  349. if (r < 0 || !pic->f->buf[0]) {
  350. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  351. r, pic->f->data[0]);
  352. return -1;
  353. }
  354. if (edges_needed) {
  355. int i;
  356. for (i = 0; pic->f->data[i]; i++) {
  357. int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
  358. pic->f->linesize[i] +
  359. (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
  360. pic->f->data[i] += offset;
  361. }
  362. pic->f->width = s->avctx->width;
  363. pic->f->height = s->avctx->height;
  364. }
  365. if (s->avctx->hwaccel) {
  366. assert(!pic->hwaccel_picture_private);
  367. if (s->avctx->hwaccel->frame_priv_data_size) {
  368. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->frame_priv_data_size);
  369. if (!pic->hwaccel_priv_buf) {
  370. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  371. return -1;
  372. }
  373. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  374. }
  375. }
  376. if (s->linesize && (s->linesize != pic->f->linesize[0] ||
  377. s->uvlinesize != pic->f->linesize[1])) {
  378. av_log(s->avctx, AV_LOG_ERROR,
  379. "get_buffer() failed (stride changed)\n");
  380. ff_mpeg_unref_picture(s->avctx, pic);
  381. return -1;
  382. }
  383. if (pic->f->linesize[1] != pic->f->linesize[2]) {
  384. av_log(s->avctx, AV_LOG_ERROR,
  385. "get_buffer() failed (uv stride mismatch)\n");
  386. ff_mpeg_unref_picture(s->avctx, pic);
  387. return -1;
  388. }
  389. if (!s->edge_emu_buffer &&
  390. (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
  391. av_log(s->avctx, AV_LOG_ERROR,
  392. "get_buffer() failed to allocate context scratch buffers.\n");
  393. ff_mpeg_unref_picture(s->avctx, pic);
  394. return ret;
  395. }
  396. return 0;
  397. }
  398. void ff_free_picture_tables(Picture *pic)
  399. {
  400. int i;
  401. av_buffer_unref(&pic->mb_var_buf);
  402. av_buffer_unref(&pic->mc_mb_var_buf);
  403. av_buffer_unref(&pic->mb_mean_buf);
  404. av_buffer_unref(&pic->mbskip_table_buf);
  405. av_buffer_unref(&pic->qscale_table_buf);
  406. av_buffer_unref(&pic->mb_type_buf);
  407. for (i = 0; i < 2; i++) {
  408. av_buffer_unref(&pic->motion_val_buf[i]);
  409. av_buffer_unref(&pic->ref_index_buf[i]);
  410. }
  411. }
  412. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  413. {
  414. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  415. const int mb_array_size = s->mb_stride * s->mb_height;
  416. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  417. int i;
  418. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  419. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  420. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  421. sizeof(uint32_t));
  422. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  423. return AVERROR(ENOMEM);
  424. if (s->encoding) {
  425. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  426. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  427. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  428. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  429. return AVERROR(ENOMEM);
  430. }
  431. if (s->out_format == FMT_H263 || s->encoding) {
  432. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  433. int ref_index_size = 4 * mb_array_size;
  434. for (i = 0; mv_size && i < 2; i++) {
  435. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  436. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  437. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  438. return AVERROR(ENOMEM);
  439. }
  440. }
  441. return 0;
  442. }
  443. static int make_tables_writable(Picture *pic)
  444. {
  445. int ret, i;
  446. #define MAKE_WRITABLE(table) \
  447. do {\
  448. if (pic->table &&\
  449. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  450. return ret;\
  451. } while (0)
  452. MAKE_WRITABLE(mb_var_buf);
  453. MAKE_WRITABLE(mc_mb_var_buf);
  454. MAKE_WRITABLE(mb_mean_buf);
  455. MAKE_WRITABLE(mbskip_table_buf);
  456. MAKE_WRITABLE(qscale_table_buf);
  457. MAKE_WRITABLE(mb_type_buf);
  458. for (i = 0; i < 2; i++) {
  459. MAKE_WRITABLE(motion_val_buf[i]);
  460. MAKE_WRITABLE(ref_index_buf[i]);
  461. }
  462. return 0;
  463. }
  464. /**
  465. * Allocate a Picture.
  466. * The pixels are allocated/set by calling get_buffer() if shared = 0
  467. */
  468. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  469. {
  470. int i, ret;
  471. if (shared) {
  472. assert(pic->f->data[0]);
  473. pic->shared = 1;
  474. } else {
  475. assert(!pic->f->buf[0]);
  476. if (alloc_frame_buffer(s, pic) < 0)
  477. return -1;
  478. s->linesize = pic->f->linesize[0];
  479. s->uvlinesize = pic->f->linesize[1];
  480. }
  481. if (!pic->qscale_table_buf)
  482. ret = alloc_picture_tables(s, pic);
  483. else
  484. ret = make_tables_writable(pic);
  485. if (ret < 0)
  486. goto fail;
  487. if (s->encoding) {
  488. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  489. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  490. pic->mb_mean = pic->mb_mean_buf->data;
  491. }
  492. pic->mbskip_table = pic->mbskip_table_buf->data;
  493. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  494. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  495. if (pic->motion_val_buf[0]) {
  496. for (i = 0; i < 2; i++) {
  497. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  498. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  499. }
  500. }
  501. return 0;
  502. fail:
  503. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  504. ff_mpeg_unref_picture(s->avctx, pic);
  505. ff_free_picture_tables(pic);
  506. return AVERROR(ENOMEM);
  507. }
  508. /**
  509. * Deallocate a picture.
  510. */
  511. void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
  512. {
  513. pic->tf.f = pic->f;
  514. /* WM Image / Screen codecs allocate internal buffers with different
  515. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  516. if (avctx->codec->id != AV_CODEC_ID_WMV3IMAGE &&
  517. avctx->codec->id != AV_CODEC_ID_VC1IMAGE &&
  518. avctx->codec->id != AV_CODEC_ID_MSS2)
  519. ff_thread_release_buffer(avctx, &pic->tf);
  520. else if (pic->f)
  521. av_frame_unref(pic->f);
  522. av_buffer_unref(&pic->hwaccel_priv_buf);
  523. if (pic->needs_realloc)
  524. ff_free_picture_tables(pic);
  525. }
  526. static int update_picture_tables(Picture *dst, Picture *src)
  527. {
  528. int i;
  529. #define UPDATE_TABLE(table)\
  530. do {\
  531. if (src->table &&\
  532. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  533. av_buffer_unref(&dst->table);\
  534. dst->table = av_buffer_ref(src->table);\
  535. if (!dst->table) {\
  536. ff_free_picture_tables(dst);\
  537. return AVERROR(ENOMEM);\
  538. }\
  539. }\
  540. } while (0)
  541. UPDATE_TABLE(mb_var_buf);
  542. UPDATE_TABLE(mc_mb_var_buf);
  543. UPDATE_TABLE(mb_mean_buf);
  544. UPDATE_TABLE(mbskip_table_buf);
  545. UPDATE_TABLE(qscale_table_buf);
  546. UPDATE_TABLE(mb_type_buf);
  547. for (i = 0; i < 2; i++) {
  548. UPDATE_TABLE(motion_val_buf[i]);
  549. UPDATE_TABLE(ref_index_buf[i]);
  550. }
  551. dst->mb_var = src->mb_var;
  552. dst->mc_mb_var = src->mc_mb_var;
  553. dst->mb_mean = src->mb_mean;
  554. dst->mbskip_table = src->mbskip_table;
  555. dst->qscale_table = src->qscale_table;
  556. dst->mb_type = src->mb_type;
  557. for (i = 0; i < 2; i++) {
  558. dst->motion_val[i] = src->motion_val[i];
  559. dst->ref_index[i] = src->ref_index[i];
  560. }
  561. return 0;
  562. }
  563. int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
  564. {
  565. int ret;
  566. av_assert0(!dst->f->buf[0]);
  567. av_assert0(src->f->buf[0]);
  568. src->tf.f = src->f;
  569. dst->tf.f = dst->f;
  570. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  571. if (ret < 0)
  572. goto fail;
  573. ret = update_picture_tables(dst, src);
  574. if (ret < 0)
  575. goto fail;
  576. if (src->hwaccel_picture_private) {
  577. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  578. if (!dst->hwaccel_priv_buf)
  579. goto fail;
  580. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  581. }
  582. dst->field_picture = src->field_picture;
  583. dst->mb_var_sum = src->mb_var_sum;
  584. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  585. dst->b_frame_score = src->b_frame_score;
  586. dst->needs_realloc = src->needs_realloc;
  587. dst->reference = src->reference;
  588. dst->shared = src->shared;
  589. return 0;
  590. fail:
  591. ff_mpeg_unref_picture(avctx, dst);
  592. return ret;
  593. }
  594. static int init_duplicate_context(MpegEncContext *s)
  595. {
  596. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  597. int c_size = s->mb_stride * (s->mb_height + 1);
  598. int yc_size = y_size + 2 * c_size;
  599. int i;
  600. s->edge_emu_buffer =
  601. s->me.scratchpad =
  602. s->me.temp =
  603. s->rd_scratchpad =
  604. s->b_scratchpad =
  605. s->obmc_scratchpad = NULL;
  606. if (s->encoding) {
  607. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  608. ME_MAP_SIZE * sizeof(uint32_t), fail)
  609. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  610. ME_MAP_SIZE * sizeof(uint32_t), fail)
  611. if (s->avctx->noise_reduction) {
  612. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  613. 2 * 64 * sizeof(int), fail)
  614. }
  615. }
  616. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  617. s->block = s->blocks[0];
  618. for (i = 0; i < 12; i++) {
  619. s->pblocks[i] = &s->block[i];
  620. }
  621. if (s->avctx->codec_tag == AV_RL32("VCR2")) {
  622. // exchange uv
  623. int16_t (*tmp)[64];
  624. tmp = s->pblocks[4];
  625. s->pblocks[4] = s->pblocks[5];
  626. s->pblocks[5] = tmp;
  627. }
  628. if (s->out_format == FMT_H263) {
  629. /* ac values */
  630. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  631. yc_size * sizeof(int16_t) * 16, fail);
  632. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  633. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  634. s->ac_val[2] = s->ac_val[1] + c_size;
  635. }
  636. return 0;
  637. fail:
  638. return -1; // free() through ff_mpv_common_end()
  639. }
  640. static void free_duplicate_context(MpegEncContext *s)
  641. {
  642. if (!s)
  643. return;
  644. av_freep(&s->edge_emu_buffer);
  645. av_freep(&s->me.scratchpad);
  646. s->me.temp =
  647. s->rd_scratchpad =
  648. s->b_scratchpad =
  649. s->obmc_scratchpad = NULL;
  650. av_freep(&s->dct_error_sum);
  651. av_freep(&s->me.map);
  652. av_freep(&s->me.score_map);
  653. av_freep(&s->blocks);
  654. av_freep(&s->ac_val_base);
  655. s->block = NULL;
  656. }
  657. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  658. {
  659. #define COPY(a) bak->a = src->a
  660. COPY(edge_emu_buffer);
  661. COPY(me.scratchpad);
  662. COPY(me.temp);
  663. COPY(rd_scratchpad);
  664. COPY(b_scratchpad);
  665. COPY(obmc_scratchpad);
  666. COPY(me.map);
  667. COPY(me.score_map);
  668. COPY(blocks);
  669. COPY(block);
  670. COPY(start_mb_y);
  671. COPY(end_mb_y);
  672. COPY(me.map_generation);
  673. COPY(pb);
  674. COPY(dct_error_sum);
  675. COPY(dct_count[0]);
  676. COPY(dct_count[1]);
  677. COPY(ac_val_base);
  678. COPY(ac_val[0]);
  679. COPY(ac_val[1]);
  680. COPY(ac_val[2]);
  681. #undef COPY
  682. }
  683. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  684. {
  685. MpegEncContext bak;
  686. int i, ret;
  687. // FIXME copy only needed parts
  688. // START_TIMER
  689. backup_duplicate_context(&bak, dst);
  690. memcpy(dst, src, sizeof(MpegEncContext));
  691. backup_duplicate_context(dst, &bak);
  692. for (i = 0; i < 12; i++) {
  693. dst->pblocks[i] = &dst->block[i];
  694. }
  695. if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
  696. // exchange uv
  697. int16_t (*tmp)[64];
  698. tmp = dst->pblocks[4];
  699. dst->pblocks[4] = dst->pblocks[5];
  700. dst->pblocks[5] = tmp;
  701. }
  702. if (!dst->edge_emu_buffer &&
  703. (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
  704. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  705. "scratch buffers.\n");
  706. return ret;
  707. }
  708. // STOP_TIMER("update_duplicate_context")
  709. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  710. return 0;
  711. }
  712. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  713. const AVCodecContext *src)
  714. {
  715. int i, ret;
  716. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  717. if (dst == src || !s1->context_initialized)
  718. return 0;
  719. // FIXME can parameters change on I-frames?
  720. // in that case dst may need a reinit
  721. if (!s->context_initialized) {
  722. int err;
  723. memcpy(s, s1, sizeof(MpegEncContext));
  724. s->avctx = dst;
  725. s->bitstream_buffer = NULL;
  726. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  727. ff_mpv_idct_init(s);
  728. if ((err = ff_mpv_common_init(s)) < 0)
  729. return err;
  730. }
  731. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  732. int err;
  733. s->context_reinit = 0;
  734. s->height = s1->height;
  735. s->width = s1->width;
  736. if ((err = ff_mpv_common_frame_size_change(s)) < 0)
  737. return err;
  738. }
  739. s->avctx->coded_height = s1->avctx->coded_height;
  740. s->avctx->coded_width = s1->avctx->coded_width;
  741. s->avctx->width = s1->avctx->width;
  742. s->avctx->height = s1->avctx->height;
  743. s->coded_picture_number = s1->coded_picture_number;
  744. s->picture_number = s1->picture_number;
  745. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  746. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  747. if (s1->picture[i].f->buf[0] &&
  748. (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
  749. return ret;
  750. }
  751. #define UPDATE_PICTURE(pic)\
  752. do {\
  753. ff_mpeg_unref_picture(s->avctx, &s->pic);\
  754. if (s1->pic.f->buf[0])\
  755. ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
  756. else\
  757. ret = update_picture_tables(&s->pic, &s1->pic);\
  758. if (ret < 0)\
  759. return ret;\
  760. } while (0)
  761. UPDATE_PICTURE(current_picture);
  762. UPDATE_PICTURE(last_picture);
  763. UPDATE_PICTURE(next_picture);
  764. #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
  765. ((pic && pic >= old_ctx->picture && \
  766. pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
  767. &new_ctx->picture[pic - old_ctx->picture] : NULL)
  768. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  769. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  770. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  771. // Error/bug resilience
  772. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  773. s->workaround_bugs = s1->workaround_bugs;
  774. // MPEG4 timing info
  775. memcpy(&s->last_time_base, &s1->last_time_base,
  776. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  777. (char *) &s1->last_time_base);
  778. // B-frame info
  779. s->max_b_frames = s1->max_b_frames;
  780. s->low_delay = s1->low_delay;
  781. s->droppable = s1->droppable;
  782. // DivX handling (doesn't work)
  783. s->divx_packed = s1->divx_packed;
  784. if (s1->bitstream_buffer) {
  785. if (s1->bitstream_buffer_size +
  786. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  787. av_fast_malloc(&s->bitstream_buffer,
  788. &s->allocated_bitstream_buffer_size,
  789. s1->allocated_bitstream_buffer_size);
  790. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  791. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  792. s1->bitstream_buffer_size);
  793. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  794. FF_INPUT_BUFFER_PADDING_SIZE);
  795. }
  796. // linesize dependend scratch buffer allocation
  797. if (!s->edge_emu_buffer)
  798. if (s1->linesize) {
  799. if (frame_size_alloc(s, s1->linesize) < 0) {
  800. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  801. "scratch buffers.\n");
  802. return AVERROR(ENOMEM);
  803. }
  804. } else {
  805. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  806. "be allocated due to unknown size.\n");
  807. return AVERROR_BUG;
  808. }
  809. // MPEG2/interlacing info
  810. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  811. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  812. if (!s1->first_field) {
  813. s->last_pict_type = s1->pict_type;
  814. if (s1->current_picture_ptr)
  815. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
  816. }
  817. return 0;
  818. }
  819. /**
  820. * Set the given MpegEncContext to common defaults
  821. * (same for encoding and decoding).
  822. * The changed fields will not depend upon the
  823. * prior state of the MpegEncContext.
  824. */
  825. void ff_mpv_common_defaults(MpegEncContext *s)
  826. {
  827. s->y_dc_scale_table =
  828. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  829. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  830. s->progressive_frame = 1;
  831. s->progressive_sequence = 1;
  832. s->picture_structure = PICT_FRAME;
  833. s->coded_picture_number = 0;
  834. s->picture_number = 0;
  835. s->f_code = 1;
  836. s->b_code = 1;
  837. s->slice_context_count = 1;
  838. }
  839. /**
  840. * Set the given MpegEncContext to defaults for decoding.
  841. * the changed fields will not depend upon
  842. * the prior state of the MpegEncContext.
  843. */
  844. void ff_mpv_decode_defaults(MpegEncContext *s)
  845. {
  846. ff_mpv_common_defaults(s);
  847. }
  848. static int init_er(MpegEncContext *s)
  849. {
  850. ERContext *er = &s->er;
  851. int mb_array_size = s->mb_height * s->mb_stride;
  852. int i;
  853. er->avctx = s->avctx;
  854. er->mb_index2xy = s->mb_index2xy;
  855. er->mb_num = s->mb_num;
  856. er->mb_width = s->mb_width;
  857. er->mb_height = s->mb_height;
  858. er->mb_stride = s->mb_stride;
  859. er->b8_stride = s->b8_stride;
  860. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  861. er->error_status_table = av_mallocz(mb_array_size);
  862. if (!er->er_temp_buffer || !er->error_status_table)
  863. goto fail;
  864. er->mbskip_table = s->mbskip_table;
  865. er->mbintra_table = s->mbintra_table;
  866. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  867. er->dc_val[i] = s->dc_val[i];
  868. er->decode_mb = mpeg_er_decode_mb;
  869. er->opaque = s;
  870. return 0;
  871. fail:
  872. av_freep(&er->er_temp_buffer);
  873. av_freep(&er->error_status_table);
  874. return AVERROR(ENOMEM);
  875. }
  876. /**
  877. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  878. */
  879. static int init_context_frame(MpegEncContext *s)
  880. {
  881. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  882. s->mb_width = (s->width + 15) / 16;
  883. s->mb_stride = s->mb_width + 1;
  884. s->b8_stride = s->mb_width * 2 + 1;
  885. mb_array_size = s->mb_height * s->mb_stride;
  886. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  887. /* set default edge pos, will be overriden
  888. * in decode_header if needed */
  889. s->h_edge_pos = s->mb_width * 16;
  890. s->v_edge_pos = s->mb_height * 16;
  891. s->mb_num = s->mb_width * s->mb_height;
  892. s->block_wrap[0] =
  893. s->block_wrap[1] =
  894. s->block_wrap[2] =
  895. s->block_wrap[3] = s->b8_stride;
  896. s->block_wrap[4] =
  897. s->block_wrap[5] = s->mb_stride;
  898. y_size = s->b8_stride * (2 * s->mb_height + 1);
  899. c_size = s->mb_stride * (s->mb_height + 1);
  900. yc_size = y_size + 2 * c_size;
  901. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  902. fail); // error ressilience code looks cleaner with this
  903. for (y = 0; y < s->mb_height; y++)
  904. for (x = 0; x < s->mb_width; x++)
  905. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  906. s->mb_index2xy[s->mb_height * s->mb_width] =
  907. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  908. if (s->encoding) {
  909. /* Allocate MV tables */
  910. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  911. mv_table_size * 2 * sizeof(int16_t), fail);
  912. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  913. mv_table_size * 2 * sizeof(int16_t), fail);
  914. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  915. mv_table_size * 2 * sizeof(int16_t), fail);
  916. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  917. mv_table_size * 2 * sizeof(int16_t), fail);
  918. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  919. mv_table_size * 2 * sizeof(int16_t), fail);
  920. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  921. mv_table_size * 2 * sizeof(int16_t), fail);
  922. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  923. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  924. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  925. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  926. s->mb_stride + 1;
  927. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  928. s->mb_stride + 1;
  929. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  930. /* Allocate MB type table */
  931. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  932. sizeof(uint16_t), fail); // needed for encoding
  933. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  934. sizeof(int), fail);
  935. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  936. mb_array_size * sizeof(float), fail);
  937. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  938. mb_array_size * sizeof(float), fail);
  939. }
  940. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  941. (s->avctx->flags & CODEC_FLAG_INTERLACED_ME)) {
  942. /* interlaced direct mode decoding tables */
  943. for (i = 0; i < 2; i++) {
  944. int j, k;
  945. for (j = 0; j < 2; j++) {
  946. for (k = 0; k < 2; k++) {
  947. FF_ALLOCZ_OR_GOTO(s->avctx,
  948. s->b_field_mv_table_base[i][j][k],
  949. mv_table_size * 2 * sizeof(int16_t),
  950. fail);
  951. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  952. s->mb_stride + 1;
  953. }
  954. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  955. mb_array_size * 2 * sizeof(uint8_t), fail);
  956. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  957. mv_table_size * 2 * sizeof(int16_t), fail);
  958. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  959. + s->mb_stride + 1;
  960. }
  961. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  962. mb_array_size * 2 * sizeof(uint8_t), fail);
  963. }
  964. }
  965. if (s->out_format == FMT_H263) {
  966. /* cbp values */
  967. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  968. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  969. /* cbp, ac_pred, pred_dir */
  970. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  971. mb_array_size * sizeof(uint8_t), fail);
  972. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  973. mb_array_size * sizeof(uint8_t), fail);
  974. }
  975. if (s->h263_pred || s->h263_plus || !s->encoding) {
  976. /* dc values */
  977. // MN: we need these for error resilience of intra-frames
  978. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  979. yc_size * sizeof(int16_t), fail);
  980. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  981. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  982. s->dc_val[2] = s->dc_val[1] + c_size;
  983. for (i = 0; i < yc_size; i++)
  984. s->dc_val_base[i] = 1024;
  985. }
  986. /* which mb is a intra block */
  987. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  988. memset(s->mbintra_table, 1, mb_array_size);
  989. /* init macroblock skip table */
  990. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  991. // Note the + 1 is for a quicker mpeg4 slice_end detection
  992. return init_er(s);
  993. fail:
  994. return AVERROR(ENOMEM);
  995. }
  996. /**
  997. * init common structure for both encoder and decoder.
  998. * this assumes that some variables like width/height are already set
  999. */
  1000. av_cold int ff_mpv_common_init(MpegEncContext *s)
  1001. {
  1002. int i;
  1003. int nb_slices = (HAVE_THREADS &&
  1004. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  1005. s->avctx->thread_count : 1;
  1006. if (s->encoding && s->avctx->slices)
  1007. nb_slices = s->avctx->slices;
  1008. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1009. s->mb_height = (s->height + 31) / 32 * 2;
  1010. else
  1011. s->mb_height = (s->height + 15) / 16;
  1012. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  1013. av_log(s->avctx, AV_LOG_ERROR,
  1014. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  1015. return -1;
  1016. }
  1017. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  1018. int max_slices;
  1019. if (s->mb_height)
  1020. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  1021. else
  1022. max_slices = MAX_THREADS;
  1023. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  1024. " reducing to %d\n", nb_slices, max_slices);
  1025. nb_slices = max_slices;
  1026. }
  1027. if ((s->width || s->height) &&
  1028. av_image_check_size(s->width, s->height, 0, s->avctx))
  1029. return -1;
  1030. dct_init(s);
  1031. /* set chroma shifts */
  1032. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1033. &s->chroma_x_shift,
  1034. &s->chroma_y_shift);
  1035. /* convert fourcc to upper case */
  1036. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  1037. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  1038. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  1039. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1040. s->picture[i].f = av_frame_alloc();
  1041. if (!s->picture[i].f)
  1042. goto fail;
  1043. }
  1044. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1045. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1046. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1047. memset(&s->new_picture, 0, sizeof(s->new_picture));
  1048. s->next_picture.f = av_frame_alloc();
  1049. if (!s->next_picture.f)
  1050. goto fail;
  1051. s->last_picture.f = av_frame_alloc();
  1052. if (!s->last_picture.f)
  1053. goto fail;
  1054. s->current_picture.f = av_frame_alloc();
  1055. if (!s->current_picture.f)
  1056. goto fail;
  1057. s->new_picture.f = av_frame_alloc();
  1058. if (!s->new_picture.f)
  1059. goto fail;
  1060. if (s->width && s->height) {
  1061. if (init_context_frame(s))
  1062. goto fail;
  1063. s->parse_context.state = -1;
  1064. }
  1065. s->context_initialized = 1;
  1066. s->thread_context[0] = s;
  1067. if (s->width && s->height) {
  1068. if (nb_slices > 1) {
  1069. for (i = 1; i < nb_slices; i++) {
  1070. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1071. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1072. }
  1073. for (i = 0; i < nb_slices; i++) {
  1074. if (init_duplicate_context(s->thread_context[i]) < 0)
  1075. goto fail;
  1076. s->thread_context[i]->start_mb_y =
  1077. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1078. s->thread_context[i]->end_mb_y =
  1079. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1080. }
  1081. } else {
  1082. if (init_duplicate_context(s) < 0)
  1083. goto fail;
  1084. s->start_mb_y = 0;
  1085. s->end_mb_y = s->mb_height;
  1086. }
  1087. s->slice_context_count = nb_slices;
  1088. }
  1089. return 0;
  1090. fail:
  1091. ff_mpv_common_end(s);
  1092. return -1;
  1093. }
  1094. /**
  1095. * Frees and resets MpegEncContext fields depending on the resolution.
  1096. * Is used during resolution changes to avoid a full reinitialization of the
  1097. * codec.
  1098. */
  1099. static void free_context_frame(MpegEncContext *s)
  1100. {
  1101. int i, j, k;
  1102. av_freep(&s->mb_type);
  1103. av_freep(&s->p_mv_table_base);
  1104. av_freep(&s->b_forw_mv_table_base);
  1105. av_freep(&s->b_back_mv_table_base);
  1106. av_freep(&s->b_bidir_forw_mv_table_base);
  1107. av_freep(&s->b_bidir_back_mv_table_base);
  1108. av_freep(&s->b_direct_mv_table_base);
  1109. s->p_mv_table = NULL;
  1110. s->b_forw_mv_table = NULL;
  1111. s->b_back_mv_table = NULL;
  1112. s->b_bidir_forw_mv_table = NULL;
  1113. s->b_bidir_back_mv_table = NULL;
  1114. s->b_direct_mv_table = NULL;
  1115. for (i = 0; i < 2; i++) {
  1116. for (j = 0; j < 2; j++) {
  1117. for (k = 0; k < 2; k++) {
  1118. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1119. s->b_field_mv_table[i][j][k] = NULL;
  1120. }
  1121. av_freep(&s->b_field_select_table[i][j]);
  1122. av_freep(&s->p_field_mv_table_base[i][j]);
  1123. s->p_field_mv_table[i][j] = NULL;
  1124. }
  1125. av_freep(&s->p_field_select_table[i]);
  1126. }
  1127. av_freep(&s->dc_val_base);
  1128. av_freep(&s->coded_block_base);
  1129. av_freep(&s->mbintra_table);
  1130. av_freep(&s->cbp_table);
  1131. av_freep(&s->pred_dir_table);
  1132. av_freep(&s->mbskip_table);
  1133. av_freep(&s->er.error_status_table);
  1134. av_freep(&s->er.er_temp_buffer);
  1135. av_freep(&s->mb_index2xy);
  1136. av_freep(&s->lambda_table);
  1137. av_freep(&s->cplx_tab);
  1138. av_freep(&s->bits_tab);
  1139. s->linesize = s->uvlinesize = 0;
  1140. }
  1141. int ff_mpv_common_frame_size_change(MpegEncContext *s)
  1142. {
  1143. int i, err = 0;
  1144. if (s->slice_context_count > 1) {
  1145. for (i = 0; i < s->slice_context_count; i++) {
  1146. free_duplicate_context(s->thread_context[i]);
  1147. }
  1148. for (i = 1; i < s->slice_context_count; i++) {
  1149. av_freep(&s->thread_context[i]);
  1150. }
  1151. } else
  1152. free_duplicate_context(s);
  1153. free_context_frame(s);
  1154. if (s->picture)
  1155. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1156. s->picture[i].needs_realloc = 1;
  1157. }
  1158. s->last_picture_ptr =
  1159. s->next_picture_ptr =
  1160. s->current_picture_ptr = NULL;
  1161. // init
  1162. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1163. s->mb_height = (s->height + 31) / 32 * 2;
  1164. else
  1165. s->mb_height = (s->height + 15) / 16;
  1166. if ((s->width || s->height) &&
  1167. (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
  1168. goto fail;
  1169. if ((err = init_context_frame(s)))
  1170. goto fail;
  1171. s->thread_context[0] = s;
  1172. if (s->width && s->height) {
  1173. int nb_slices = s->slice_context_count;
  1174. if (nb_slices > 1) {
  1175. for (i = 1; i < nb_slices; i++) {
  1176. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1177. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1178. }
  1179. for (i = 0; i < nb_slices; i++) {
  1180. if ((err = init_duplicate_context(s->thread_context[i])) < 0)
  1181. goto fail;
  1182. s->thread_context[i]->start_mb_y =
  1183. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1184. s->thread_context[i]->end_mb_y =
  1185. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1186. }
  1187. } else {
  1188. if (init_duplicate_context(s) < 0)
  1189. goto fail;
  1190. s->start_mb_y = 0;
  1191. s->end_mb_y = s->mb_height;
  1192. }
  1193. s->slice_context_count = nb_slices;
  1194. }
  1195. return 0;
  1196. fail:
  1197. ff_mpv_common_end(s);
  1198. return err;
  1199. }
  1200. /* init common structure for both encoder and decoder */
  1201. void ff_mpv_common_end(MpegEncContext *s)
  1202. {
  1203. int i;
  1204. if (s->slice_context_count > 1) {
  1205. for (i = 0; i < s->slice_context_count; i++) {
  1206. free_duplicate_context(s->thread_context[i]);
  1207. }
  1208. for (i = 1; i < s->slice_context_count; i++) {
  1209. av_freep(&s->thread_context[i]);
  1210. }
  1211. s->slice_context_count = 1;
  1212. } else free_duplicate_context(s);
  1213. av_freep(&s->parse_context.buffer);
  1214. s->parse_context.buffer_size = 0;
  1215. av_freep(&s->bitstream_buffer);
  1216. s->allocated_bitstream_buffer_size = 0;
  1217. if (s->picture) {
  1218. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1219. ff_free_picture_tables(&s->picture[i]);
  1220. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1221. av_frame_free(&s->picture[i].f);
  1222. }
  1223. }
  1224. av_freep(&s->picture);
  1225. ff_free_picture_tables(&s->last_picture);
  1226. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1227. av_frame_free(&s->last_picture.f);
  1228. ff_free_picture_tables(&s->current_picture);
  1229. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1230. av_frame_free(&s->current_picture.f);
  1231. ff_free_picture_tables(&s->next_picture);
  1232. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1233. av_frame_free(&s->next_picture.f);
  1234. ff_free_picture_tables(&s->new_picture);
  1235. ff_mpeg_unref_picture(s->avctx, &s->new_picture);
  1236. av_frame_free(&s->new_picture.f);
  1237. free_context_frame(s);
  1238. s->context_initialized = 0;
  1239. s->last_picture_ptr =
  1240. s->next_picture_ptr =
  1241. s->current_picture_ptr = NULL;
  1242. s->linesize = s->uvlinesize = 0;
  1243. }
  1244. static void release_unused_pictures(AVCodecContext *avctx, Picture *picture)
  1245. {
  1246. int i;
  1247. /* release non reference frames */
  1248. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1249. if (!picture[i].reference)
  1250. ff_mpeg_unref_picture(avctx, &picture[i]);
  1251. }
  1252. }
  1253. static inline int pic_is_unused(Picture *pic)
  1254. {
  1255. if (!pic->f->buf[0])
  1256. return 1;
  1257. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1258. return 1;
  1259. return 0;
  1260. }
  1261. static int find_unused_picture(Picture *picture, int shared)
  1262. {
  1263. int i;
  1264. if (shared) {
  1265. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1266. if (!picture[i].f->buf[0])
  1267. return i;
  1268. }
  1269. } else {
  1270. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1271. if (pic_is_unused(&picture[i]))
  1272. return i;
  1273. }
  1274. }
  1275. return AVERROR_INVALIDDATA;
  1276. }
  1277. int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  1278. {
  1279. int ret = find_unused_picture(picture, shared);
  1280. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1281. if (picture[ret].needs_realloc) {
  1282. picture[ret].needs_realloc = 0;
  1283. ff_free_picture_tables(&picture[ret]);
  1284. ff_mpeg_unref_picture(avctx, &picture[ret]);
  1285. }
  1286. }
  1287. return ret;
  1288. }
  1289. /**
  1290. * generic function called after decoding
  1291. * the header and before a frame is decoded.
  1292. */
  1293. int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1294. {
  1295. int i, ret;
  1296. Picture *pic;
  1297. s->mb_skipped = 0;
  1298. /* mark & release old frames */
  1299. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1300. s->last_picture_ptr != s->next_picture_ptr &&
  1301. s->last_picture_ptr->f->buf[0]) {
  1302. ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
  1303. }
  1304. /* release forgotten pictures */
  1305. /* if (mpeg124/h263) */
  1306. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1307. if (&s->picture[i] != s->last_picture_ptr &&
  1308. &s->picture[i] != s->next_picture_ptr &&
  1309. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1310. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1311. av_log(avctx, AV_LOG_ERROR,
  1312. "releasing zombie picture\n");
  1313. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1314. }
  1315. }
  1316. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1317. release_unused_pictures(s->avctx, s->picture);
  1318. if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
  1319. // we already have a unused image
  1320. // (maybe it was set before reading the header)
  1321. pic = s->current_picture_ptr;
  1322. } else {
  1323. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1324. if (i < 0) {
  1325. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1326. return i;
  1327. }
  1328. pic = &s->picture[i];
  1329. }
  1330. pic->reference = 0;
  1331. if (!s->droppable) {
  1332. if (s->pict_type != AV_PICTURE_TYPE_B)
  1333. pic->reference = 3;
  1334. }
  1335. pic->f->coded_picture_number = s->coded_picture_number++;
  1336. if (ff_alloc_picture(s, pic, 0) < 0)
  1337. return -1;
  1338. s->current_picture_ptr = pic;
  1339. // FIXME use only the vars from current_pic
  1340. s->current_picture_ptr->f->top_field_first = s->top_field_first;
  1341. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1342. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1343. if (s->picture_structure != PICT_FRAME)
  1344. s->current_picture_ptr->f->top_field_first =
  1345. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1346. }
  1347. s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
  1348. !s->progressive_sequence;
  1349. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1350. s->current_picture_ptr->f->pict_type = s->pict_type;
  1351. // if (s->avctx->flags && CODEC_FLAG_QSCALE)
  1352. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1353. s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1354. if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
  1355. s->current_picture_ptr)) < 0)
  1356. return ret;
  1357. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1358. s->last_picture_ptr = s->next_picture_ptr;
  1359. if (!s->droppable)
  1360. s->next_picture_ptr = s->current_picture_ptr;
  1361. }
  1362. ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1363. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1364. s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
  1365. s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
  1366. s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
  1367. s->pict_type, s->droppable);
  1368. if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
  1369. (s->pict_type != AV_PICTURE_TYPE_I ||
  1370. s->picture_structure != PICT_FRAME)) {
  1371. int h_chroma_shift, v_chroma_shift;
  1372. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1373. &h_chroma_shift, &v_chroma_shift);
  1374. if (s->pict_type != AV_PICTURE_TYPE_I)
  1375. av_log(avctx, AV_LOG_ERROR,
  1376. "warning: first frame is no keyframe\n");
  1377. else if (s->picture_structure != PICT_FRAME)
  1378. av_log(avctx, AV_LOG_INFO,
  1379. "allocate dummy last picture for field based first keyframe\n");
  1380. /* Allocate a dummy frame */
  1381. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1382. if (i < 0) {
  1383. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1384. return i;
  1385. }
  1386. s->last_picture_ptr = &s->picture[i];
  1387. s->last_picture_ptr->reference = 3;
  1388. s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1389. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1390. s->last_picture_ptr = NULL;
  1391. return -1;
  1392. }
  1393. memset(s->last_picture_ptr->f->data[0], 0,
  1394. avctx->height * s->last_picture_ptr->f->linesize[0]);
  1395. memset(s->last_picture_ptr->f->data[1], 0x80,
  1396. (avctx->height >> v_chroma_shift) *
  1397. s->last_picture_ptr->f->linesize[1]);
  1398. memset(s->last_picture_ptr->f->data[2], 0x80,
  1399. (avctx->height >> v_chroma_shift) *
  1400. s->last_picture_ptr->f->linesize[2]);
  1401. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1402. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1403. }
  1404. if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
  1405. s->pict_type == AV_PICTURE_TYPE_B) {
  1406. /* Allocate a dummy frame */
  1407. i = ff_find_unused_picture(s->avctx, s->picture, 0);
  1408. if (i < 0) {
  1409. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1410. return i;
  1411. }
  1412. s->next_picture_ptr = &s->picture[i];
  1413. s->next_picture_ptr->reference = 3;
  1414. s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
  1415. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1416. s->next_picture_ptr = NULL;
  1417. return -1;
  1418. }
  1419. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1420. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1421. }
  1422. if (s->last_picture_ptr) {
  1423. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1424. if (s->last_picture_ptr->f->buf[0] &&
  1425. (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
  1426. s->last_picture_ptr)) < 0)
  1427. return ret;
  1428. }
  1429. if (s->next_picture_ptr) {
  1430. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1431. if (s->next_picture_ptr->f->buf[0] &&
  1432. (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
  1433. s->next_picture_ptr)) < 0)
  1434. return ret;
  1435. }
  1436. if (s->pict_type != AV_PICTURE_TYPE_I &&
  1437. !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
  1438. av_log(s, AV_LOG_ERROR,
  1439. "Non-reference picture received and no reference available\n");
  1440. return AVERROR_INVALIDDATA;
  1441. }
  1442. if (s->picture_structure!= PICT_FRAME) {
  1443. int i;
  1444. for (i = 0; i < 4; i++) {
  1445. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1446. s->current_picture.f->data[i] +=
  1447. s->current_picture.f->linesize[i];
  1448. }
  1449. s->current_picture.f->linesize[i] *= 2;
  1450. s->last_picture.f->linesize[i] *= 2;
  1451. s->next_picture.f->linesize[i] *= 2;
  1452. }
  1453. }
  1454. /* set dequantizer, we can't do it during init as
  1455. * it might change for mpeg4 and we can't do it in the header
  1456. * decode as init is not called for mpeg4 there yet */
  1457. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1458. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1459. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1460. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1461. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1462. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1463. } else {
  1464. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1465. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1466. }
  1467. #if FF_API_XVMC
  1468. FF_DISABLE_DEPRECATION_WARNINGS
  1469. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1470. return ff_xvmc_field_start(s, avctx);
  1471. FF_ENABLE_DEPRECATION_WARNINGS
  1472. #endif /* FF_API_XVMC */
  1473. return 0;
  1474. }
  1475. /* called after a frame has been decoded. */
  1476. void ff_mpv_frame_end(MpegEncContext *s)
  1477. {
  1478. #if FF_API_XVMC
  1479. FF_DISABLE_DEPRECATION_WARNINGS
  1480. /* redraw edges for the frame if decoding didn't complete */
  1481. // just to make sure that all data is rendered.
  1482. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1483. ff_xvmc_field_end(s);
  1484. } else
  1485. FF_ENABLE_DEPRECATION_WARNINGS
  1486. #endif /* FF_API_XVMC */
  1487. emms_c();
  1488. if (s->current_picture.reference)
  1489. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1490. }
  1491. /**
  1492. * Print debugging info for the given picture.
  1493. */
  1494. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1495. {
  1496. AVFrame *pict;
  1497. if (s->avctx->hwaccel || !p || !p->mb_type)
  1498. return;
  1499. pict = p->f;
  1500. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1501. int x,y;
  1502. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1503. switch (pict->pict_type) {
  1504. case AV_PICTURE_TYPE_I:
  1505. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1506. break;
  1507. case AV_PICTURE_TYPE_P:
  1508. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1509. break;
  1510. case AV_PICTURE_TYPE_B:
  1511. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1512. break;
  1513. case AV_PICTURE_TYPE_S:
  1514. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1515. break;
  1516. case AV_PICTURE_TYPE_SI:
  1517. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1518. break;
  1519. case AV_PICTURE_TYPE_SP:
  1520. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1521. break;
  1522. }
  1523. for (y = 0; y < s->mb_height; y++) {
  1524. for (x = 0; x < s->mb_width; x++) {
  1525. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1526. int count = s->mbskip_table[x + y * s->mb_stride];
  1527. if (count > 9)
  1528. count = 9;
  1529. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1530. }
  1531. if (s->avctx->debug & FF_DEBUG_QP) {
  1532. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1533. p->qscale_table[x + y * s->mb_stride]);
  1534. }
  1535. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1536. int mb_type = p->mb_type[x + y * s->mb_stride];
  1537. // Type & MV direction
  1538. if (IS_PCM(mb_type))
  1539. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1540. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1541. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1542. else if (IS_INTRA4x4(mb_type))
  1543. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1544. else if (IS_INTRA16x16(mb_type))
  1545. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1546. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1547. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1548. else if (IS_DIRECT(mb_type))
  1549. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1550. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1551. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1552. else if (IS_GMC(mb_type))
  1553. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1554. else if (IS_SKIP(mb_type))
  1555. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1556. else if (!USES_LIST(mb_type, 1))
  1557. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1558. else if (!USES_LIST(mb_type, 0))
  1559. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1560. else {
  1561. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1562. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1563. }
  1564. // segmentation
  1565. if (IS_8X8(mb_type))
  1566. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1567. else if (IS_16X8(mb_type))
  1568. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1569. else if (IS_8X16(mb_type))
  1570. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1571. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1572. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1573. else
  1574. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1575. if (IS_INTERLACED(mb_type))
  1576. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1577. else
  1578. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1579. }
  1580. }
  1581. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1582. }
  1583. }
  1584. }
  1585. /**
  1586. * find the lowest MB row referenced in the MVs
  1587. */
  1588. int ff_mpv_lowest_referenced_row(MpegEncContext *s, int dir)
  1589. {
  1590. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1591. int my, off, i, mvs;
  1592. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1593. goto unhandled;
  1594. switch (s->mv_type) {
  1595. case MV_TYPE_16X16:
  1596. mvs = 1;
  1597. break;
  1598. case MV_TYPE_16X8:
  1599. mvs = 2;
  1600. break;
  1601. case MV_TYPE_8X8:
  1602. mvs = 4;
  1603. break;
  1604. default:
  1605. goto unhandled;
  1606. }
  1607. for (i = 0; i < mvs; i++) {
  1608. my = s->mv[dir][i][1]<<qpel_shift;
  1609. my_max = FFMAX(my_max, my);
  1610. my_min = FFMIN(my_min, my);
  1611. }
  1612. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1613. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1614. unhandled:
  1615. return s->mb_height-1;
  1616. }
  1617. /* put block[] to dest[] */
  1618. static inline void put_dct(MpegEncContext *s,
  1619. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1620. {
  1621. s->dct_unquantize_intra(s, block, i, qscale);
  1622. s->idsp.idct_put(dest, line_size, block);
  1623. }
  1624. /* add block[] to dest[] */
  1625. static inline void add_dct(MpegEncContext *s,
  1626. int16_t *block, int i, uint8_t *dest, int line_size)
  1627. {
  1628. if (s->block_last_index[i] >= 0) {
  1629. s->idsp.idct_add(dest, line_size, block);
  1630. }
  1631. }
  1632. static inline void add_dequant_dct(MpegEncContext *s,
  1633. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1634. {
  1635. if (s->block_last_index[i] >= 0) {
  1636. s->dct_unquantize_inter(s, block, i, qscale);
  1637. s->idsp.idct_add(dest, line_size, block);
  1638. }
  1639. }
  1640. /**
  1641. * Clean dc, ac, coded_block for the current non-intra MB.
  1642. */
  1643. void ff_clean_intra_table_entries(MpegEncContext *s)
  1644. {
  1645. int wrap = s->b8_stride;
  1646. int xy = s->block_index[0];
  1647. s->dc_val[0][xy ] =
  1648. s->dc_val[0][xy + 1 ] =
  1649. s->dc_val[0][xy + wrap] =
  1650. s->dc_val[0][xy + 1 + wrap] = 1024;
  1651. /* ac pred */
  1652. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1653. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1654. if (s->msmpeg4_version>=3) {
  1655. s->coded_block[xy ] =
  1656. s->coded_block[xy + 1 ] =
  1657. s->coded_block[xy + wrap] =
  1658. s->coded_block[xy + 1 + wrap] = 0;
  1659. }
  1660. /* chroma */
  1661. wrap = s->mb_stride;
  1662. xy = s->mb_x + s->mb_y * wrap;
  1663. s->dc_val[1][xy] =
  1664. s->dc_val[2][xy] = 1024;
  1665. /* ac pred */
  1666. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1667. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1668. s->mbintra_table[xy]= 0;
  1669. }
  1670. /* generic function called after a macroblock has been parsed by the
  1671. decoder or after it has been encoded by the encoder.
  1672. Important variables used:
  1673. s->mb_intra : true if intra macroblock
  1674. s->mv_dir : motion vector direction
  1675. s->mv_type : motion vector type
  1676. s->mv : motion vector
  1677. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1678. */
  1679. static av_always_inline
  1680. void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1681. int is_mpeg12)
  1682. {
  1683. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1684. #if FF_API_XVMC
  1685. FF_DISABLE_DEPRECATION_WARNINGS
  1686. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1687. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1688. return;
  1689. }
  1690. FF_ENABLE_DEPRECATION_WARNINGS
  1691. #endif /* FF_API_XVMC */
  1692. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1693. /* print DCT coefficients */
  1694. int i,j;
  1695. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1696. for(i=0; i<6; i++){
  1697. for(j=0; j<64; j++){
  1698. av_log(s->avctx, AV_LOG_DEBUG, "%5d",
  1699. block[i][s->idsp.idct_permutation[j]]);
  1700. }
  1701. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1702. }
  1703. }
  1704. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1705. /* update DC predictors for P macroblocks */
  1706. if (!s->mb_intra) {
  1707. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1708. if(s->mbintra_table[mb_xy])
  1709. ff_clean_intra_table_entries(s);
  1710. } else {
  1711. s->last_dc[0] =
  1712. s->last_dc[1] =
  1713. s->last_dc[2] = 128 << s->intra_dc_precision;
  1714. }
  1715. }
  1716. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1717. s->mbintra_table[mb_xy]=1;
  1718. if ((s->avctx->flags & CODEC_FLAG_PSNR) ||
  1719. !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
  1720. s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
  1721. uint8_t *dest_y, *dest_cb, *dest_cr;
  1722. int dct_linesize, dct_offset;
  1723. op_pixels_func (*op_pix)[4];
  1724. qpel_mc_func (*op_qpix)[16];
  1725. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1726. const int uvlinesize = s->current_picture.f->linesize[1];
  1727. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1728. const int block_size = 8;
  1729. /* avoid copy if macroblock skipped in last frame too */
  1730. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1731. if(!s->encoding){
  1732. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1733. if (s->mb_skipped) {
  1734. s->mb_skipped= 0;
  1735. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1736. *mbskip_ptr = 1;
  1737. } else if(!s->current_picture.reference) {
  1738. *mbskip_ptr = 1;
  1739. } else{
  1740. *mbskip_ptr = 0; /* not skipped */
  1741. }
  1742. }
  1743. dct_linesize = linesize << s->interlaced_dct;
  1744. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1745. if(readable){
  1746. dest_y= s->dest[0];
  1747. dest_cb= s->dest[1];
  1748. dest_cr= s->dest[2];
  1749. }else{
  1750. dest_y = s->b_scratchpad;
  1751. dest_cb= s->b_scratchpad+16*linesize;
  1752. dest_cr= s->b_scratchpad+32*linesize;
  1753. }
  1754. if (!s->mb_intra) {
  1755. /* motion handling */
  1756. /* decoding or more than one mb_type (MC was already done otherwise) */
  1757. if(!s->encoding){
  1758. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1759. if (s->mv_dir & MV_DIR_FORWARD) {
  1760. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1761. ff_mpv_lowest_referenced_row(s, 0),
  1762. 0);
  1763. }
  1764. if (s->mv_dir & MV_DIR_BACKWARD) {
  1765. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1766. ff_mpv_lowest_referenced_row(s, 1),
  1767. 0);
  1768. }
  1769. }
  1770. op_qpix= s->me.qpel_put;
  1771. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1772. op_pix = s->hdsp.put_pixels_tab;
  1773. }else{
  1774. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1775. }
  1776. if (s->mv_dir & MV_DIR_FORWARD) {
  1777. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
  1778. op_pix = s->hdsp.avg_pixels_tab;
  1779. op_qpix= s->me.qpel_avg;
  1780. }
  1781. if (s->mv_dir & MV_DIR_BACKWARD) {
  1782. ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
  1783. }
  1784. }
  1785. /* skip dequant / idct if we are really late ;) */
  1786. if(s->avctx->skip_idct){
  1787. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1788. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1789. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1790. goto skip_idct;
  1791. }
  1792. /* add dct residue */
  1793. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1794. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1795. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1796. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1797. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1798. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1799. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  1800. if (s->chroma_y_shift){
  1801. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1802. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1803. }else{
  1804. dct_linesize >>= 1;
  1805. dct_offset >>=1;
  1806. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1807. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1808. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1809. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1810. }
  1811. }
  1812. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1813. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1814. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1815. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1816. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1817. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  1818. if(s->chroma_y_shift){//Chroma420
  1819. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1820. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1821. }else{
  1822. //chroma422
  1823. dct_linesize = uvlinesize << s->interlaced_dct;
  1824. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1825. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1826. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1827. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1828. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1829. if(!s->chroma_x_shift){//Chroma444
  1830. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1831. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1832. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1833. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1834. }
  1835. }
  1836. }//fi gray
  1837. }
  1838. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1839. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1840. }
  1841. } else {
  1842. /* dct only in intra block */
  1843. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1844. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1845. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1846. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1847. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1848. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  1849. if(s->chroma_y_shift){
  1850. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1851. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1852. }else{
  1853. dct_offset >>=1;
  1854. dct_linesize >>=1;
  1855. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1856. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1857. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1858. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1859. }
  1860. }
  1861. }else{
  1862. s->idsp.idct_put(dest_y, dct_linesize, block[0]);
  1863. s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1864. s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
  1865. s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1866. if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
  1867. if(s->chroma_y_shift){
  1868. s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
  1869. s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
  1870. }else{
  1871. dct_linesize = uvlinesize << s->interlaced_dct;
  1872. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1873. s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
  1874. s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
  1875. s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1876. s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1877. if(!s->chroma_x_shift){//Chroma444
  1878. s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1879. s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1880. s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1881. s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1882. }
  1883. }
  1884. }//gray
  1885. }
  1886. }
  1887. skip_idct:
  1888. if(!readable){
  1889. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1890. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1891. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1892. }
  1893. }
  1894. }
  1895. void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
  1896. {
  1897. #if !CONFIG_SMALL
  1898. if(s->out_format == FMT_MPEG1) {
  1899. mpv_decode_mb_internal(s, block, 1);
  1900. } else
  1901. #endif
  1902. mpv_decode_mb_internal(s, block, 0);
  1903. }
  1904. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1905. {
  1906. ff_draw_horiz_band(s->avctx, s->current_picture.f,
  1907. s->last_picture.f, y, h, s->picture_structure,
  1908. s->first_field, s->low_delay);
  1909. }
  1910. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1911. const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
  1912. const int uvlinesize = s->current_picture.f->linesize[1];
  1913. const int mb_size= 4;
  1914. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1915. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1916. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1917. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1918. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1919. 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;
  1920. //block_index is not used by mpeg2, so it is not affected by chroma_format
  1921. s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
  1922. s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1923. s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  1924. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  1925. {
  1926. if(s->picture_structure==PICT_FRAME){
  1927. s->dest[0] += s->mb_y * linesize << mb_size;
  1928. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1929. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  1930. }else{
  1931. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  1932. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1933. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  1934. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  1935. }
  1936. }
  1937. }
  1938. /**
  1939. * Permute an 8x8 block.
  1940. * @param block the block which will be permuted according to the given permutation vector
  1941. * @param permutation the permutation vector
  1942. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  1943. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  1944. * (inverse) permutated to scantable order!
  1945. */
  1946. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  1947. {
  1948. int i;
  1949. int16_t temp[64];
  1950. if(last<=0) return;
  1951. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  1952. for(i=0; i<=last; i++){
  1953. const int j= scantable[i];
  1954. temp[j]= block[j];
  1955. block[j]=0;
  1956. }
  1957. for(i=0; i<=last; i++){
  1958. const int j= scantable[i];
  1959. const int perm_j= permutation[j];
  1960. block[perm_j]= temp[j];
  1961. }
  1962. }
  1963. void ff_mpeg_flush(AVCodecContext *avctx){
  1964. int i;
  1965. MpegEncContext *s = avctx->priv_data;
  1966. if (!s || !s->picture)
  1967. return;
  1968. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  1969. ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
  1970. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  1971. ff_mpeg_unref_picture(s->avctx, &s->current_picture);
  1972. ff_mpeg_unref_picture(s->avctx, &s->last_picture);
  1973. ff_mpeg_unref_picture(s->avctx, &s->next_picture);
  1974. s->mb_x= s->mb_y= 0;
  1975. s->parse_context.state= -1;
  1976. s->parse_context.frame_start_found= 0;
  1977. s->parse_context.overread= 0;
  1978. s->parse_context.overread_index= 0;
  1979. s->parse_context.index= 0;
  1980. s->parse_context.last_index= 0;
  1981. s->bitstream_buffer_size=0;
  1982. s->pp_time=0;
  1983. }
  1984. /**
  1985. * set qscale and update qscale dependent variables.
  1986. */
  1987. void ff_set_qscale(MpegEncContext * s, int qscale)
  1988. {
  1989. if (qscale < 1)
  1990. qscale = 1;
  1991. else if (qscale > 31)
  1992. qscale = 31;
  1993. s->qscale = qscale;
  1994. s->chroma_qscale= s->chroma_qscale_table[qscale];
  1995. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  1996. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  1997. }
  1998. void ff_mpv_report_decode_progress(MpegEncContext *s)
  1999. {
  2000. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2001. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2002. }