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.

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