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.

3247 lines
117KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "h264chroma.h"
  35. #include "internal.h"
  36. #include "mathops.h"
  37. #include "mpegvideo.h"
  38. #include "mjpegenc.h"
  39. #include "msmpeg4.h"
  40. #include "thread.h"
  41. #include <limits.h>
  42. static const uint8_t ff_default_chroma_qscale_table[32] = {
  43. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  44. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  45. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  46. };
  47. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  48. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  49. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  50. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  51. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  52. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  53. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  54. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  55. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  56. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  57. };
  58. static const uint8_t mpeg2_dc_scale_table1[128] = {
  59. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  60. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  61. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  62. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  63. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  64. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  67. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  68. };
  69. static const uint8_t mpeg2_dc_scale_table2[128] = {
  70. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  71. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  72. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  73. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  74. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  76. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  77. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  78. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  79. };
  80. static const uint8_t mpeg2_dc_scale_table3[128] = {
  81. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  82. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  83. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  84. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  85. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  86. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. };
  91. const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
  92. ff_mpeg1_dc_scale_table,
  93. mpeg2_dc_scale_table1,
  94. mpeg2_dc_scale_table2,
  95. mpeg2_dc_scale_table3,
  96. };
  97. const enum AVPixelFormat ff_pixfmt_list_420[] = {
  98. AV_PIX_FMT_YUV420P,
  99. AV_PIX_FMT_NONE
  100. };
  101. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  102. int16_t *block, int n, int qscale)
  103. {
  104. int i, level, nCoeffs;
  105. const uint16_t *quant_matrix;
  106. nCoeffs= s->block_last_index[n];
  107. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  108. /* XXX: only mpeg1 */
  109. quant_matrix = s->intra_matrix;
  110. for(i=1;i<=nCoeffs;i++) {
  111. int j= s->intra_scantable.permutated[i];
  112. level = block[j];
  113. if (level) {
  114. if (level < 0) {
  115. level = -level;
  116. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  117. level = (level - 1) | 1;
  118. level = -level;
  119. } else {
  120. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  121. level = (level - 1) | 1;
  122. }
  123. block[j] = level;
  124. }
  125. }
  126. }
  127. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  128. int16_t *block, int n, int qscale)
  129. {
  130. int i, level, nCoeffs;
  131. const uint16_t *quant_matrix;
  132. nCoeffs= s->block_last_index[n];
  133. quant_matrix = s->inter_matrix;
  134. for(i=0; i<=nCoeffs; i++) {
  135. int j= s->intra_scantable.permutated[i];
  136. level = block[j];
  137. if (level) {
  138. if (level < 0) {
  139. level = -level;
  140. level = (((level << 1) + 1) * qscale *
  141. ((int) (quant_matrix[j]))) >> 4;
  142. level = (level - 1) | 1;
  143. level = -level;
  144. } else {
  145. level = (((level << 1) + 1) * qscale *
  146. ((int) (quant_matrix[j]))) >> 4;
  147. level = (level - 1) | 1;
  148. }
  149. block[j] = level;
  150. }
  151. }
  152. }
  153. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  154. int16_t *block, int n, int qscale)
  155. {
  156. int i, level, nCoeffs;
  157. const uint16_t *quant_matrix;
  158. if(s->alternate_scan) nCoeffs= 63;
  159. else nCoeffs= s->block_last_index[n];
  160. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  161. quant_matrix = s->intra_matrix;
  162. for(i=1;i<=nCoeffs;i++) {
  163. int j= s->intra_scantable.permutated[i];
  164. level = block[j];
  165. if (level) {
  166. if (level < 0) {
  167. level = -level;
  168. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  169. level = -level;
  170. } else {
  171. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  172. }
  173. block[j] = level;
  174. }
  175. }
  176. }
  177. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  178. int16_t *block, int n, int qscale)
  179. {
  180. int i, level, nCoeffs;
  181. const uint16_t *quant_matrix;
  182. int sum=-1;
  183. if(s->alternate_scan) nCoeffs= 63;
  184. else nCoeffs= s->block_last_index[n];
  185. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  186. sum += block[0];
  187. quant_matrix = s->intra_matrix;
  188. for(i=1;i<=nCoeffs;i++) {
  189. int j= s->intra_scantable.permutated[i];
  190. level = block[j];
  191. if (level) {
  192. if (level < 0) {
  193. level = -level;
  194. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  195. level = -level;
  196. } else {
  197. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  198. }
  199. block[j] = level;
  200. sum+=level;
  201. }
  202. }
  203. block[63]^=sum&1;
  204. }
  205. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  206. int16_t *block, int n, int qscale)
  207. {
  208. int i, level, nCoeffs;
  209. const uint16_t *quant_matrix;
  210. int sum=-1;
  211. if(s->alternate_scan) nCoeffs= 63;
  212. else nCoeffs= s->block_last_index[n];
  213. quant_matrix = s->inter_matrix;
  214. for(i=0; i<=nCoeffs; i++) {
  215. int j= s->intra_scantable.permutated[i];
  216. level = block[j];
  217. if (level) {
  218. if (level < 0) {
  219. level = -level;
  220. level = (((level << 1) + 1) * qscale *
  221. ((int) (quant_matrix[j]))) >> 4;
  222. level = -level;
  223. } else {
  224. level = (((level << 1) + 1) * qscale *
  225. ((int) (quant_matrix[j]))) >> 4;
  226. }
  227. block[j] = level;
  228. sum+=level;
  229. }
  230. }
  231. block[63]^=sum&1;
  232. }
  233. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  234. int16_t *block, int n, int qscale)
  235. {
  236. int i, level, qmul, qadd;
  237. int nCoeffs;
  238. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  239. qmul = qscale << 1;
  240. if (!s->h263_aic) {
  241. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  242. qadd = (qscale - 1) | 1;
  243. }else{
  244. qadd = 0;
  245. }
  246. if(s->ac_pred)
  247. nCoeffs=63;
  248. else
  249. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  250. for(i=1; i<=nCoeffs; i++) {
  251. level = block[i];
  252. if (level) {
  253. if (level < 0) {
  254. level = level * qmul - qadd;
  255. } else {
  256. level = level * qmul + qadd;
  257. }
  258. block[i] = level;
  259. }
  260. }
  261. }
  262. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  263. int16_t *block, int n, int qscale)
  264. {
  265. int i, level, qmul, qadd;
  266. int nCoeffs;
  267. av_assert2(s->block_last_index[n]>=0);
  268. qadd = (qscale - 1) | 1;
  269. qmul = qscale << 1;
  270. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  271. for(i=0; i<=nCoeffs; i++) {
  272. level = block[i];
  273. if (level) {
  274. if (level < 0) {
  275. level = level * qmul - qadd;
  276. } else {
  277. level = level * qmul + qadd;
  278. }
  279. block[i] = level;
  280. }
  281. }
  282. }
  283. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  284. int (*mv)[2][4][2],
  285. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  286. {
  287. MpegEncContext *s = opaque;
  288. s->mv_dir = mv_dir;
  289. s->mv_type = mv_type;
  290. s->mb_intra = mb_intra;
  291. s->mb_skipped = mb_skipped;
  292. s->mb_x = mb_x;
  293. s->mb_y = mb_y;
  294. memcpy(s->mv, mv, sizeof(*mv));
  295. ff_init_block_index(s);
  296. ff_update_block_index(s);
  297. s->dsp.clear_blocks(s->block[0]);
  298. s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  299. 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);
  300. 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);
  301. if (ref)
  302. av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
  303. ff_MPV_decode_mb(s, s->block);
  304. }
  305. /* init common dct for both encoder and decoder */
  306. av_cold int ff_dct_common_init(MpegEncContext *s)
  307. {
  308. ff_dsputil_init(&s->dsp, s->avctx);
  309. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  310. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  311. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  312. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  313. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  314. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  315. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  316. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  317. if (s->flags & CODEC_FLAG_BITEXACT)
  318. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  319. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  320. if (ARCH_ALPHA)
  321. ff_MPV_common_init_axp(s);
  322. if (ARCH_ARM)
  323. ff_MPV_common_init_arm(s);
  324. if (ARCH_BFIN)
  325. ff_MPV_common_init_bfin(s);
  326. if (ARCH_PPC)
  327. ff_MPV_common_init_ppc(s);
  328. if (ARCH_X86)
  329. ff_MPV_common_init_x86(s);
  330. /* load & permutate scantables
  331. * note: only wmv uses different ones
  332. */
  333. if (s->alternate_scan) {
  334. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  335. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  336. } else {
  337. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  338. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  339. }
  340. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  341. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  342. return 0;
  343. }
  344. static int frame_size_alloc(MpegEncContext *s, int linesize)
  345. {
  346. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  347. // edge emu needs blocksize + filter length - 1
  348. // (= 17x17 for halfpel / 21x21 for h264)
  349. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  350. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  351. // linesize * interlaced * MBsize
  352. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
  353. fail);
  354. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
  355. fail)
  356. s->me.temp = s->me.scratchpad;
  357. s->rd_scratchpad = s->me.scratchpad;
  358. s->b_scratchpad = s->me.scratchpad;
  359. s->obmc_scratchpad = s->me.scratchpad + 16;
  360. return 0;
  361. fail:
  362. av_freep(&s->edge_emu_buffer);
  363. return AVERROR(ENOMEM);
  364. }
  365. /**
  366. * Allocate a frame buffer
  367. */
  368. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  369. {
  370. int r, ret;
  371. pic->tf.f = &pic->f;
  372. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  373. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  374. s->codec_id != AV_CODEC_ID_MSS2)
  375. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  376. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  377. else {
  378. pic->f.width = s->avctx->width;
  379. pic->f.height = s->avctx->height;
  380. pic->f.format = s->avctx->pix_fmt;
  381. r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
  382. }
  383. if (r < 0 || !pic->f.buf[0]) {
  384. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  385. r, pic->f.data[0]);
  386. return -1;
  387. }
  388. if (s->avctx->hwaccel) {
  389. assert(!pic->hwaccel_picture_private);
  390. if (s->avctx->hwaccel->priv_data_size) {
  391. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
  392. if (!pic->hwaccel_priv_buf) {
  393. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  394. return -1;
  395. }
  396. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  397. }
  398. }
  399. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  400. s->uvlinesize != pic->f.linesize[1])) {
  401. av_log(s->avctx, AV_LOG_ERROR,
  402. "get_buffer() failed (stride changed)\n");
  403. ff_mpeg_unref_picture(s, pic);
  404. return -1;
  405. }
  406. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  407. av_log(s->avctx, AV_LOG_ERROR,
  408. "get_buffer() failed (uv stride mismatch)\n");
  409. ff_mpeg_unref_picture(s, pic);
  410. return -1;
  411. }
  412. if (!s->edge_emu_buffer &&
  413. (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  414. av_log(s->avctx, AV_LOG_ERROR,
  415. "get_buffer() failed to allocate context scratch buffers.\n");
  416. ff_mpeg_unref_picture(s, pic);
  417. return ret;
  418. }
  419. return 0;
  420. }
  421. void ff_free_picture_tables(Picture *pic)
  422. {
  423. int i;
  424. pic->alloc_mb_width =
  425. pic->alloc_mb_height = 0;
  426. av_buffer_unref(&pic->mb_var_buf);
  427. av_buffer_unref(&pic->mc_mb_var_buf);
  428. av_buffer_unref(&pic->mb_mean_buf);
  429. av_buffer_unref(&pic->mbskip_table_buf);
  430. av_buffer_unref(&pic->qscale_table_buf);
  431. av_buffer_unref(&pic->mb_type_buf);
  432. for (i = 0; i < 2; i++) {
  433. av_buffer_unref(&pic->motion_val_buf[i]);
  434. av_buffer_unref(&pic->ref_index_buf[i]);
  435. }
  436. }
  437. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  438. {
  439. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  440. const int mb_array_size = s->mb_stride * s->mb_height;
  441. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  442. int i;
  443. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  444. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  445. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  446. sizeof(uint32_t));
  447. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  448. return AVERROR(ENOMEM);
  449. if (s->encoding) {
  450. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  451. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  452. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  453. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  454. return AVERROR(ENOMEM);
  455. }
  456. if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
  457. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  458. int ref_index_size = 4 * mb_array_size;
  459. for (i = 0; mv_size && i < 2; i++) {
  460. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  461. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  462. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  463. return AVERROR(ENOMEM);
  464. }
  465. }
  466. pic->alloc_mb_width = s->mb_width;
  467. pic->alloc_mb_height = s->mb_height;
  468. return 0;
  469. }
  470. static int make_tables_writable(Picture *pic)
  471. {
  472. int ret, i;
  473. #define MAKE_WRITABLE(table) \
  474. do {\
  475. if (pic->table &&\
  476. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  477. return ret;\
  478. } while (0)
  479. MAKE_WRITABLE(mb_var_buf);
  480. MAKE_WRITABLE(mc_mb_var_buf);
  481. MAKE_WRITABLE(mb_mean_buf);
  482. MAKE_WRITABLE(mbskip_table_buf);
  483. MAKE_WRITABLE(qscale_table_buf);
  484. MAKE_WRITABLE(mb_type_buf);
  485. for (i = 0; i < 2; i++) {
  486. MAKE_WRITABLE(motion_val_buf[i]);
  487. MAKE_WRITABLE(ref_index_buf[i]);
  488. }
  489. return 0;
  490. }
  491. /**
  492. * Allocate a Picture.
  493. * The pixels are allocated/set by calling get_buffer() if shared = 0
  494. */
  495. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  496. {
  497. int i, ret;
  498. if (pic->qscale_table_buf)
  499. if ( pic->alloc_mb_width != s->mb_width
  500. || pic->alloc_mb_height != s->mb_height)
  501. ff_free_picture_tables(pic);
  502. if (shared) {
  503. av_assert0(pic->f.data[0]);
  504. pic->shared = 1;
  505. } else {
  506. av_assert0(!pic->f.buf[0]);
  507. if (alloc_frame_buffer(s, pic) < 0)
  508. return -1;
  509. s->linesize = pic->f.linesize[0];
  510. s->uvlinesize = pic->f.linesize[1];
  511. }
  512. if (!pic->qscale_table_buf)
  513. ret = alloc_picture_tables(s, pic);
  514. else
  515. ret = make_tables_writable(pic);
  516. if (ret < 0)
  517. goto fail;
  518. if (s->encoding) {
  519. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  520. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  521. pic->mb_mean = pic->mb_mean_buf->data;
  522. }
  523. pic->mbskip_table = pic->mbskip_table_buf->data;
  524. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  525. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  526. if (pic->motion_val_buf[0]) {
  527. for (i = 0; i < 2; i++) {
  528. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  529. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  530. }
  531. }
  532. return 0;
  533. fail:
  534. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  535. ff_mpeg_unref_picture(s, pic);
  536. ff_free_picture_tables(pic);
  537. return AVERROR(ENOMEM);
  538. }
  539. /**
  540. * Deallocate a picture.
  541. */
  542. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  543. {
  544. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  545. pic->tf.f = &pic->f;
  546. /* WM Image / Screen codecs allocate internal buffers with different
  547. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  548. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  549. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  550. s->codec_id != AV_CODEC_ID_MSS2)
  551. ff_thread_release_buffer(s->avctx, &pic->tf);
  552. else
  553. av_frame_unref(&pic->f);
  554. av_buffer_unref(&pic->hwaccel_priv_buf);
  555. if (pic->needs_realloc)
  556. ff_free_picture_tables(pic);
  557. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  558. }
  559. static int update_picture_tables(Picture *dst, Picture *src)
  560. {
  561. int i;
  562. #define UPDATE_TABLE(table)\
  563. do {\
  564. if (src->table &&\
  565. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  566. av_buffer_unref(&dst->table);\
  567. dst->table = av_buffer_ref(src->table);\
  568. if (!dst->table) {\
  569. ff_free_picture_tables(dst);\
  570. return AVERROR(ENOMEM);\
  571. }\
  572. }\
  573. } while (0)
  574. UPDATE_TABLE(mb_var_buf);
  575. UPDATE_TABLE(mc_mb_var_buf);
  576. UPDATE_TABLE(mb_mean_buf);
  577. UPDATE_TABLE(mbskip_table_buf);
  578. UPDATE_TABLE(qscale_table_buf);
  579. UPDATE_TABLE(mb_type_buf);
  580. for (i = 0; i < 2; i++) {
  581. UPDATE_TABLE(motion_val_buf[i]);
  582. UPDATE_TABLE(ref_index_buf[i]);
  583. }
  584. dst->mb_var = src->mb_var;
  585. dst->mc_mb_var = src->mc_mb_var;
  586. dst->mb_mean = src->mb_mean;
  587. dst->mbskip_table = src->mbskip_table;
  588. dst->qscale_table = src->qscale_table;
  589. dst->mb_type = src->mb_type;
  590. for (i = 0; i < 2; i++) {
  591. dst->motion_val[i] = src->motion_val[i];
  592. dst->ref_index[i] = src->ref_index[i];
  593. }
  594. dst->alloc_mb_width = src->alloc_mb_width;
  595. dst->alloc_mb_height = src->alloc_mb_height;
  596. return 0;
  597. }
  598. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  599. {
  600. int ret;
  601. av_assert0(!dst->f.buf[0]);
  602. av_assert0(src->f.buf[0]);
  603. src->tf.f = &src->f;
  604. dst->tf.f = &dst->f;
  605. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  606. if (ret < 0)
  607. goto fail;
  608. ret = update_picture_tables(dst, src);
  609. if (ret < 0)
  610. goto fail;
  611. if (src->hwaccel_picture_private) {
  612. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  613. if (!dst->hwaccel_priv_buf)
  614. goto fail;
  615. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  616. }
  617. dst->field_picture = src->field_picture;
  618. dst->mb_var_sum = src->mb_var_sum;
  619. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  620. dst->b_frame_score = src->b_frame_score;
  621. dst->needs_realloc = src->needs_realloc;
  622. dst->reference = src->reference;
  623. dst->shared = src->shared;
  624. return 0;
  625. fail:
  626. ff_mpeg_unref_picture(s, dst);
  627. return ret;
  628. }
  629. static void exchange_uv(MpegEncContext *s)
  630. {
  631. int16_t (*tmp)[64];
  632. tmp = s->pblocks[4];
  633. s->pblocks[4] = s->pblocks[5];
  634. s->pblocks[5] = tmp;
  635. }
  636. static int init_duplicate_context(MpegEncContext *s)
  637. {
  638. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  639. int c_size = s->mb_stride * (s->mb_height + 1);
  640. int yc_size = y_size + 2 * c_size;
  641. int i;
  642. if (s->mb_height & 1)
  643. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  644. s->edge_emu_buffer =
  645. s->me.scratchpad =
  646. s->me.temp =
  647. s->rd_scratchpad =
  648. s->b_scratchpad =
  649. s->obmc_scratchpad = NULL;
  650. if (s->encoding) {
  651. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  652. ME_MAP_SIZE * sizeof(uint32_t), fail)
  653. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  654. ME_MAP_SIZE * sizeof(uint32_t), fail)
  655. if (s->avctx->noise_reduction) {
  656. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  657. 2 * 64 * sizeof(int), fail)
  658. }
  659. }
  660. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  661. s->block = s->blocks[0];
  662. for (i = 0; i < 12; i++) {
  663. s->pblocks[i] = &s->block[i];
  664. }
  665. if (s->avctx->codec_tag == AV_RL32("VCR2"))
  666. exchange_uv(s);
  667. if (s->out_format == FMT_H263) {
  668. /* ac values */
  669. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  670. yc_size * sizeof(int16_t) * 16, fail);
  671. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  672. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  673. s->ac_val[2] = s->ac_val[1] + c_size;
  674. }
  675. return 0;
  676. fail:
  677. return -1; // free() through ff_MPV_common_end()
  678. }
  679. static void free_duplicate_context(MpegEncContext *s)
  680. {
  681. if (s == NULL)
  682. return;
  683. av_freep(&s->edge_emu_buffer);
  684. av_freep(&s->me.scratchpad);
  685. s->me.temp =
  686. s->rd_scratchpad =
  687. s->b_scratchpad =
  688. s->obmc_scratchpad = NULL;
  689. av_freep(&s->dct_error_sum);
  690. av_freep(&s->me.map);
  691. av_freep(&s->me.score_map);
  692. av_freep(&s->blocks);
  693. av_freep(&s->ac_val_base);
  694. s->block = NULL;
  695. }
  696. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  697. {
  698. #define COPY(a) bak->a = src->a
  699. COPY(edge_emu_buffer);
  700. COPY(me.scratchpad);
  701. COPY(me.temp);
  702. COPY(rd_scratchpad);
  703. COPY(b_scratchpad);
  704. COPY(obmc_scratchpad);
  705. COPY(me.map);
  706. COPY(me.score_map);
  707. COPY(blocks);
  708. COPY(block);
  709. COPY(start_mb_y);
  710. COPY(end_mb_y);
  711. COPY(me.map_generation);
  712. COPY(pb);
  713. COPY(dct_error_sum);
  714. COPY(dct_count[0]);
  715. COPY(dct_count[1]);
  716. COPY(ac_val_base);
  717. COPY(ac_val[0]);
  718. COPY(ac_val[1]);
  719. COPY(ac_val[2]);
  720. #undef COPY
  721. }
  722. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  723. {
  724. MpegEncContext bak;
  725. int i, ret;
  726. // FIXME copy only needed parts
  727. // START_TIMER
  728. backup_duplicate_context(&bak, dst);
  729. memcpy(dst, src, sizeof(MpegEncContext));
  730. backup_duplicate_context(dst, &bak);
  731. for (i = 0; i < 12; i++) {
  732. dst->pblocks[i] = &dst->block[i];
  733. }
  734. if (dst->avctx->codec_tag == AV_RL32("VCR2"))
  735. exchange_uv(dst);
  736. if (!dst->edge_emu_buffer &&
  737. (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
  738. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  739. "scratch buffers.\n");
  740. return ret;
  741. }
  742. // STOP_TIMER("update_duplicate_context")
  743. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  744. return 0;
  745. }
  746. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  747. const AVCodecContext *src)
  748. {
  749. int i, ret;
  750. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  751. if (dst == src)
  752. return 0;
  753. av_assert0(s != s1);
  754. // FIXME can parameters change on I-frames?
  755. // in that case dst may need a reinit
  756. if (!s->context_initialized) {
  757. memcpy(s, s1, sizeof(MpegEncContext));
  758. s->avctx = dst;
  759. s->bitstream_buffer = NULL;
  760. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  761. if (s1->context_initialized){
  762. // s->picture_range_start += MAX_PICTURE_COUNT;
  763. // s->picture_range_end += MAX_PICTURE_COUNT;
  764. if((ret = ff_MPV_common_init(s)) < 0){
  765. memset(s, 0, sizeof(MpegEncContext));
  766. s->avctx = dst;
  767. return ret;
  768. }
  769. }
  770. }
  771. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  772. s->context_reinit = 0;
  773. s->height = s1->height;
  774. s->width = s1->width;
  775. if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
  776. return ret;
  777. }
  778. s->avctx->coded_height = s1->avctx->coded_height;
  779. s->avctx->coded_width = s1->avctx->coded_width;
  780. s->avctx->width = s1->avctx->width;
  781. s->avctx->height = s1->avctx->height;
  782. s->coded_picture_number = s1->coded_picture_number;
  783. s->picture_number = s1->picture_number;
  784. av_assert0(!s->picture || s->picture != s1->picture);
  785. if(s->picture)
  786. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  787. ff_mpeg_unref_picture(s, &s->picture[i]);
  788. if (s1->picture[i].f.buf[0] &&
  789. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  790. return ret;
  791. }
  792. #define UPDATE_PICTURE(pic)\
  793. do {\
  794. ff_mpeg_unref_picture(s, &s->pic);\
  795. if (s1->pic.f.buf[0])\
  796. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  797. else\
  798. ret = update_picture_tables(&s->pic, &s1->pic);\
  799. if (ret < 0)\
  800. return ret;\
  801. } while (0)
  802. UPDATE_PICTURE(current_picture);
  803. UPDATE_PICTURE(last_picture);
  804. UPDATE_PICTURE(next_picture);
  805. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  806. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  807. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  808. // Error/bug resilience
  809. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  810. s->workaround_bugs = s1->workaround_bugs;
  811. s->padding_bug_score = s1->padding_bug_score;
  812. // MPEG4 timing info
  813. memcpy(&s->last_time_base, &s1->last_time_base,
  814. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  815. (char *) &s1->last_time_base);
  816. // B-frame info
  817. s->max_b_frames = s1->max_b_frames;
  818. s->low_delay = s1->low_delay;
  819. s->droppable = s1->droppable;
  820. // DivX handling (doesn't work)
  821. s->divx_packed = s1->divx_packed;
  822. if (s1->bitstream_buffer) {
  823. if (s1->bitstream_buffer_size +
  824. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  825. av_fast_malloc(&s->bitstream_buffer,
  826. &s->allocated_bitstream_buffer_size,
  827. s1->allocated_bitstream_buffer_size);
  828. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  829. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  830. s1->bitstream_buffer_size);
  831. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  832. FF_INPUT_BUFFER_PADDING_SIZE);
  833. }
  834. // linesize dependend scratch buffer allocation
  835. if (!s->edge_emu_buffer)
  836. if (s1->linesize) {
  837. if (frame_size_alloc(s, s1->linesize) < 0) {
  838. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  839. "scratch buffers.\n");
  840. return AVERROR(ENOMEM);
  841. }
  842. } else {
  843. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  844. "be allocated due to unknown size.\n");
  845. }
  846. // MPEG2/interlacing info
  847. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  848. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  849. if (!s1->first_field) {
  850. s->last_pict_type = s1->pict_type;
  851. if (s1->current_picture_ptr)
  852. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  853. }
  854. return 0;
  855. }
  856. /**
  857. * Set the given MpegEncContext to common defaults
  858. * (same for encoding and decoding).
  859. * The changed fields will not depend upon the
  860. * prior state of the MpegEncContext.
  861. */
  862. void ff_MPV_common_defaults(MpegEncContext *s)
  863. {
  864. s->y_dc_scale_table =
  865. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  866. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  867. s->progressive_frame = 1;
  868. s->progressive_sequence = 1;
  869. s->picture_structure = PICT_FRAME;
  870. s->coded_picture_number = 0;
  871. s->picture_number = 0;
  872. s->f_code = 1;
  873. s->b_code = 1;
  874. s->slice_context_count = 1;
  875. }
  876. /**
  877. * Set the given MpegEncContext to defaults for decoding.
  878. * the changed fields will not depend upon
  879. * the prior state of the MpegEncContext.
  880. */
  881. void ff_MPV_decode_defaults(MpegEncContext *s)
  882. {
  883. ff_MPV_common_defaults(s);
  884. }
  885. static int init_er(MpegEncContext *s)
  886. {
  887. ERContext *er = &s->er;
  888. int mb_array_size = s->mb_height * s->mb_stride;
  889. int i;
  890. er->avctx = s->avctx;
  891. er->dsp = &s->dsp;
  892. er->mb_index2xy = s->mb_index2xy;
  893. er->mb_num = s->mb_num;
  894. er->mb_width = s->mb_width;
  895. er->mb_height = s->mb_height;
  896. er->mb_stride = s->mb_stride;
  897. er->b8_stride = s->b8_stride;
  898. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  899. er->error_status_table = av_mallocz(mb_array_size);
  900. if (!er->er_temp_buffer || !er->error_status_table)
  901. goto fail;
  902. er->mbskip_table = s->mbskip_table;
  903. er->mbintra_table = s->mbintra_table;
  904. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  905. er->dc_val[i] = s->dc_val[i];
  906. er->decode_mb = mpeg_er_decode_mb;
  907. er->opaque = s;
  908. return 0;
  909. fail:
  910. av_freep(&er->er_temp_buffer);
  911. av_freep(&er->error_status_table);
  912. return AVERROR(ENOMEM);
  913. }
  914. /**
  915. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  916. */
  917. static int init_context_frame(MpegEncContext *s)
  918. {
  919. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  920. s->mb_width = (s->width + 15) / 16;
  921. s->mb_stride = s->mb_width + 1;
  922. s->b8_stride = s->mb_width * 2 + 1;
  923. s->b4_stride = s->mb_width * 4 + 1;
  924. mb_array_size = s->mb_height * s->mb_stride;
  925. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  926. /* set default edge pos, will be overriden
  927. * in decode_header if needed */
  928. s->h_edge_pos = s->mb_width * 16;
  929. s->v_edge_pos = s->mb_height * 16;
  930. s->mb_num = s->mb_width * s->mb_height;
  931. s->block_wrap[0] =
  932. s->block_wrap[1] =
  933. s->block_wrap[2] =
  934. s->block_wrap[3] = s->b8_stride;
  935. s->block_wrap[4] =
  936. s->block_wrap[5] = s->mb_stride;
  937. y_size = s->b8_stride * (2 * s->mb_height + 1);
  938. c_size = s->mb_stride * (s->mb_height + 1);
  939. yc_size = y_size + 2 * c_size;
  940. if (s->mb_height & 1)
  941. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  942. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
  943. for (y = 0; y < s->mb_height; y++)
  944. for (x = 0; x < s->mb_width; x++)
  945. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  946. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  947. if (s->encoding) {
  948. /* Allocate MV tables */
  949. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  950. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  951. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  952. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  953. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  954. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  955. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  956. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  957. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  958. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  959. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  960. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  961. /* Allocate MB type table */
  962. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  963. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  964. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  965. mb_array_size * sizeof(float), fail);
  966. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  967. mb_array_size * sizeof(float), fail);
  968. }
  969. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  970. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  971. /* interlaced direct mode decoding tables */
  972. for (i = 0; i < 2; i++) {
  973. int j, k;
  974. for (j = 0; j < 2; j++) {
  975. for (k = 0; k < 2; k++) {
  976. FF_ALLOCZ_OR_GOTO(s->avctx,
  977. s->b_field_mv_table_base[i][j][k],
  978. mv_table_size * 2 * sizeof(int16_t),
  979. fail);
  980. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  981. s->mb_stride + 1;
  982. }
  983. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  984. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  985. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  986. }
  987. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  988. }
  989. }
  990. if (s->out_format == FMT_H263) {
  991. /* cbp values */
  992. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  993. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  994. /* cbp, ac_pred, pred_dir */
  995. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  996. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  997. }
  998. if (s->h263_pred || s->h263_plus || !s->encoding) {
  999. /* dc values */
  1000. // MN: we need these for error resilience of intra-frames
  1001. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  1002. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  1003. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  1004. s->dc_val[2] = s->dc_val[1] + c_size;
  1005. for (i = 0; i < yc_size; i++)
  1006. s->dc_val_base[i] = 1024;
  1007. }
  1008. /* which mb is a intra block */
  1009. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  1010. memset(s->mbintra_table, 1, mb_array_size);
  1011. /* init macroblock skip table */
  1012. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  1013. // Note the + 1 is for a quicker mpeg4 slice_end detection
  1014. return init_er(s);
  1015. fail:
  1016. return AVERROR(ENOMEM);
  1017. }
  1018. /**
  1019. * init common structure for both encoder and decoder.
  1020. * this assumes that some variables like width/height are already set
  1021. */
  1022. av_cold int ff_MPV_common_init(MpegEncContext *s)
  1023. {
  1024. int i;
  1025. int nb_slices = (HAVE_THREADS &&
  1026. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  1027. s->avctx->thread_count : 1;
  1028. if (s->encoding && s->avctx->slices)
  1029. nb_slices = s->avctx->slices;
  1030. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1031. s->mb_height = (s->height + 31) / 32 * 2;
  1032. else
  1033. s->mb_height = (s->height + 15) / 16;
  1034. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  1035. av_log(s->avctx, AV_LOG_ERROR,
  1036. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  1037. return -1;
  1038. }
  1039. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  1040. int max_slices;
  1041. if (s->mb_height)
  1042. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  1043. else
  1044. max_slices = MAX_THREADS;
  1045. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  1046. " reducing to %d\n", nb_slices, max_slices);
  1047. nb_slices = max_slices;
  1048. }
  1049. if ((s->width || s->height) &&
  1050. av_image_check_size(s->width, s->height, 0, s->avctx))
  1051. return -1;
  1052. ff_dct_common_init(s);
  1053. s->flags = s->avctx->flags;
  1054. s->flags2 = s->avctx->flags2;
  1055. /* set chroma shifts */
  1056. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  1057. &s->chroma_x_shift,
  1058. &s->chroma_y_shift);
  1059. /* convert fourcc to upper case */
  1060. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  1061. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  1062. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  1063. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  1064. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1065. av_frame_unref(&s->picture[i].f);
  1066. }
  1067. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1068. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1069. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1070. av_frame_unref(&s->next_picture.f);
  1071. av_frame_unref(&s->last_picture.f);
  1072. av_frame_unref(&s->current_picture.f);
  1073. if (init_context_frame(s))
  1074. goto fail;
  1075. s->parse_context.state = -1;
  1076. s->context_initialized = 1;
  1077. s->thread_context[0] = s;
  1078. // if (s->width && s->height) {
  1079. if (nb_slices > 1) {
  1080. for (i = 1; i < nb_slices; i++) {
  1081. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1082. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1083. }
  1084. for (i = 0; i < nb_slices; i++) {
  1085. if (init_duplicate_context(s->thread_context[i]) < 0)
  1086. goto fail;
  1087. s->thread_context[i]->start_mb_y =
  1088. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1089. s->thread_context[i]->end_mb_y =
  1090. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1091. }
  1092. } else {
  1093. if (init_duplicate_context(s) < 0)
  1094. goto fail;
  1095. s->start_mb_y = 0;
  1096. s->end_mb_y = s->mb_height;
  1097. }
  1098. s->slice_context_count = nb_slices;
  1099. // }
  1100. return 0;
  1101. fail:
  1102. ff_MPV_common_end(s);
  1103. return -1;
  1104. }
  1105. /**
  1106. * Frees and resets MpegEncContext fields depending on the resolution.
  1107. * Is used during resolution changes to avoid a full reinitialization of the
  1108. * codec.
  1109. */
  1110. static int free_context_frame(MpegEncContext *s)
  1111. {
  1112. int i, j, k;
  1113. av_freep(&s->mb_type);
  1114. av_freep(&s->p_mv_table_base);
  1115. av_freep(&s->b_forw_mv_table_base);
  1116. av_freep(&s->b_back_mv_table_base);
  1117. av_freep(&s->b_bidir_forw_mv_table_base);
  1118. av_freep(&s->b_bidir_back_mv_table_base);
  1119. av_freep(&s->b_direct_mv_table_base);
  1120. s->p_mv_table = NULL;
  1121. s->b_forw_mv_table = NULL;
  1122. s->b_back_mv_table = NULL;
  1123. s->b_bidir_forw_mv_table = NULL;
  1124. s->b_bidir_back_mv_table = NULL;
  1125. s->b_direct_mv_table = NULL;
  1126. for (i = 0; i < 2; i++) {
  1127. for (j = 0; j < 2; j++) {
  1128. for (k = 0; k < 2; k++) {
  1129. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1130. s->b_field_mv_table[i][j][k] = NULL;
  1131. }
  1132. av_freep(&s->b_field_select_table[i][j]);
  1133. av_freep(&s->p_field_mv_table_base[i][j]);
  1134. s->p_field_mv_table[i][j] = NULL;
  1135. }
  1136. av_freep(&s->p_field_select_table[i]);
  1137. }
  1138. av_freep(&s->dc_val_base);
  1139. av_freep(&s->coded_block_base);
  1140. av_freep(&s->mbintra_table);
  1141. av_freep(&s->cbp_table);
  1142. av_freep(&s->pred_dir_table);
  1143. av_freep(&s->mbskip_table);
  1144. av_freep(&s->er.error_status_table);
  1145. av_freep(&s->er.er_temp_buffer);
  1146. av_freep(&s->mb_index2xy);
  1147. av_freep(&s->lambda_table);
  1148. av_freep(&s->cplx_tab);
  1149. av_freep(&s->bits_tab);
  1150. s->linesize = s->uvlinesize = 0;
  1151. return 0;
  1152. }
  1153. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1154. {
  1155. int i, err = 0;
  1156. if (s->slice_context_count > 1) {
  1157. for (i = 0; i < s->slice_context_count; i++) {
  1158. free_duplicate_context(s->thread_context[i]);
  1159. }
  1160. for (i = 1; i < s->slice_context_count; i++) {
  1161. av_freep(&s->thread_context[i]);
  1162. }
  1163. } else
  1164. free_duplicate_context(s);
  1165. if ((err = free_context_frame(s)) < 0)
  1166. return err;
  1167. if (s->picture)
  1168. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1169. s->picture[i].needs_realloc = 1;
  1170. }
  1171. s->last_picture_ptr =
  1172. s->next_picture_ptr =
  1173. s->current_picture_ptr = NULL;
  1174. // init
  1175. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1176. s->mb_height = (s->height + 31) / 32 * 2;
  1177. else
  1178. s->mb_height = (s->height + 15) / 16;
  1179. if ((s->width || s->height) &&
  1180. av_image_check_size(s->width, s->height, 0, s->avctx))
  1181. return AVERROR_INVALIDDATA;
  1182. if ((err = init_context_frame(s)))
  1183. goto fail;
  1184. s->thread_context[0] = s;
  1185. if (s->width && s->height) {
  1186. int nb_slices = s->slice_context_count;
  1187. if (nb_slices > 1) {
  1188. for (i = 1; i < nb_slices; i++) {
  1189. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1190. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1191. }
  1192. for (i = 0; i < nb_slices; i++) {
  1193. if (init_duplicate_context(s->thread_context[i]) < 0)
  1194. goto fail;
  1195. s->thread_context[i]->start_mb_y =
  1196. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1197. s->thread_context[i]->end_mb_y =
  1198. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1199. }
  1200. } else {
  1201. err = init_duplicate_context(s);
  1202. if (err < 0)
  1203. goto fail;
  1204. s->start_mb_y = 0;
  1205. s->end_mb_y = s->mb_height;
  1206. }
  1207. s->slice_context_count = nb_slices;
  1208. }
  1209. return 0;
  1210. fail:
  1211. ff_MPV_common_end(s);
  1212. return err;
  1213. }
  1214. /* init common structure for both encoder and decoder */
  1215. void ff_MPV_common_end(MpegEncContext *s)
  1216. {
  1217. int i;
  1218. if (s->slice_context_count > 1) {
  1219. for (i = 0; i < s->slice_context_count; i++) {
  1220. free_duplicate_context(s->thread_context[i]);
  1221. }
  1222. for (i = 1; i < s->slice_context_count; i++) {
  1223. av_freep(&s->thread_context[i]);
  1224. }
  1225. s->slice_context_count = 1;
  1226. } else free_duplicate_context(s);
  1227. av_freep(&s->parse_context.buffer);
  1228. s->parse_context.buffer_size = 0;
  1229. av_freep(&s->bitstream_buffer);
  1230. s->allocated_bitstream_buffer_size = 0;
  1231. if (s->picture) {
  1232. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1233. ff_free_picture_tables(&s->picture[i]);
  1234. ff_mpeg_unref_picture(s, &s->picture[i]);
  1235. }
  1236. }
  1237. av_freep(&s->picture);
  1238. ff_free_picture_tables(&s->last_picture);
  1239. ff_mpeg_unref_picture(s, &s->last_picture);
  1240. ff_free_picture_tables(&s->current_picture);
  1241. ff_mpeg_unref_picture(s, &s->current_picture);
  1242. ff_free_picture_tables(&s->next_picture);
  1243. ff_mpeg_unref_picture(s, &s->next_picture);
  1244. free_context_frame(s);
  1245. s->context_initialized = 0;
  1246. s->last_picture_ptr =
  1247. s->next_picture_ptr =
  1248. s->current_picture_ptr = NULL;
  1249. s->linesize = s->uvlinesize = 0;
  1250. }
  1251. av_cold void ff_init_rl(RLTable *rl,
  1252. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1253. {
  1254. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1255. uint8_t index_run[MAX_RUN + 1];
  1256. int last, run, level, start, end, i;
  1257. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1258. if (static_store && rl->max_level[0])
  1259. return;
  1260. /* compute max_level[], max_run[] and index_run[] */
  1261. for (last = 0; last < 2; last++) {
  1262. if (last == 0) {
  1263. start = 0;
  1264. end = rl->last;
  1265. } else {
  1266. start = rl->last;
  1267. end = rl->n;
  1268. }
  1269. memset(max_level, 0, MAX_RUN + 1);
  1270. memset(max_run, 0, MAX_LEVEL + 1);
  1271. memset(index_run, rl->n, MAX_RUN + 1);
  1272. for (i = start; i < end; i++) {
  1273. run = rl->table_run[i];
  1274. level = rl->table_level[i];
  1275. if (index_run[run] == rl->n)
  1276. index_run[run] = i;
  1277. if (level > max_level[run])
  1278. max_level[run] = level;
  1279. if (run > max_run[level])
  1280. max_run[level] = run;
  1281. }
  1282. if (static_store)
  1283. rl->max_level[last] = static_store[last];
  1284. else
  1285. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1286. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1287. if (static_store)
  1288. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1289. else
  1290. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1291. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1292. if (static_store)
  1293. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1294. else
  1295. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1296. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1297. }
  1298. }
  1299. av_cold void ff_init_vlc_rl(RLTable *rl)
  1300. {
  1301. int i, q;
  1302. for (q = 0; q < 32; q++) {
  1303. int qmul = q * 2;
  1304. int qadd = (q - 1) | 1;
  1305. if (q == 0) {
  1306. qmul = 1;
  1307. qadd = 0;
  1308. }
  1309. for (i = 0; i < rl->vlc.table_size; i++) {
  1310. int code = rl->vlc.table[i][0];
  1311. int len = rl->vlc.table[i][1];
  1312. int level, run;
  1313. if (len == 0) { // illegal code
  1314. run = 66;
  1315. level = MAX_LEVEL;
  1316. } else if (len < 0) { // more bits needed
  1317. run = 0;
  1318. level = code;
  1319. } else {
  1320. if (code == rl->n) { // esc
  1321. run = 66;
  1322. level = 0;
  1323. } else {
  1324. run = rl->table_run[code] + 1;
  1325. level = rl->table_level[code] * qmul + qadd;
  1326. if (code >= rl->last) run += 192;
  1327. }
  1328. }
  1329. rl->rl_vlc[q][i].len = len;
  1330. rl->rl_vlc[q][i].level = level;
  1331. rl->rl_vlc[q][i].run = run;
  1332. }
  1333. }
  1334. }
  1335. static void release_unused_pictures(MpegEncContext *s)
  1336. {
  1337. int i;
  1338. /* release non reference frames */
  1339. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1340. if (!s->picture[i].reference)
  1341. ff_mpeg_unref_picture(s, &s->picture[i]);
  1342. }
  1343. }
  1344. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1345. {
  1346. if (pic == s->last_picture_ptr)
  1347. return 0;
  1348. if (pic->f.buf[0] == NULL)
  1349. return 1;
  1350. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1351. return 1;
  1352. return 0;
  1353. }
  1354. static int find_unused_picture(MpegEncContext *s, int shared)
  1355. {
  1356. int i;
  1357. if (shared) {
  1358. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1359. if (s->picture[i].f.buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
  1360. return i;
  1361. }
  1362. } else {
  1363. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1364. if (pic_is_unused(s, &s->picture[i]))
  1365. return i;
  1366. }
  1367. }
  1368. av_log(s->avctx, AV_LOG_FATAL,
  1369. "Internal error, picture buffer overflow\n");
  1370. /* We could return -1, but the codec would crash trying to draw into a
  1371. * non-existing frame anyway. This is safer than waiting for a random crash.
  1372. * Also the return of this is never useful, an encoder must only allocate
  1373. * as much as allowed in the specification. This has no relationship to how
  1374. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1375. * enough for such valid streams).
  1376. * Plus, a decoder has to check stream validity and remove frames if too
  1377. * many reference frames are around. Waiting for "OOM" is not correct at
  1378. * all. Similarly, missing reference frames have to be replaced by
  1379. * interpolated/MC frames, anything else is a bug in the codec ...
  1380. */
  1381. abort();
  1382. return -1;
  1383. }
  1384. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1385. {
  1386. int ret = find_unused_picture(s, shared);
  1387. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1388. if (s->picture[ret].needs_realloc) {
  1389. s->picture[ret].needs_realloc = 0;
  1390. ff_free_picture_tables(&s->picture[ret]);
  1391. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1392. }
  1393. }
  1394. return ret;
  1395. }
  1396. /**
  1397. * generic function called after decoding
  1398. * the header and before a frame is decoded.
  1399. */
  1400. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1401. {
  1402. int i, ret;
  1403. Picture *pic;
  1404. s->mb_skipped = 0;
  1405. if (!ff_thread_can_start_frame(avctx)) {
  1406. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1407. return -1;
  1408. }
  1409. /* mark & release old frames */
  1410. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1411. s->last_picture_ptr != s->next_picture_ptr &&
  1412. s->last_picture_ptr->f.buf[0]) {
  1413. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1414. }
  1415. /* release forgotten pictures */
  1416. /* if (mpeg124/h263) */
  1417. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1418. if (&s->picture[i] != s->last_picture_ptr &&
  1419. &s->picture[i] != s->next_picture_ptr &&
  1420. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1421. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1422. av_log(avctx, AV_LOG_ERROR,
  1423. "releasing zombie picture\n");
  1424. ff_mpeg_unref_picture(s, &s->picture[i]);
  1425. }
  1426. }
  1427. ff_mpeg_unref_picture(s, &s->current_picture);
  1428. release_unused_pictures(s);
  1429. if (s->current_picture_ptr &&
  1430. s->current_picture_ptr->f.buf[0] == NULL) {
  1431. // we already have a unused image
  1432. // (maybe it was set before reading the header)
  1433. pic = s->current_picture_ptr;
  1434. } else {
  1435. i = ff_find_unused_picture(s, 0);
  1436. if (i < 0) {
  1437. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1438. return i;
  1439. }
  1440. pic = &s->picture[i];
  1441. }
  1442. pic->reference = 0;
  1443. if (!s->droppable) {
  1444. if (s->pict_type != AV_PICTURE_TYPE_B)
  1445. pic->reference = 3;
  1446. }
  1447. pic->f.coded_picture_number = s->coded_picture_number++;
  1448. if (ff_alloc_picture(s, pic, 0) < 0)
  1449. return -1;
  1450. s->current_picture_ptr = pic;
  1451. // FIXME use only the vars from current_pic
  1452. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1453. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1454. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1455. if (s->picture_structure != PICT_FRAME)
  1456. s->current_picture_ptr->f.top_field_first =
  1457. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1458. }
  1459. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1460. !s->progressive_sequence;
  1461. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1462. s->current_picture_ptr->f.pict_type = s->pict_type;
  1463. // if (s->flags && CODEC_FLAG_QSCALE)
  1464. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1465. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1466. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1467. s->current_picture_ptr)) < 0)
  1468. return ret;
  1469. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1470. s->last_picture_ptr = s->next_picture_ptr;
  1471. if (!s->droppable)
  1472. s->next_picture_ptr = s->current_picture_ptr;
  1473. }
  1474. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1475. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1476. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1477. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1478. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1479. s->pict_type, s->droppable);
  1480. if ((s->last_picture_ptr == NULL ||
  1481. s->last_picture_ptr->f.buf[0] == NULL) &&
  1482. (s->pict_type != AV_PICTURE_TYPE_I ||
  1483. s->picture_structure != PICT_FRAME)) {
  1484. int h_chroma_shift, v_chroma_shift;
  1485. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1486. &h_chroma_shift, &v_chroma_shift);
  1487. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f.buf[0])
  1488. av_log(avctx, AV_LOG_DEBUG,
  1489. "allocating dummy last picture for B frame\n");
  1490. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1491. av_log(avctx, AV_LOG_ERROR,
  1492. "warning: first frame is no keyframe\n");
  1493. else if (s->picture_structure != PICT_FRAME)
  1494. av_log(avctx, AV_LOG_DEBUG,
  1495. "allocate dummy last picture for field based first keyframe\n");
  1496. /* Allocate a dummy frame */
  1497. i = ff_find_unused_picture(s, 0);
  1498. if (i < 0) {
  1499. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1500. return i;
  1501. }
  1502. s->last_picture_ptr = &s->picture[i];
  1503. s->last_picture_ptr->reference = 3;
  1504. s->last_picture_ptr->f.key_frame = 0;
  1505. s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1506. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1507. s->last_picture_ptr = NULL;
  1508. return -1;
  1509. }
  1510. memset(s->last_picture_ptr->f.data[0], 0x80,
  1511. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1512. memset(s->last_picture_ptr->f.data[1], 0x80,
  1513. (avctx->height >> v_chroma_shift) *
  1514. s->last_picture_ptr->f.linesize[1]);
  1515. memset(s->last_picture_ptr->f.data[2], 0x80,
  1516. (avctx->height >> v_chroma_shift) *
  1517. s->last_picture_ptr->f.linesize[2]);
  1518. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1519. for(i=0; i<avctx->height; i++)
  1520. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1521. }
  1522. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1523. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1524. }
  1525. if ((s->next_picture_ptr == NULL ||
  1526. s->next_picture_ptr->f.buf[0] == NULL) &&
  1527. s->pict_type == AV_PICTURE_TYPE_B) {
  1528. /* Allocate a dummy frame */
  1529. i = ff_find_unused_picture(s, 0);
  1530. if (i < 0) {
  1531. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1532. return i;
  1533. }
  1534. s->next_picture_ptr = &s->picture[i];
  1535. s->next_picture_ptr->reference = 3;
  1536. s->next_picture_ptr->f.key_frame = 0;
  1537. s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1538. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1539. s->next_picture_ptr = NULL;
  1540. return -1;
  1541. }
  1542. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1543. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1544. }
  1545. #if 0 // BUFREF-FIXME
  1546. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1547. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1548. #endif
  1549. if (s->last_picture_ptr) {
  1550. ff_mpeg_unref_picture(s, &s->last_picture);
  1551. if (s->last_picture_ptr->f.buf[0] &&
  1552. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1553. s->last_picture_ptr)) < 0)
  1554. return ret;
  1555. }
  1556. if (s->next_picture_ptr) {
  1557. ff_mpeg_unref_picture(s, &s->next_picture);
  1558. if (s->next_picture_ptr->f.buf[0] &&
  1559. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1560. s->next_picture_ptr)) < 0)
  1561. return ret;
  1562. }
  1563. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1564. s->last_picture_ptr->f.buf[0]));
  1565. if (s->picture_structure!= PICT_FRAME) {
  1566. int i;
  1567. for (i = 0; i < 4; i++) {
  1568. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1569. s->current_picture.f.data[i] +=
  1570. s->current_picture.f.linesize[i];
  1571. }
  1572. s->current_picture.f.linesize[i] *= 2;
  1573. s->last_picture.f.linesize[i] *= 2;
  1574. s->next_picture.f.linesize[i] *= 2;
  1575. }
  1576. }
  1577. s->err_recognition = avctx->err_recognition;
  1578. /* set dequantizer, we can't do it during init as
  1579. * it might change for mpeg4 and we can't do it in the header
  1580. * decode as init is not called for mpeg4 there yet */
  1581. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1582. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1583. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1584. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1585. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1586. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1587. } else {
  1588. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1589. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1590. }
  1591. return 0;
  1592. }
  1593. /* called after a frame has been decoded. */
  1594. void ff_MPV_frame_end(MpegEncContext *s)
  1595. {
  1596. if ((s->er.error_count || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
  1597. !s->avctx->hwaccel &&
  1598. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1599. s->unrestricted_mv &&
  1600. s->current_picture.reference &&
  1601. !s->intra_only &&
  1602. !(s->flags & CODEC_FLAG_EMU_EDGE) &&
  1603. !s->avctx->lowres
  1604. ) {
  1605. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1606. int hshift = desc->log2_chroma_w;
  1607. int vshift = desc->log2_chroma_h;
  1608. s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
  1609. s->h_edge_pos, s->v_edge_pos,
  1610. EDGE_WIDTH, EDGE_WIDTH,
  1611. EDGE_TOP | EDGE_BOTTOM);
  1612. s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
  1613. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1614. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1615. EDGE_TOP | EDGE_BOTTOM);
  1616. s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
  1617. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1618. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1619. EDGE_TOP | EDGE_BOTTOM);
  1620. }
  1621. emms_c();
  1622. if (s->current_picture.reference)
  1623. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1624. }
  1625. /**
  1626. * Draw a line from (ex, ey) -> (sx, sy).
  1627. * @param w width of the image
  1628. * @param h height of the image
  1629. * @param stride stride/linesize of the image
  1630. * @param color color of the arrow
  1631. */
  1632. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1633. int w, int h, int stride, int color)
  1634. {
  1635. int x, y, fr, f;
  1636. sx = av_clip(sx, 0, w - 1);
  1637. sy = av_clip(sy, 0, h - 1);
  1638. ex = av_clip(ex, 0, w - 1);
  1639. ey = av_clip(ey, 0, h - 1);
  1640. buf[sy * stride + sx] += color;
  1641. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1642. if (sx > ex) {
  1643. FFSWAP(int, sx, ex);
  1644. FFSWAP(int, sy, ey);
  1645. }
  1646. buf += sx + sy * stride;
  1647. ex -= sx;
  1648. f = ((ey - sy) << 16) / ex;
  1649. for (x = 0; x <= ex; x++) {
  1650. y = (x * f) >> 16;
  1651. fr = (x * f) & 0xFFFF;
  1652. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1653. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1654. }
  1655. } else {
  1656. if (sy > ey) {
  1657. FFSWAP(int, sx, ex);
  1658. FFSWAP(int, sy, ey);
  1659. }
  1660. buf += sx + sy * stride;
  1661. ey -= sy;
  1662. if (ey)
  1663. f = ((ex - sx) << 16) / ey;
  1664. else
  1665. f = 0;
  1666. for(y= 0; y <= ey; y++){
  1667. x = (y*f) >> 16;
  1668. fr = (y*f) & 0xFFFF;
  1669. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1670. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1671. }
  1672. }
  1673. }
  1674. /**
  1675. * Draw an arrow from (ex, ey) -> (sx, sy).
  1676. * @param w width of the image
  1677. * @param h height of the image
  1678. * @param stride stride/linesize of the image
  1679. * @param color color of the arrow
  1680. */
  1681. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1682. int ey, int w, int h, int stride, int color)
  1683. {
  1684. int dx,dy;
  1685. sx = av_clip(sx, -100, w + 100);
  1686. sy = av_clip(sy, -100, h + 100);
  1687. ex = av_clip(ex, -100, w + 100);
  1688. ey = av_clip(ey, -100, h + 100);
  1689. dx = ex - sx;
  1690. dy = ey - sy;
  1691. if (dx * dx + dy * dy > 3 * 3) {
  1692. int rx = dx + dy;
  1693. int ry = -dx + dy;
  1694. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1695. // FIXME subpixel accuracy
  1696. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1697. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1698. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1699. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1700. }
  1701. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1702. }
  1703. /**
  1704. * Print debugging info for the given picture.
  1705. */
  1706. void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
  1707. int *low_delay,
  1708. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1709. {
  1710. if (avctx->hwaccel || !p || !p->mb_type
  1711. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1712. return;
  1713. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1714. int x,y;
  1715. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1716. av_get_picture_type_char(pict->pict_type));
  1717. for (y = 0; y < mb_height; y++) {
  1718. for (x = 0; x < mb_width; x++) {
  1719. if (avctx->debug & FF_DEBUG_SKIP) {
  1720. int count = mbskip_table[x + y * mb_stride];
  1721. if (count > 9)
  1722. count = 9;
  1723. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1724. }
  1725. if (avctx->debug & FF_DEBUG_QP) {
  1726. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1727. p->qscale_table[x + y * mb_stride]);
  1728. }
  1729. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1730. int mb_type = p->mb_type[x + y * mb_stride];
  1731. // Type & MV direction
  1732. if (IS_PCM(mb_type))
  1733. av_log(avctx, AV_LOG_DEBUG, "P");
  1734. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1735. av_log(avctx, AV_LOG_DEBUG, "A");
  1736. else if (IS_INTRA4x4(mb_type))
  1737. av_log(avctx, AV_LOG_DEBUG, "i");
  1738. else if (IS_INTRA16x16(mb_type))
  1739. av_log(avctx, AV_LOG_DEBUG, "I");
  1740. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1741. av_log(avctx, AV_LOG_DEBUG, "d");
  1742. else if (IS_DIRECT(mb_type))
  1743. av_log(avctx, AV_LOG_DEBUG, "D");
  1744. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1745. av_log(avctx, AV_LOG_DEBUG, "g");
  1746. else if (IS_GMC(mb_type))
  1747. av_log(avctx, AV_LOG_DEBUG, "G");
  1748. else if (IS_SKIP(mb_type))
  1749. av_log(avctx, AV_LOG_DEBUG, "S");
  1750. else if (!USES_LIST(mb_type, 1))
  1751. av_log(avctx, AV_LOG_DEBUG, ">");
  1752. else if (!USES_LIST(mb_type, 0))
  1753. av_log(avctx, AV_LOG_DEBUG, "<");
  1754. else {
  1755. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1756. av_log(avctx, AV_LOG_DEBUG, "X");
  1757. }
  1758. // segmentation
  1759. if (IS_8X8(mb_type))
  1760. av_log(avctx, AV_LOG_DEBUG, "+");
  1761. else if (IS_16X8(mb_type))
  1762. av_log(avctx, AV_LOG_DEBUG, "-");
  1763. else if (IS_8X16(mb_type))
  1764. av_log(avctx, AV_LOG_DEBUG, "|");
  1765. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1766. av_log(avctx, AV_LOG_DEBUG, " ");
  1767. else
  1768. av_log(avctx, AV_LOG_DEBUG, "?");
  1769. if (IS_INTERLACED(mb_type))
  1770. av_log(avctx, AV_LOG_DEBUG, "=");
  1771. else
  1772. av_log(avctx, AV_LOG_DEBUG, " ");
  1773. }
  1774. }
  1775. av_log(avctx, AV_LOG_DEBUG, "\n");
  1776. }
  1777. }
  1778. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1779. (avctx->debug_mv)) {
  1780. const int shift = 1 + quarter_sample;
  1781. int mb_y;
  1782. uint8_t *ptr;
  1783. int i;
  1784. int h_chroma_shift, v_chroma_shift, block_height;
  1785. const int width = avctx->width;
  1786. const int height = avctx->height;
  1787. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1788. const int mv_stride = (mb_width << mv_sample_log2) +
  1789. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1790. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1791. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1792. av_frame_make_writable(pict);
  1793. pict->opaque = NULL;
  1794. ptr = pict->data[0];
  1795. block_height = 16 >> v_chroma_shift;
  1796. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1797. int mb_x;
  1798. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1799. const int mb_index = mb_x + mb_y * mb_stride;
  1800. if ((avctx->debug_mv) && p->motion_val[0]) {
  1801. int type;
  1802. for (type = 0; type < 3; type++) {
  1803. int direction = 0;
  1804. switch (type) {
  1805. case 0:
  1806. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1807. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1808. continue;
  1809. direction = 0;
  1810. break;
  1811. case 1:
  1812. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1813. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1814. continue;
  1815. direction = 0;
  1816. break;
  1817. case 2:
  1818. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1819. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1820. continue;
  1821. direction = 1;
  1822. break;
  1823. }
  1824. if (!USES_LIST(p->mb_type[mb_index], direction))
  1825. continue;
  1826. if (IS_8X8(p->mb_type[mb_index])) {
  1827. int i;
  1828. for (i = 0; i < 4; i++) {
  1829. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1830. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1831. int xy = (mb_x * 2 + (i & 1) +
  1832. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1833. int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
  1834. int my = (p->motion_val[direction][xy][1] >> shift) + sy;
  1835. draw_arrow(ptr, sx, sy, mx, my, width,
  1836. height, pict->linesize[0], 100);
  1837. }
  1838. } else if (IS_16X8(p->mb_type[mb_index])) {
  1839. int i;
  1840. for (i = 0; i < 2; i++) {
  1841. int sx = mb_x * 16 + 8;
  1842. int sy = mb_y * 16 + 4 + 8 * i;
  1843. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1844. int mx = (p->motion_val[direction][xy][0] >> shift);
  1845. int my = (p->motion_val[direction][xy][1] >> shift);
  1846. if (IS_INTERLACED(p->mb_type[mb_index]))
  1847. my *= 2;
  1848. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1849. height, pict->linesize[0], 100);
  1850. }
  1851. } else if (IS_8X16(p->mb_type[mb_index])) {
  1852. int i;
  1853. for (i = 0; i < 2; i++) {
  1854. int sx = mb_x * 16 + 4 + 8 * i;
  1855. int sy = mb_y * 16 + 8;
  1856. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1857. int mx = p->motion_val[direction][xy][0] >> shift;
  1858. int my = p->motion_val[direction][xy][1] >> shift;
  1859. if (IS_INTERLACED(p->mb_type[mb_index]))
  1860. my *= 2;
  1861. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1862. height, pict->linesize[0], 100);
  1863. }
  1864. } else {
  1865. int sx= mb_x * 16 + 8;
  1866. int sy= mb_y * 16 + 8;
  1867. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1868. int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
  1869. int my= (p->motion_val[direction][xy][1]>>shift) + sy;
  1870. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
  1871. }
  1872. }
  1873. }
  1874. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1875. uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
  1876. 0x0101010101010101ULL;
  1877. int y;
  1878. for (y = 0; y < block_height; y++) {
  1879. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1880. (block_height * mb_y + y) *
  1881. pict->linesize[1]) = c;
  1882. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1883. (block_height * mb_y + y) *
  1884. pict->linesize[2]) = c;
  1885. }
  1886. }
  1887. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1888. p->motion_val[0]) {
  1889. int mb_type = p->mb_type[mb_index];
  1890. uint64_t u,v;
  1891. int y;
  1892. #define COLOR(theta, r) \
  1893. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1894. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1895. u = v = 128;
  1896. if (IS_PCM(mb_type)) {
  1897. COLOR(120, 48)
  1898. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1899. IS_INTRA16x16(mb_type)) {
  1900. COLOR(30, 48)
  1901. } else if (IS_INTRA4x4(mb_type)) {
  1902. COLOR(90, 48)
  1903. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1904. // COLOR(120, 48)
  1905. } else if (IS_DIRECT(mb_type)) {
  1906. COLOR(150, 48)
  1907. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1908. COLOR(170, 48)
  1909. } else if (IS_GMC(mb_type)) {
  1910. COLOR(190, 48)
  1911. } else if (IS_SKIP(mb_type)) {
  1912. // COLOR(180, 48)
  1913. } else if (!USES_LIST(mb_type, 1)) {
  1914. COLOR(240, 48)
  1915. } else if (!USES_LIST(mb_type, 0)) {
  1916. COLOR(0, 48)
  1917. } else {
  1918. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1919. COLOR(300,48)
  1920. }
  1921. u *= 0x0101010101010101ULL;
  1922. v *= 0x0101010101010101ULL;
  1923. for (y = 0; y < block_height; y++) {
  1924. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1925. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1926. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1927. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1928. }
  1929. // segmentation
  1930. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1931. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1932. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1933. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1934. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1935. }
  1936. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1937. for (y = 0; y < 16; y++)
  1938. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1939. pict->linesize[0]] ^= 0x80;
  1940. }
  1941. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1942. int dm = 1 << (mv_sample_log2 - 2);
  1943. for (i = 0; i < 4; i++) {
  1944. int sx = mb_x * 16 + 8 * (i & 1);
  1945. int sy = mb_y * 16 + 8 * (i >> 1);
  1946. int xy = (mb_x * 2 + (i & 1) +
  1947. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1948. // FIXME bidir
  1949. int32_t *mv = (int32_t *) &p->motion_val[0][xy];
  1950. if (mv[0] != mv[dm] ||
  1951. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1952. for (y = 0; y < 8; y++)
  1953. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1954. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1955. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1956. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1957. }
  1958. }
  1959. if (IS_INTERLACED(mb_type) &&
  1960. avctx->codec->id == AV_CODEC_ID_H264) {
  1961. // hmm
  1962. }
  1963. }
  1964. mbskip_table[mb_index] = 0;
  1965. }
  1966. }
  1967. }
  1968. }
  1969. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1970. {
  1971. ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
  1972. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1973. }
  1974. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1975. {
  1976. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1977. int offset = 2*s->mb_stride + 1;
  1978. if(!ref)
  1979. return AVERROR(ENOMEM);
  1980. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1981. ref->size -= offset;
  1982. ref->data += offset;
  1983. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1984. }
  1985. static inline int hpel_motion_lowres(MpegEncContext *s,
  1986. uint8_t *dest, uint8_t *src,
  1987. int field_based, int field_select,
  1988. int src_x, int src_y,
  1989. int width, int height, ptrdiff_t stride,
  1990. int h_edge_pos, int v_edge_pos,
  1991. int w, int h, h264_chroma_mc_func *pix_op,
  1992. int motion_x, int motion_y)
  1993. {
  1994. const int lowres = s->avctx->lowres;
  1995. const int op_index = FFMIN(lowres, 3);
  1996. const int s_mask = (2 << lowres) - 1;
  1997. int emu = 0;
  1998. int sx, sy;
  1999. if (s->quarter_sample) {
  2000. motion_x /= 2;
  2001. motion_y /= 2;
  2002. }
  2003. sx = motion_x & s_mask;
  2004. sy = motion_y & s_mask;
  2005. src_x += motion_x >> lowres + 1;
  2006. src_y += motion_y >> lowres + 1;
  2007. src += src_y * stride + src_x;
  2008. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  2009. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2010. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  2011. s->linesize, s->linesize,
  2012. w + 1, (h + 1) << field_based,
  2013. src_x, src_y << field_based,
  2014. h_edge_pos, v_edge_pos);
  2015. src = s->edge_emu_buffer;
  2016. emu = 1;
  2017. }
  2018. sx = (sx << 2) >> lowres;
  2019. sy = (sy << 2) >> lowres;
  2020. if (field_select)
  2021. src += s->linesize;
  2022. pix_op[op_index](dest, src, stride, h, sx, sy);
  2023. return emu;
  2024. }
  2025. /* apply one mpeg motion vector to the three components */
  2026. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  2027. uint8_t *dest_y,
  2028. uint8_t *dest_cb,
  2029. uint8_t *dest_cr,
  2030. int field_based,
  2031. int bottom_field,
  2032. int field_select,
  2033. uint8_t **ref_picture,
  2034. h264_chroma_mc_func *pix_op,
  2035. int motion_x, int motion_y,
  2036. int h, int mb_y)
  2037. {
  2038. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2039. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  2040. ptrdiff_t uvlinesize, linesize;
  2041. const int lowres = s->avctx->lowres;
  2042. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  2043. const int block_s = 8>>lowres;
  2044. const int s_mask = (2 << lowres) - 1;
  2045. const int h_edge_pos = s->h_edge_pos >> lowres;
  2046. const int v_edge_pos = s->v_edge_pos >> lowres;
  2047. linesize = s->current_picture.f.linesize[0] << field_based;
  2048. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  2049. // FIXME obviously not perfect but qpel will not work in lowres anyway
  2050. if (s->quarter_sample) {
  2051. motion_x /= 2;
  2052. motion_y /= 2;
  2053. }
  2054. if(field_based){
  2055. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  2056. }
  2057. sx = motion_x & s_mask;
  2058. sy = motion_y & s_mask;
  2059. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  2060. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  2061. if (s->out_format == FMT_H263) {
  2062. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  2063. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  2064. uvsrc_x = src_x >> 1;
  2065. uvsrc_y = src_y >> 1;
  2066. } else if (s->out_format == FMT_H261) {
  2067. // even chroma mv's are full pel in H261
  2068. mx = motion_x / 4;
  2069. my = motion_y / 4;
  2070. uvsx = (2 * mx) & s_mask;
  2071. uvsy = (2 * my) & s_mask;
  2072. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  2073. uvsrc_y = mb_y * block_s + (my >> lowres);
  2074. } else {
  2075. if(s->chroma_y_shift){
  2076. mx = motion_x / 2;
  2077. my = motion_y / 2;
  2078. uvsx = mx & s_mask;
  2079. uvsy = my & s_mask;
  2080. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  2081. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  2082. } else {
  2083. if(s->chroma_x_shift){
  2084. //Chroma422
  2085. mx = motion_x / 2;
  2086. uvsx = mx & s_mask;
  2087. uvsy = motion_y & s_mask;
  2088. uvsrc_y = src_y;
  2089. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2090. } else {
  2091. //Chroma444
  2092. uvsx = motion_x & s_mask;
  2093. uvsy = motion_y & s_mask;
  2094. uvsrc_x = src_x;
  2095. uvsrc_y = src_y;
  2096. }
  2097. }
  2098. }
  2099. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2100. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2101. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2102. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  2103. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2104. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2105. linesize >> field_based, linesize >> field_based,
  2106. 17, 17 + field_based,
  2107. src_x, src_y << field_based, h_edge_pos,
  2108. v_edge_pos);
  2109. ptr_y = s->edge_emu_buffer;
  2110. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2111. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  2112. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  2113. uvlinesize >> field_based, uvlinesize >> field_based,
  2114. 9, 9 + field_based,
  2115. uvsrc_x, uvsrc_y << field_based,
  2116. h_edge_pos >> 1, v_edge_pos >> 1);
  2117. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  2118. uvlinesize >> field_based,uvlinesize >> field_based,
  2119. 9, 9 + field_based,
  2120. uvsrc_x, uvsrc_y << field_based,
  2121. h_edge_pos >> 1, v_edge_pos >> 1);
  2122. ptr_cb = uvbuf;
  2123. ptr_cr = uvbuf + 16;
  2124. }
  2125. }
  2126. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  2127. if (bottom_field) {
  2128. dest_y += s->linesize;
  2129. dest_cb += s->uvlinesize;
  2130. dest_cr += s->uvlinesize;
  2131. }
  2132. if (field_select) {
  2133. ptr_y += s->linesize;
  2134. ptr_cb += s->uvlinesize;
  2135. ptr_cr += s->uvlinesize;
  2136. }
  2137. sx = (sx << 2) >> lowres;
  2138. sy = (sy << 2) >> lowres;
  2139. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2140. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2141. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  2142. uvsx = (uvsx << 2) >> lowres;
  2143. uvsy = (uvsy << 2) >> lowres;
  2144. if (hc) {
  2145. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  2146. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  2147. }
  2148. }
  2149. // FIXME h261 lowres loop filter
  2150. }
  2151. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2152. uint8_t *dest_cb, uint8_t *dest_cr,
  2153. uint8_t **ref_picture,
  2154. h264_chroma_mc_func * pix_op,
  2155. int mx, int my)
  2156. {
  2157. const int lowres = s->avctx->lowres;
  2158. const int op_index = FFMIN(lowres, 3);
  2159. const int block_s = 8 >> lowres;
  2160. const int s_mask = (2 << lowres) - 1;
  2161. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2162. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2163. int emu = 0, src_x, src_y, sx, sy;
  2164. ptrdiff_t offset;
  2165. uint8_t *ptr;
  2166. if (s->quarter_sample) {
  2167. mx /= 2;
  2168. my /= 2;
  2169. }
  2170. /* In case of 8X8, we construct a single chroma motion vector
  2171. with a special rounding */
  2172. mx = ff_h263_round_chroma(mx);
  2173. my = ff_h263_round_chroma(my);
  2174. sx = mx & s_mask;
  2175. sy = my & s_mask;
  2176. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2177. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2178. offset = src_y * s->uvlinesize + src_x;
  2179. ptr = ref_picture[1] + offset;
  2180. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2181. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2182. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2183. s->uvlinesize, s->uvlinesize,
  2184. 9, 9,
  2185. src_x, src_y, h_edge_pos, v_edge_pos);
  2186. ptr = s->edge_emu_buffer;
  2187. emu = 1;
  2188. }
  2189. sx = (sx << 2) >> lowres;
  2190. sy = (sy << 2) >> lowres;
  2191. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2192. ptr = ref_picture[2] + offset;
  2193. if (emu) {
  2194. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2195. s->uvlinesize, s->uvlinesize,
  2196. 9, 9,
  2197. src_x, src_y, h_edge_pos, v_edge_pos);
  2198. ptr = s->edge_emu_buffer;
  2199. }
  2200. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2201. }
  2202. /**
  2203. * motion compensation of a single macroblock
  2204. * @param s context
  2205. * @param dest_y luma destination pointer
  2206. * @param dest_cb chroma cb/u destination pointer
  2207. * @param dest_cr chroma cr/v destination pointer
  2208. * @param dir direction (0->forward, 1->backward)
  2209. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2210. * @param pix_op halfpel motion compensation function (average or put normally)
  2211. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2212. */
  2213. static inline void MPV_motion_lowres(MpegEncContext *s,
  2214. uint8_t *dest_y, uint8_t *dest_cb,
  2215. uint8_t *dest_cr,
  2216. int dir, uint8_t **ref_picture,
  2217. h264_chroma_mc_func *pix_op)
  2218. {
  2219. int mx, my;
  2220. int mb_x, mb_y, i;
  2221. const int lowres = s->avctx->lowres;
  2222. const int block_s = 8 >>lowres;
  2223. mb_x = s->mb_x;
  2224. mb_y = s->mb_y;
  2225. switch (s->mv_type) {
  2226. case MV_TYPE_16X16:
  2227. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2228. 0, 0, 0,
  2229. ref_picture, pix_op,
  2230. s->mv[dir][0][0], s->mv[dir][0][1],
  2231. 2 * block_s, mb_y);
  2232. break;
  2233. case MV_TYPE_8X8:
  2234. mx = 0;
  2235. my = 0;
  2236. for (i = 0; i < 4; i++) {
  2237. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2238. s->linesize) * block_s,
  2239. ref_picture[0], 0, 0,
  2240. (2 * mb_x + (i & 1)) * block_s,
  2241. (2 * mb_y + (i >> 1)) * block_s,
  2242. s->width, s->height, s->linesize,
  2243. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2244. block_s, block_s, pix_op,
  2245. s->mv[dir][i][0], s->mv[dir][i][1]);
  2246. mx += s->mv[dir][i][0];
  2247. my += s->mv[dir][i][1];
  2248. }
  2249. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2250. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2251. pix_op, mx, my);
  2252. break;
  2253. case MV_TYPE_FIELD:
  2254. if (s->picture_structure == PICT_FRAME) {
  2255. /* top field */
  2256. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2257. 1, 0, s->field_select[dir][0],
  2258. ref_picture, pix_op,
  2259. s->mv[dir][0][0], s->mv[dir][0][1],
  2260. block_s, mb_y);
  2261. /* bottom field */
  2262. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2263. 1, 1, s->field_select[dir][1],
  2264. ref_picture, pix_op,
  2265. s->mv[dir][1][0], s->mv[dir][1][1],
  2266. block_s, mb_y);
  2267. } else {
  2268. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2269. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2270. ref_picture = s->current_picture_ptr->f.data;
  2271. }
  2272. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2273. 0, 0, s->field_select[dir][0],
  2274. ref_picture, pix_op,
  2275. s->mv[dir][0][0],
  2276. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2277. }
  2278. break;
  2279. case MV_TYPE_16X8:
  2280. for (i = 0; i < 2; i++) {
  2281. uint8_t **ref2picture;
  2282. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2283. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2284. ref2picture = ref_picture;
  2285. } else {
  2286. ref2picture = s->current_picture_ptr->f.data;
  2287. }
  2288. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2289. 0, 0, s->field_select[dir][i],
  2290. ref2picture, pix_op,
  2291. s->mv[dir][i][0], s->mv[dir][i][1] +
  2292. 2 * block_s * i, block_s, mb_y >> 1);
  2293. dest_y += 2 * block_s * s->linesize;
  2294. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2295. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2296. }
  2297. break;
  2298. case MV_TYPE_DMV:
  2299. if (s->picture_structure == PICT_FRAME) {
  2300. for (i = 0; i < 2; i++) {
  2301. int j;
  2302. for (j = 0; j < 2; j++) {
  2303. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2304. 1, j, j ^ i,
  2305. ref_picture, pix_op,
  2306. s->mv[dir][2 * i + j][0],
  2307. s->mv[dir][2 * i + j][1],
  2308. block_s, mb_y);
  2309. }
  2310. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2311. }
  2312. } else {
  2313. for (i = 0; i < 2; i++) {
  2314. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2315. 0, 0, s->picture_structure != i + 1,
  2316. ref_picture, pix_op,
  2317. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2318. 2 * block_s, mb_y >> 1);
  2319. // after put we make avg of the same block
  2320. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2321. // opposite parity is always in the same
  2322. // frame if this is second field
  2323. if (!s->first_field) {
  2324. ref_picture = s->current_picture_ptr->f.data;
  2325. }
  2326. }
  2327. }
  2328. break;
  2329. default:
  2330. av_assert2(0);
  2331. }
  2332. }
  2333. /**
  2334. * find the lowest MB row referenced in the MVs
  2335. */
  2336. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2337. {
  2338. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2339. int my, off, i, mvs;
  2340. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2341. goto unhandled;
  2342. switch (s->mv_type) {
  2343. case MV_TYPE_16X16:
  2344. mvs = 1;
  2345. break;
  2346. case MV_TYPE_16X8:
  2347. mvs = 2;
  2348. break;
  2349. case MV_TYPE_8X8:
  2350. mvs = 4;
  2351. break;
  2352. default:
  2353. goto unhandled;
  2354. }
  2355. for (i = 0; i < mvs; i++) {
  2356. my = s->mv[dir][i][1]<<qpel_shift;
  2357. my_max = FFMAX(my_max, my);
  2358. my_min = FFMIN(my_min, my);
  2359. }
  2360. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2361. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2362. unhandled:
  2363. return s->mb_height-1;
  2364. }
  2365. /* put block[] to dest[] */
  2366. static inline void put_dct(MpegEncContext *s,
  2367. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2368. {
  2369. s->dct_unquantize_intra(s, block, i, qscale);
  2370. s->dsp.idct_put (dest, line_size, block);
  2371. }
  2372. /* add block[] to dest[] */
  2373. static inline void add_dct(MpegEncContext *s,
  2374. int16_t *block, int i, uint8_t *dest, int line_size)
  2375. {
  2376. if (s->block_last_index[i] >= 0) {
  2377. s->dsp.idct_add (dest, line_size, block);
  2378. }
  2379. }
  2380. static inline void add_dequant_dct(MpegEncContext *s,
  2381. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2382. {
  2383. if (s->block_last_index[i] >= 0) {
  2384. s->dct_unquantize_inter(s, block, i, qscale);
  2385. s->dsp.idct_add (dest, line_size, block);
  2386. }
  2387. }
  2388. /**
  2389. * Clean dc, ac, coded_block for the current non-intra MB.
  2390. */
  2391. void ff_clean_intra_table_entries(MpegEncContext *s)
  2392. {
  2393. int wrap = s->b8_stride;
  2394. int xy = s->block_index[0];
  2395. s->dc_val[0][xy ] =
  2396. s->dc_val[0][xy + 1 ] =
  2397. s->dc_val[0][xy + wrap] =
  2398. s->dc_val[0][xy + 1 + wrap] = 1024;
  2399. /* ac pred */
  2400. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2401. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2402. if (s->msmpeg4_version>=3) {
  2403. s->coded_block[xy ] =
  2404. s->coded_block[xy + 1 ] =
  2405. s->coded_block[xy + wrap] =
  2406. s->coded_block[xy + 1 + wrap] = 0;
  2407. }
  2408. /* chroma */
  2409. wrap = s->mb_stride;
  2410. xy = s->mb_x + s->mb_y * wrap;
  2411. s->dc_val[1][xy] =
  2412. s->dc_val[2][xy] = 1024;
  2413. /* ac pred */
  2414. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2415. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2416. s->mbintra_table[xy]= 0;
  2417. }
  2418. /* generic function called after a macroblock has been parsed by the
  2419. decoder or after it has been encoded by the encoder.
  2420. Important variables used:
  2421. s->mb_intra : true if intra macroblock
  2422. s->mv_dir : motion vector direction
  2423. s->mv_type : motion vector type
  2424. s->mv : motion vector
  2425. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2426. */
  2427. static av_always_inline
  2428. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2429. int lowres_flag, int is_mpeg12)
  2430. {
  2431. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2432. if (CONFIG_XVMC &&
  2433. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2434. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2435. return;
  2436. }
  2437. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2438. /* print DCT coefficients */
  2439. int i,j;
  2440. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2441. for(i=0; i<6; i++){
  2442. for(j=0; j<64; j++){
  2443. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  2444. }
  2445. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2446. }
  2447. }
  2448. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2449. /* update DC predictors for P macroblocks */
  2450. if (!s->mb_intra) {
  2451. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2452. if(s->mbintra_table[mb_xy])
  2453. ff_clean_intra_table_entries(s);
  2454. } else {
  2455. s->last_dc[0] =
  2456. s->last_dc[1] =
  2457. s->last_dc[2] = 128 << s->intra_dc_precision;
  2458. }
  2459. }
  2460. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2461. s->mbintra_table[mb_xy]=1;
  2462. if ( (s->flags&CODEC_FLAG_PSNR)
  2463. || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
  2464. || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  2465. uint8_t *dest_y, *dest_cb, *dest_cr;
  2466. int dct_linesize, dct_offset;
  2467. op_pixels_func (*op_pix)[4];
  2468. qpel_mc_func (*op_qpix)[16];
  2469. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2470. const int uvlinesize = s->current_picture.f.linesize[1];
  2471. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2472. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2473. /* avoid copy if macroblock skipped in last frame too */
  2474. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2475. if(!s->encoding){
  2476. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2477. if (s->mb_skipped) {
  2478. s->mb_skipped= 0;
  2479. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2480. *mbskip_ptr = 1;
  2481. } else if(!s->current_picture.reference) {
  2482. *mbskip_ptr = 1;
  2483. } else{
  2484. *mbskip_ptr = 0; /* not skipped */
  2485. }
  2486. }
  2487. dct_linesize = linesize << s->interlaced_dct;
  2488. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2489. if(readable){
  2490. dest_y= s->dest[0];
  2491. dest_cb= s->dest[1];
  2492. dest_cr= s->dest[2];
  2493. }else{
  2494. dest_y = s->b_scratchpad;
  2495. dest_cb= s->b_scratchpad+16*linesize;
  2496. dest_cr= s->b_scratchpad+32*linesize;
  2497. }
  2498. if (!s->mb_intra) {
  2499. /* motion handling */
  2500. /* decoding or more than one mb_type (MC was already done otherwise) */
  2501. if(!s->encoding){
  2502. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2503. if (s->mv_dir & MV_DIR_FORWARD) {
  2504. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2505. ff_MPV_lowest_referenced_row(s, 0),
  2506. 0);
  2507. }
  2508. if (s->mv_dir & MV_DIR_BACKWARD) {
  2509. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2510. ff_MPV_lowest_referenced_row(s, 1),
  2511. 0);
  2512. }
  2513. }
  2514. if(lowres_flag){
  2515. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2516. if (s->mv_dir & MV_DIR_FORWARD) {
  2517. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2518. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2519. }
  2520. if (s->mv_dir & MV_DIR_BACKWARD) {
  2521. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2522. }
  2523. }else{
  2524. op_qpix = s->me.qpel_put;
  2525. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2526. op_pix = s->hdsp.put_pixels_tab;
  2527. }else{
  2528. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2529. }
  2530. if (s->mv_dir & MV_DIR_FORWARD) {
  2531. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2532. op_pix = s->hdsp.avg_pixels_tab;
  2533. op_qpix= s->me.qpel_avg;
  2534. }
  2535. if (s->mv_dir & MV_DIR_BACKWARD) {
  2536. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2537. }
  2538. }
  2539. }
  2540. /* skip dequant / idct if we are really late ;) */
  2541. if(s->avctx->skip_idct){
  2542. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2543. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2544. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2545. goto skip_idct;
  2546. }
  2547. /* add dct residue */
  2548. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2549. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2550. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2551. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2552. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2553. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2554. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2555. if (s->chroma_y_shift){
  2556. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2557. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2558. }else{
  2559. dct_linesize >>= 1;
  2560. dct_offset >>=1;
  2561. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2562. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2563. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2564. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2565. }
  2566. }
  2567. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2568. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2569. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2570. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2571. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2572. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2573. if(s->chroma_y_shift){//Chroma420
  2574. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2575. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2576. }else{
  2577. //chroma422
  2578. dct_linesize = uvlinesize << s->interlaced_dct;
  2579. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2580. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2581. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2582. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2583. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2584. if(!s->chroma_x_shift){//Chroma444
  2585. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2586. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2587. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2588. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2589. }
  2590. }
  2591. }//fi gray
  2592. }
  2593. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2594. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2595. }
  2596. } else {
  2597. /* dct only in intra block */
  2598. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2599. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2600. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2601. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2602. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2603. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2604. if(s->chroma_y_shift){
  2605. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2606. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2607. }else{
  2608. dct_offset >>=1;
  2609. dct_linesize >>=1;
  2610. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2611. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2612. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2613. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2614. }
  2615. }
  2616. }else{
  2617. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2618. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2619. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2620. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2621. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2622. if(s->chroma_y_shift){
  2623. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2624. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2625. }else{
  2626. dct_linesize = uvlinesize << s->interlaced_dct;
  2627. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2628. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2629. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2630. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2631. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2632. if(!s->chroma_x_shift){//Chroma444
  2633. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2634. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2635. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2636. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2637. }
  2638. }
  2639. }//gray
  2640. }
  2641. }
  2642. skip_idct:
  2643. if(!readable){
  2644. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2645. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2646. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2647. }
  2648. }
  2649. }
  2650. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2651. #if !CONFIG_SMALL
  2652. if(s->out_format == FMT_MPEG1) {
  2653. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2654. else MPV_decode_mb_internal(s, block, 0, 1);
  2655. } else
  2656. #endif
  2657. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2658. else MPV_decode_mb_internal(s, block, 0, 0);
  2659. }
  2660. /**
  2661. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2662. */
  2663. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  2664. Picture *last, int y, int h, int picture_structure,
  2665. int first_field, int draw_edges, int low_delay,
  2666. int v_edge_pos, int h_edge_pos)
  2667. {
  2668. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  2669. int hshift = desc->log2_chroma_w;
  2670. int vshift = desc->log2_chroma_h;
  2671. const int field_pic = picture_structure != PICT_FRAME;
  2672. if(field_pic){
  2673. h <<= 1;
  2674. y <<= 1;
  2675. }
  2676. if (!avctx->hwaccel &&
  2677. !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  2678. draw_edges &&
  2679. cur->reference &&
  2680. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  2681. int *linesize = cur->f.linesize;
  2682. int sides = 0, edge_h;
  2683. if (y==0) sides |= EDGE_TOP;
  2684. if (y + h >= v_edge_pos)
  2685. sides |= EDGE_BOTTOM;
  2686. edge_h= FFMIN(h, v_edge_pos - y);
  2687. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  2688. linesize[0], h_edge_pos, edge_h,
  2689. EDGE_WIDTH, EDGE_WIDTH, sides);
  2690. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  2691. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  2692. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2693. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  2694. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  2695. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2696. }
  2697. h = FFMIN(h, avctx->height - y);
  2698. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2699. if (avctx->draw_horiz_band) {
  2700. AVFrame *src;
  2701. int offset[AV_NUM_DATA_POINTERS];
  2702. int i;
  2703. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  2704. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2705. src = &cur->f;
  2706. else if (last)
  2707. src = &last->f;
  2708. else
  2709. return;
  2710. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  2711. picture_structure == PICT_FRAME &&
  2712. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  2713. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2714. offset[i] = 0;
  2715. }else{
  2716. offset[0]= y * src->linesize[0];
  2717. offset[1]=
  2718. offset[2]= (y >> vshift) * src->linesize[1];
  2719. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2720. offset[i] = 0;
  2721. }
  2722. emms_c();
  2723. avctx->draw_horiz_band(avctx, src, offset,
  2724. y, picture_structure, h);
  2725. }
  2726. }
  2727. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2728. {
  2729. int draw_edges = s->unrestricted_mv && !s->intra_only;
  2730. ff_draw_horiz_band(s->avctx, &s->dsp, s->current_picture_ptr,
  2731. s->last_picture_ptr, y, h, s->picture_structure,
  2732. s->first_field, draw_edges, s->low_delay,
  2733. s->v_edge_pos, s->h_edge_pos);
  2734. }
  2735. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2736. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2737. const int uvlinesize = s->current_picture.f.linesize[1];
  2738. const int mb_size= 4 - s->avctx->lowres;
  2739. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2740. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2741. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2742. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2743. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2744. 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;
  2745. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2746. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2747. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2748. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2749. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2750. {
  2751. if(s->picture_structure==PICT_FRAME){
  2752. s->dest[0] += s->mb_y * linesize << mb_size;
  2753. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2754. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2755. }else{
  2756. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2757. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2758. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2759. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2760. }
  2761. }
  2762. }
  2763. /**
  2764. * Permute an 8x8 block.
  2765. * @param block the block which will be permuted according to the given permutation vector
  2766. * @param permutation the permutation vector
  2767. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2768. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2769. * (inverse) permutated to scantable order!
  2770. */
  2771. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2772. {
  2773. int i;
  2774. int16_t temp[64];
  2775. if(last<=0) return;
  2776. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2777. for(i=0; i<=last; i++){
  2778. const int j= scantable[i];
  2779. temp[j]= block[j];
  2780. block[j]=0;
  2781. }
  2782. for(i=0; i<=last; i++){
  2783. const int j= scantable[i];
  2784. const int perm_j= permutation[j];
  2785. block[perm_j]= temp[j];
  2786. }
  2787. }
  2788. void ff_mpeg_flush(AVCodecContext *avctx){
  2789. int i;
  2790. MpegEncContext *s = avctx->priv_data;
  2791. if(s==NULL || s->picture==NULL)
  2792. return;
  2793. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2794. ff_mpeg_unref_picture(s, &s->picture[i]);
  2795. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2796. ff_mpeg_unref_picture(s, &s->current_picture);
  2797. ff_mpeg_unref_picture(s, &s->last_picture);
  2798. ff_mpeg_unref_picture(s, &s->next_picture);
  2799. s->mb_x= s->mb_y= 0;
  2800. s->closed_gop= 0;
  2801. s->parse_context.state= -1;
  2802. s->parse_context.frame_start_found= 0;
  2803. s->parse_context.overread= 0;
  2804. s->parse_context.overread_index= 0;
  2805. s->parse_context.index= 0;
  2806. s->parse_context.last_index= 0;
  2807. s->bitstream_buffer_size=0;
  2808. s->pp_time=0;
  2809. }
  2810. /**
  2811. * set qscale and update qscale dependent variables.
  2812. */
  2813. void ff_set_qscale(MpegEncContext * s, int qscale)
  2814. {
  2815. if (qscale < 1)
  2816. qscale = 1;
  2817. else if (qscale > 31)
  2818. qscale = 31;
  2819. s->qscale = qscale;
  2820. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2821. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2822. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2823. }
  2824. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2825. {
  2826. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2827. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2828. }
  2829. #if CONFIG_ERROR_RESILIENCE
  2830. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2831. {
  2832. ERContext *er = &s->er;
  2833. er->cur_pic = s->current_picture_ptr;
  2834. er->last_pic = s->last_picture_ptr;
  2835. er->next_pic = s->next_picture_ptr;
  2836. er->pp_time = s->pp_time;
  2837. er->pb_time = s->pb_time;
  2838. er->quarter_sample = s->quarter_sample;
  2839. er->partitioned_frame = s->partitioned_frame;
  2840. ff_er_frame_start(er);
  2841. }
  2842. #endif /* CONFIG_ERROR_RESILIENCE */