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.

3341 lines
122KB

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