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.

3334 lines
121KB

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