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.

3296 lines
119KB

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