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.

3336 lines
121KB

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