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.

3262 lines
118KB

  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. /**
  1229. * generic function called after decoding
  1230. * the header and before a frame is decoded.
  1231. */
  1232. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1233. {
  1234. int i, ret;
  1235. Picture *pic;
  1236. s->mb_skipped = 0;
  1237. if (!ff_thread_can_start_frame(avctx)) {
  1238. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1239. return -1;
  1240. }
  1241. /* mark & release old frames */
  1242. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1243. s->last_picture_ptr != s->next_picture_ptr &&
  1244. s->last_picture_ptr->f.buf[0]) {
  1245. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1246. }
  1247. /* release forgotten pictures */
  1248. /* if (mpeg124/h263) */
  1249. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1250. if (&s->picture[i] != s->last_picture_ptr &&
  1251. &s->picture[i] != s->next_picture_ptr &&
  1252. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1253. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1254. av_log(avctx, AV_LOG_ERROR,
  1255. "releasing zombie picture\n");
  1256. ff_mpeg_unref_picture(s, &s->picture[i]);
  1257. }
  1258. }
  1259. ff_mpeg_unref_picture(s, &s->current_picture);
  1260. release_unused_pictures(s);
  1261. if (s->current_picture_ptr &&
  1262. s->current_picture_ptr->f.buf[0] == NULL) {
  1263. // we already have a unused image
  1264. // (maybe it was set before reading the header)
  1265. pic = s->current_picture_ptr;
  1266. } else {
  1267. i = ff_find_unused_picture(s, 0);
  1268. if (i < 0) {
  1269. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1270. return i;
  1271. }
  1272. pic = &s->picture[i];
  1273. }
  1274. pic->reference = 0;
  1275. if (!s->droppable) {
  1276. if (s->pict_type != AV_PICTURE_TYPE_B)
  1277. pic->reference = 3;
  1278. }
  1279. pic->f.coded_picture_number = s->coded_picture_number++;
  1280. if (ff_alloc_picture(s, pic, 0) < 0)
  1281. return -1;
  1282. s->current_picture_ptr = pic;
  1283. // FIXME use only the vars from current_pic
  1284. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1285. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1286. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1287. if (s->picture_structure != PICT_FRAME)
  1288. s->current_picture_ptr->f.top_field_first =
  1289. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1290. }
  1291. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1292. !s->progressive_sequence;
  1293. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1294. s->current_picture_ptr->f.pict_type = s->pict_type;
  1295. // if (s->flags && CODEC_FLAG_QSCALE)
  1296. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1297. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1298. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1299. s->current_picture_ptr)) < 0)
  1300. return ret;
  1301. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1302. s->last_picture_ptr = s->next_picture_ptr;
  1303. if (!s->droppable)
  1304. s->next_picture_ptr = s->current_picture_ptr;
  1305. }
  1306. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1307. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1308. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1309. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1310. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1311. s->pict_type, s->droppable);
  1312. if ((s->last_picture_ptr == NULL ||
  1313. s->last_picture_ptr->f.buf[0] == NULL) &&
  1314. (s->pict_type != AV_PICTURE_TYPE_I ||
  1315. s->picture_structure != PICT_FRAME)) {
  1316. int h_chroma_shift, v_chroma_shift;
  1317. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1318. &h_chroma_shift, &v_chroma_shift);
  1319. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f.buf[0])
  1320. av_log(avctx, AV_LOG_DEBUG,
  1321. "allocating dummy last picture for B frame\n");
  1322. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1323. av_log(avctx, AV_LOG_ERROR,
  1324. "warning: first frame is no keyframe\n");
  1325. else if (s->picture_structure != PICT_FRAME)
  1326. av_log(avctx, AV_LOG_DEBUG,
  1327. "allocate dummy last picture for field based first keyframe\n");
  1328. /* Allocate a dummy frame */
  1329. i = ff_find_unused_picture(s, 0);
  1330. if (i < 0) {
  1331. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1332. return i;
  1333. }
  1334. s->last_picture_ptr = &s->picture[i];
  1335. s->last_picture_ptr->reference = 3;
  1336. s->last_picture_ptr->f.key_frame = 0;
  1337. s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1338. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1339. s->last_picture_ptr = NULL;
  1340. return -1;
  1341. }
  1342. memset(s->last_picture_ptr->f.data[0], 0x80,
  1343. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1344. memset(s->last_picture_ptr->f.data[1], 0x80,
  1345. (avctx->height >> v_chroma_shift) *
  1346. s->last_picture_ptr->f.linesize[1]);
  1347. memset(s->last_picture_ptr->f.data[2], 0x80,
  1348. (avctx->height >> v_chroma_shift) *
  1349. s->last_picture_ptr->f.linesize[2]);
  1350. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1351. for(i=0; i<avctx->height; i++)
  1352. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1353. }
  1354. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1355. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1356. }
  1357. if ((s->next_picture_ptr == NULL ||
  1358. s->next_picture_ptr->f.buf[0] == NULL) &&
  1359. s->pict_type == AV_PICTURE_TYPE_B) {
  1360. /* Allocate a dummy frame */
  1361. i = ff_find_unused_picture(s, 0);
  1362. if (i < 0) {
  1363. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1364. return i;
  1365. }
  1366. s->next_picture_ptr = &s->picture[i];
  1367. s->next_picture_ptr->reference = 3;
  1368. s->next_picture_ptr->f.key_frame = 0;
  1369. s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1370. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1371. s->next_picture_ptr = NULL;
  1372. return -1;
  1373. }
  1374. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1375. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1376. }
  1377. #if 0 // BUFREF-FIXME
  1378. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1379. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1380. #endif
  1381. if (s->last_picture_ptr) {
  1382. ff_mpeg_unref_picture(s, &s->last_picture);
  1383. if (s->last_picture_ptr->f.buf[0] &&
  1384. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1385. s->last_picture_ptr)) < 0)
  1386. return ret;
  1387. }
  1388. if (s->next_picture_ptr) {
  1389. ff_mpeg_unref_picture(s, &s->next_picture);
  1390. if (s->next_picture_ptr->f.buf[0] &&
  1391. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1392. s->next_picture_ptr)) < 0)
  1393. return ret;
  1394. }
  1395. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1396. s->last_picture_ptr->f.buf[0]));
  1397. if (s->picture_structure!= PICT_FRAME) {
  1398. int i;
  1399. for (i = 0; i < 4; i++) {
  1400. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1401. s->current_picture.f.data[i] +=
  1402. s->current_picture.f.linesize[i];
  1403. }
  1404. s->current_picture.f.linesize[i] *= 2;
  1405. s->last_picture.f.linesize[i] *= 2;
  1406. s->next_picture.f.linesize[i] *= 2;
  1407. }
  1408. }
  1409. s->err_recognition = avctx->err_recognition;
  1410. /* set dequantizer, we can't do it during init as
  1411. * it might change for mpeg4 and we can't do it in the header
  1412. * decode as init is not called for mpeg4 there yet */
  1413. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1414. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1415. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1416. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1417. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1418. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1419. } else {
  1420. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1421. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1422. }
  1423. return 0;
  1424. }
  1425. /* called after a frame has been decoded. */
  1426. void ff_MPV_frame_end(MpegEncContext *s)
  1427. {
  1428. if ((s->er.error_count || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
  1429. !s->avctx->hwaccel &&
  1430. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1431. s->unrestricted_mv &&
  1432. s->current_picture.reference &&
  1433. !s->intra_only &&
  1434. !(s->flags & CODEC_FLAG_EMU_EDGE) &&
  1435. !s->avctx->lowres
  1436. ) {
  1437. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1438. int hshift = desc->log2_chroma_w;
  1439. int vshift = desc->log2_chroma_h;
  1440. s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
  1441. s->h_edge_pos, s->v_edge_pos,
  1442. EDGE_WIDTH, EDGE_WIDTH,
  1443. EDGE_TOP | EDGE_BOTTOM);
  1444. s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
  1445. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1446. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1447. EDGE_TOP | EDGE_BOTTOM);
  1448. s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
  1449. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1450. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1451. EDGE_TOP | EDGE_BOTTOM);
  1452. }
  1453. emms_c();
  1454. if (s->current_picture.reference)
  1455. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1456. }
  1457. /**
  1458. * Draw a line from (ex, ey) -> (sx, sy).
  1459. * @param w width of the image
  1460. * @param h height of the image
  1461. * @param stride stride/linesize of the image
  1462. * @param color color of the arrow
  1463. */
  1464. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1465. int w, int h, int stride, int color)
  1466. {
  1467. int x, y, fr, f;
  1468. sx = av_clip(sx, 0, w - 1);
  1469. sy = av_clip(sy, 0, h - 1);
  1470. ex = av_clip(ex, 0, w - 1);
  1471. ey = av_clip(ey, 0, h - 1);
  1472. buf[sy * stride + sx] += color;
  1473. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1474. if (sx > ex) {
  1475. FFSWAP(int, sx, ex);
  1476. FFSWAP(int, sy, ey);
  1477. }
  1478. buf += sx + sy * stride;
  1479. ex -= sx;
  1480. f = ((ey - sy) << 16) / ex;
  1481. for (x = 0; x <= ex; x++) {
  1482. y = (x * f) >> 16;
  1483. fr = (x * f) & 0xFFFF;
  1484. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1485. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1486. }
  1487. } else {
  1488. if (sy > ey) {
  1489. FFSWAP(int, sx, ex);
  1490. FFSWAP(int, sy, ey);
  1491. }
  1492. buf += sx + sy * stride;
  1493. ey -= sy;
  1494. if (ey)
  1495. f = ((ex - sx) << 16) / ey;
  1496. else
  1497. f = 0;
  1498. for(y= 0; y <= ey; y++){
  1499. x = (y*f) >> 16;
  1500. fr = (y*f) & 0xFFFF;
  1501. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1502. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1503. }
  1504. }
  1505. }
  1506. /**
  1507. * Draw an arrow from (ex, ey) -> (sx, sy).
  1508. * @param w width of the image
  1509. * @param h height of the image
  1510. * @param stride stride/linesize of the image
  1511. * @param color color of the arrow
  1512. */
  1513. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1514. int ey, int w, int h, int stride, int color)
  1515. {
  1516. int dx,dy;
  1517. sx = av_clip(sx, -100, w + 100);
  1518. sy = av_clip(sy, -100, h + 100);
  1519. ex = av_clip(ex, -100, w + 100);
  1520. ey = av_clip(ey, -100, h + 100);
  1521. dx = ex - sx;
  1522. dy = ey - sy;
  1523. if (dx * dx + dy * dy > 3 * 3) {
  1524. int rx = dx + dy;
  1525. int ry = -dx + dy;
  1526. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1527. // FIXME subpixel accuracy
  1528. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1529. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1530. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1531. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1532. }
  1533. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1534. }
  1535. /**
  1536. * Print debugging info for the given picture.
  1537. */
  1538. void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
  1539. int *low_delay,
  1540. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1541. {
  1542. if (avctx->hwaccel || !p || !p->mb_type
  1543. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1544. return;
  1545. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1546. int x,y;
  1547. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1548. av_get_picture_type_char(pict->pict_type));
  1549. for (y = 0; y < mb_height; y++) {
  1550. for (x = 0; x < mb_width; x++) {
  1551. if (avctx->debug & FF_DEBUG_SKIP) {
  1552. int count = mbskip_table[x + y * mb_stride];
  1553. if (count > 9)
  1554. count = 9;
  1555. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1556. }
  1557. if (avctx->debug & FF_DEBUG_QP) {
  1558. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1559. p->qscale_table[x + y * mb_stride]);
  1560. }
  1561. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1562. int mb_type = p->mb_type[x + y * mb_stride];
  1563. // Type & MV direction
  1564. if (IS_PCM(mb_type))
  1565. av_log(avctx, AV_LOG_DEBUG, "P");
  1566. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1567. av_log(avctx, AV_LOG_DEBUG, "A");
  1568. else if (IS_INTRA4x4(mb_type))
  1569. av_log(avctx, AV_LOG_DEBUG, "i");
  1570. else if (IS_INTRA16x16(mb_type))
  1571. av_log(avctx, AV_LOG_DEBUG, "I");
  1572. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1573. av_log(avctx, AV_LOG_DEBUG, "d");
  1574. else if (IS_DIRECT(mb_type))
  1575. av_log(avctx, AV_LOG_DEBUG, "D");
  1576. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1577. av_log(avctx, AV_LOG_DEBUG, "g");
  1578. else if (IS_GMC(mb_type))
  1579. av_log(avctx, AV_LOG_DEBUG, "G");
  1580. else if (IS_SKIP(mb_type))
  1581. av_log(avctx, AV_LOG_DEBUG, "S");
  1582. else if (!USES_LIST(mb_type, 1))
  1583. av_log(avctx, AV_LOG_DEBUG, ">");
  1584. else if (!USES_LIST(mb_type, 0))
  1585. av_log(avctx, AV_LOG_DEBUG, "<");
  1586. else {
  1587. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1588. av_log(avctx, AV_LOG_DEBUG, "X");
  1589. }
  1590. // segmentation
  1591. if (IS_8X8(mb_type))
  1592. av_log(avctx, AV_LOG_DEBUG, "+");
  1593. else if (IS_16X8(mb_type))
  1594. av_log(avctx, AV_LOG_DEBUG, "-");
  1595. else if (IS_8X16(mb_type))
  1596. av_log(avctx, AV_LOG_DEBUG, "|");
  1597. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1598. av_log(avctx, AV_LOG_DEBUG, " ");
  1599. else
  1600. av_log(avctx, AV_LOG_DEBUG, "?");
  1601. if (IS_INTERLACED(mb_type))
  1602. av_log(avctx, AV_LOG_DEBUG, "=");
  1603. else
  1604. av_log(avctx, AV_LOG_DEBUG, " ");
  1605. }
  1606. }
  1607. av_log(avctx, AV_LOG_DEBUG, "\n");
  1608. }
  1609. }
  1610. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1611. (avctx->debug_mv)) {
  1612. const int shift = 1 + quarter_sample;
  1613. int mb_y;
  1614. uint8_t *ptr;
  1615. int i;
  1616. int h_chroma_shift, v_chroma_shift, block_height;
  1617. const int width = avctx->width;
  1618. const int height = avctx->height;
  1619. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1620. const int mv_stride = (mb_width << mv_sample_log2) +
  1621. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1622. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1623. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1624. av_frame_make_writable(pict);
  1625. pict->opaque = NULL;
  1626. ptr = pict->data[0];
  1627. block_height = 16 >> v_chroma_shift;
  1628. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1629. int mb_x;
  1630. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1631. const int mb_index = mb_x + mb_y * mb_stride;
  1632. if ((avctx->debug_mv) && p->motion_val[0]) {
  1633. int type;
  1634. for (type = 0; type < 3; type++) {
  1635. int direction = 0;
  1636. switch (type) {
  1637. case 0:
  1638. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1639. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1640. continue;
  1641. direction = 0;
  1642. break;
  1643. case 1:
  1644. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1645. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1646. continue;
  1647. direction = 0;
  1648. break;
  1649. case 2:
  1650. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1651. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1652. continue;
  1653. direction = 1;
  1654. break;
  1655. }
  1656. if (!USES_LIST(p->mb_type[mb_index], direction))
  1657. continue;
  1658. if (IS_8X8(p->mb_type[mb_index])) {
  1659. int i;
  1660. for (i = 0; i < 4; i++) {
  1661. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1662. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1663. int xy = (mb_x * 2 + (i & 1) +
  1664. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1665. int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
  1666. int my = (p->motion_val[direction][xy][1] >> shift) + sy;
  1667. draw_arrow(ptr, sx, sy, mx, my, width,
  1668. height, pict->linesize[0], 100);
  1669. }
  1670. } else if (IS_16X8(p->mb_type[mb_index])) {
  1671. int i;
  1672. for (i = 0; i < 2; i++) {
  1673. int sx = mb_x * 16 + 8;
  1674. int sy = mb_y * 16 + 4 + 8 * i;
  1675. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1676. int mx = (p->motion_val[direction][xy][0] >> shift);
  1677. int my = (p->motion_val[direction][xy][1] >> shift);
  1678. if (IS_INTERLACED(p->mb_type[mb_index]))
  1679. my *= 2;
  1680. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1681. height, pict->linesize[0], 100);
  1682. }
  1683. } else if (IS_8X16(p->mb_type[mb_index])) {
  1684. int i;
  1685. for (i = 0; i < 2; i++) {
  1686. int sx = mb_x * 16 + 4 + 8 * i;
  1687. int sy = mb_y * 16 + 8;
  1688. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1689. int mx = p->motion_val[direction][xy][0] >> shift;
  1690. int my = p->motion_val[direction][xy][1] >> shift;
  1691. if (IS_INTERLACED(p->mb_type[mb_index]))
  1692. my *= 2;
  1693. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1694. height, pict->linesize[0], 100);
  1695. }
  1696. } else {
  1697. int sx= mb_x * 16 + 8;
  1698. int sy= mb_y * 16 + 8;
  1699. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1700. int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
  1701. int my= (p->motion_val[direction][xy][1]>>shift) + sy;
  1702. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
  1703. }
  1704. }
  1705. }
  1706. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1707. uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
  1708. 0x0101010101010101ULL;
  1709. int y;
  1710. for (y = 0; y < block_height; y++) {
  1711. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1712. (block_height * mb_y + y) *
  1713. pict->linesize[1]) = c;
  1714. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1715. (block_height * mb_y + y) *
  1716. pict->linesize[2]) = c;
  1717. }
  1718. }
  1719. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1720. p->motion_val[0]) {
  1721. int mb_type = p->mb_type[mb_index];
  1722. uint64_t u,v;
  1723. int y;
  1724. #define COLOR(theta, r) \
  1725. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1726. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1727. u = v = 128;
  1728. if (IS_PCM(mb_type)) {
  1729. COLOR(120, 48)
  1730. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1731. IS_INTRA16x16(mb_type)) {
  1732. COLOR(30, 48)
  1733. } else if (IS_INTRA4x4(mb_type)) {
  1734. COLOR(90, 48)
  1735. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1736. // COLOR(120, 48)
  1737. } else if (IS_DIRECT(mb_type)) {
  1738. COLOR(150, 48)
  1739. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1740. COLOR(170, 48)
  1741. } else if (IS_GMC(mb_type)) {
  1742. COLOR(190, 48)
  1743. } else if (IS_SKIP(mb_type)) {
  1744. // COLOR(180, 48)
  1745. } else if (!USES_LIST(mb_type, 1)) {
  1746. COLOR(240, 48)
  1747. } else if (!USES_LIST(mb_type, 0)) {
  1748. COLOR(0, 48)
  1749. } else {
  1750. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1751. COLOR(300,48)
  1752. }
  1753. u *= 0x0101010101010101ULL;
  1754. v *= 0x0101010101010101ULL;
  1755. for (y = 0; y < block_height; y++) {
  1756. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1757. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1758. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1759. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1760. }
  1761. // segmentation
  1762. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1763. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1764. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1765. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1766. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1767. }
  1768. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1769. for (y = 0; y < 16; y++)
  1770. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1771. pict->linesize[0]] ^= 0x80;
  1772. }
  1773. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1774. int dm = 1 << (mv_sample_log2 - 2);
  1775. for (i = 0; i < 4; i++) {
  1776. int sx = mb_x * 16 + 8 * (i & 1);
  1777. int sy = mb_y * 16 + 8 * (i >> 1);
  1778. int xy = (mb_x * 2 + (i & 1) +
  1779. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1780. // FIXME bidir
  1781. int32_t *mv = (int32_t *) &p->motion_val[0][xy];
  1782. if (mv[0] != mv[dm] ||
  1783. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1784. for (y = 0; y < 8; y++)
  1785. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1786. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1787. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1788. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1789. }
  1790. }
  1791. if (IS_INTERLACED(mb_type) &&
  1792. avctx->codec->id == AV_CODEC_ID_H264) {
  1793. // hmm
  1794. }
  1795. }
  1796. mbskip_table[mb_index] = 0;
  1797. }
  1798. }
  1799. }
  1800. }
  1801. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1802. {
  1803. ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
  1804. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1805. }
  1806. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1807. {
  1808. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1809. int offset = 2*s->mb_stride + 1;
  1810. if(!ref)
  1811. return AVERROR(ENOMEM);
  1812. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1813. ref->size -= offset;
  1814. ref->data += offset;
  1815. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1816. }
  1817. static inline int hpel_motion_lowres(MpegEncContext *s,
  1818. uint8_t *dest, uint8_t *src,
  1819. int field_based, int field_select,
  1820. int src_x, int src_y,
  1821. int width, int height, ptrdiff_t stride,
  1822. int h_edge_pos, int v_edge_pos,
  1823. int w, int h, h264_chroma_mc_func *pix_op,
  1824. int motion_x, int motion_y)
  1825. {
  1826. const int lowres = s->avctx->lowres;
  1827. const int op_index = FFMIN(lowres, 3);
  1828. const int s_mask = (2 << lowres) - 1;
  1829. int emu = 0;
  1830. int sx, sy;
  1831. if (s->quarter_sample) {
  1832. motion_x /= 2;
  1833. motion_y /= 2;
  1834. }
  1835. sx = motion_x & s_mask;
  1836. sy = motion_y & s_mask;
  1837. src_x += motion_x >> lowres + 1;
  1838. src_y += motion_y >> lowres + 1;
  1839. src += src_y * stride + src_x;
  1840. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1841. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1842. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  1843. s->linesize, s->linesize,
  1844. w + 1, (h + 1) << field_based,
  1845. src_x, src_y << field_based,
  1846. h_edge_pos, v_edge_pos);
  1847. src = s->edge_emu_buffer;
  1848. emu = 1;
  1849. }
  1850. sx = (sx << 2) >> lowres;
  1851. sy = (sy << 2) >> lowres;
  1852. if (field_select)
  1853. src += s->linesize;
  1854. pix_op[op_index](dest, src, stride, h, sx, sy);
  1855. return emu;
  1856. }
  1857. /* apply one mpeg motion vector to the three components */
  1858. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1859. uint8_t *dest_y,
  1860. uint8_t *dest_cb,
  1861. uint8_t *dest_cr,
  1862. int field_based,
  1863. int bottom_field,
  1864. int field_select,
  1865. uint8_t **ref_picture,
  1866. h264_chroma_mc_func *pix_op,
  1867. int motion_x, int motion_y,
  1868. int h, int mb_y)
  1869. {
  1870. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1871. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  1872. ptrdiff_t uvlinesize, linesize;
  1873. const int lowres = s->avctx->lowres;
  1874. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  1875. const int block_s = 8>>lowres;
  1876. const int s_mask = (2 << lowres) - 1;
  1877. const int h_edge_pos = s->h_edge_pos >> lowres;
  1878. const int v_edge_pos = s->v_edge_pos >> lowres;
  1879. linesize = s->current_picture.f.linesize[0] << field_based;
  1880. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1881. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1882. if (s->quarter_sample) {
  1883. motion_x /= 2;
  1884. motion_y /= 2;
  1885. }
  1886. if(field_based){
  1887. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  1888. }
  1889. sx = motion_x & s_mask;
  1890. sy = motion_y & s_mask;
  1891. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  1892. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  1893. if (s->out_format == FMT_H263) {
  1894. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  1895. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  1896. uvsrc_x = src_x >> 1;
  1897. uvsrc_y = src_y >> 1;
  1898. } else if (s->out_format == FMT_H261) {
  1899. // even chroma mv's are full pel in H261
  1900. mx = motion_x / 4;
  1901. my = motion_y / 4;
  1902. uvsx = (2 * mx) & s_mask;
  1903. uvsy = (2 * my) & s_mask;
  1904. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  1905. uvsrc_y = mb_y * block_s + (my >> lowres);
  1906. } else {
  1907. if(s->chroma_y_shift){
  1908. mx = motion_x / 2;
  1909. my = motion_y / 2;
  1910. uvsx = mx & s_mask;
  1911. uvsy = my & s_mask;
  1912. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  1913. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  1914. } else {
  1915. if(s->chroma_x_shift){
  1916. //Chroma422
  1917. mx = motion_x / 2;
  1918. uvsx = mx & s_mask;
  1919. uvsy = motion_y & s_mask;
  1920. uvsrc_y = src_y;
  1921. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1922. } else {
  1923. //Chroma444
  1924. uvsx = motion_x & s_mask;
  1925. uvsy = motion_y & s_mask;
  1926. uvsrc_x = src_x;
  1927. uvsrc_y = src_y;
  1928. }
  1929. }
  1930. }
  1931. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1932. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1933. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1934. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  1935. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1936. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  1937. linesize >> field_based, linesize >> field_based,
  1938. 17, 17 + field_based,
  1939. src_x, src_y << field_based, h_edge_pos,
  1940. v_edge_pos);
  1941. ptr_y = s->edge_emu_buffer;
  1942. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1943. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  1944. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  1945. uvlinesize >> field_based, uvlinesize >> field_based,
  1946. 9, 9 + field_based,
  1947. uvsrc_x, uvsrc_y << field_based,
  1948. h_edge_pos >> 1, v_edge_pos >> 1);
  1949. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  1950. uvlinesize >> field_based,uvlinesize >> field_based,
  1951. 9, 9 + field_based,
  1952. uvsrc_x, uvsrc_y << field_based,
  1953. h_edge_pos >> 1, v_edge_pos >> 1);
  1954. ptr_cb = uvbuf;
  1955. ptr_cr = uvbuf + 16;
  1956. }
  1957. }
  1958. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  1959. if (bottom_field) {
  1960. dest_y += s->linesize;
  1961. dest_cb += s->uvlinesize;
  1962. dest_cr += s->uvlinesize;
  1963. }
  1964. if (field_select) {
  1965. ptr_y += s->linesize;
  1966. ptr_cb += s->uvlinesize;
  1967. ptr_cr += s->uvlinesize;
  1968. }
  1969. sx = (sx << 2) >> lowres;
  1970. sy = (sy << 2) >> lowres;
  1971. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  1972. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1973. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  1974. uvsx = (uvsx << 2) >> lowres;
  1975. uvsy = (uvsy << 2) >> lowres;
  1976. if (hc) {
  1977. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  1978. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  1979. }
  1980. }
  1981. // FIXME h261 lowres loop filter
  1982. }
  1983. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1984. uint8_t *dest_cb, uint8_t *dest_cr,
  1985. uint8_t **ref_picture,
  1986. h264_chroma_mc_func * pix_op,
  1987. int mx, int my)
  1988. {
  1989. const int lowres = s->avctx->lowres;
  1990. const int op_index = FFMIN(lowres, 3);
  1991. const int block_s = 8 >> lowres;
  1992. const int s_mask = (2 << lowres) - 1;
  1993. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  1994. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  1995. int emu = 0, src_x, src_y, sx, sy;
  1996. ptrdiff_t offset;
  1997. uint8_t *ptr;
  1998. if (s->quarter_sample) {
  1999. mx /= 2;
  2000. my /= 2;
  2001. }
  2002. /* In case of 8X8, we construct a single chroma motion vector
  2003. with a special rounding */
  2004. mx = ff_h263_round_chroma(mx);
  2005. my = ff_h263_round_chroma(my);
  2006. sx = mx & s_mask;
  2007. sy = my & s_mask;
  2008. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2009. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2010. offset = src_y * s->uvlinesize + src_x;
  2011. ptr = ref_picture[1] + offset;
  2012. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2013. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2014. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2015. s->uvlinesize, s->uvlinesize,
  2016. 9, 9,
  2017. src_x, src_y, h_edge_pos, v_edge_pos);
  2018. ptr = s->edge_emu_buffer;
  2019. emu = 1;
  2020. }
  2021. sx = (sx << 2) >> lowres;
  2022. sy = (sy << 2) >> lowres;
  2023. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2024. ptr = ref_picture[2] + offset;
  2025. if (emu) {
  2026. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2027. s->uvlinesize, s->uvlinesize,
  2028. 9, 9,
  2029. src_x, src_y, h_edge_pos, v_edge_pos);
  2030. ptr = s->edge_emu_buffer;
  2031. }
  2032. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2033. }
  2034. /**
  2035. * motion compensation of a single macroblock
  2036. * @param s context
  2037. * @param dest_y luma destination pointer
  2038. * @param dest_cb chroma cb/u destination pointer
  2039. * @param dest_cr chroma cr/v destination pointer
  2040. * @param dir direction (0->forward, 1->backward)
  2041. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2042. * @param pix_op halfpel motion compensation function (average or put normally)
  2043. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2044. */
  2045. static inline void MPV_motion_lowres(MpegEncContext *s,
  2046. uint8_t *dest_y, uint8_t *dest_cb,
  2047. uint8_t *dest_cr,
  2048. int dir, uint8_t **ref_picture,
  2049. h264_chroma_mc_func *pix_op)
  2050. {
  2051. int mx, my;
  2052. int mb_x, mb_y, i;
  2053. const int lowres = s->avctx->lowres;
  2054. const int block_s = 8 >>lowres;
  2055. mb_x = s->mb_x;
  2056. mb_y = s->mb_y;
  2057. switch (s->mv_type) {
  2058. case MV_TYPE_16X16:
  2059. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2060. 0, 0, 0,
  2061. ref_picture, pix_op,
  2062. s->mv[dir][0][0], s->mv[dir][0][1],
  2063. 2 * block_s, mb_y);
  2064. break;
  2065. case MV_TYPE_8X8:
  2066. mx = 0;
  2067. my = 0;
  2068. for (i = 0; i < 4; i++) {
  2069. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2070. s->linesize) * block_s,
  2071. ref_picture[0], 0, 0,
  2072. (2 * mb_x + (i & 1)) * block_s,
  2073. (2 * mb_y + (i >> 1)) * block_s,
  2074. s->width, s->height, s->linesize,
  2075. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2076. block_s, block_s, pix_op,
  2077. s->mv[dir][i][0], s->mv[dir][i][1]);
  2078. mx += s->mv[dir][i][0];
  2079. my += s->mv[dir][i][1];
  2080. }
  2081. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2082. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2083. pix_op, mx, my);
  2084. break;
  2085. case MV_TYPE_FIELD:
  2086. if (s->picture_structure == PICT_FRAME) {
  2087. /* top field */
  2088. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2089. 1, 0, s->field_select[dir][0],
  2090. ref_picture, pix_op,
  2091. s->mv[dir][0][0], s->mv[dir][0][1],
  2092. block_s, mb_y);
  2093. /* bottom field */
  2094. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2095. 1, 1, s->field_select[dir][1],
  2096. ref_picture, pix_op,
  2097. s->mv[dir][1][0], s->mv[dir][1][1],
  2098. block_s, mb_y);
  2099. } else {
  2100. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2101. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2102. ref_picture = s->current_picture_ptr->f.data;
  2103. }
  2104. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2105. 0, 0, s->field_select[dir][0],
  2106. ref_picture, pix_op,
  2107. s->mv[dir][0][0],
  2108. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2109. }
  2110. break;
  2111. case MV_TYPE_16X8:
  2112. for (i = 0; i < 2; i++) {
  2113. uint8_t **ref2picture;
  2114. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2115. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2116. ref2picture = ref_picture;
  2117. } else {
  2118. ref2picture = s->current_picture_ptr->f.data;
  2119. }
  2120. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2121. 0, 0, s->field_select[dir][i],
  2122. ref2picture, pix_op,
  2123. s->mv[dir][i][0], s->mv[dir][i][1] +
  2124. 2 * block_s * i, block_s, mb_y >> 1);
  2125. dest_y += 2 * block_s * s->linesize;
  2126. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2127. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2128. }
  2129. break;
  2130. case MV_TYPE_DMV:
  2131. if (s->picture_structure == PICT_FRAME) {
  2132. for (i = 0; i < 2; i++) {
  2133. int j;
  2134. for (j = 0; j < 2; j++) {
  2135. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2136. 1, j, j ^ i,
  2137. ref_picture, pix_op,
  2138. s->mv[dir][2 * i + j][0],
  2139. s->mv[dir][2 * i + j][1],
  2140. block_s, mb_y);
  2141. }
  2142. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2143. }
  2144. } else {
  2145. for (i = 0; i < 2; i++) {
  2146. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2147. 0, 0, s->picture_structure != i + 1,
  2148. ref_picture, pix_op,
  2149. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2150. 2 * block_s, mb_y >> 1);
  2151. // after put we make avg of the same block
  2152. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2153. // opposite parity is always in the same
  2154. // frame if this is second field
  2155. if (!s->first_field) {
  2156. ref_picture = s->current_picture_ptr->f.data;
  2157. }
  2158. }
  2159. }
  2160. break;
  2161. default:
  2162. av_assert2(0);
  2163. }
  2164. }
  2165. /**
  2166. * find the lowest MB row referenced in the MVs
  2167. */
  2168. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2169. {
  2170. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2171. int my, off, i, mvs;
  2172. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2173. goto unhandled;
  2174. switch (s->mv_type) {
  2175. case MV_TYPE_16X16:
  2176. mvs = 1;
  2177. break;
  2178. case MV_TYPE_16X8:
  2179. mvs = 2;
  2180. break;
  2181. case MV_TYPE_8X8:
  2182. mvs = 4;
  2183. break;
  2184. default:
  2185. goto unhandled;
  2186. }
  2187. for (i = 0; i < mvs; i++) {
  2188. my = s->mv[dir][i][1]<<qpel_shift;
  2189. my_max = FFMAX(my_max, my);
  2190. my_min = FFMIN(my_min, my);
  2191. }
  2192. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2193. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2194. unhandled:
  2195. return s->mb_height-1;
  2196. }
  2197. /* put block[] to dest[] */
  2198. static inline void put_dct(MpegEncContext *s,
  2199. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2200. {
  2201. s->dct_unquantize_intra(s, block, i, qscale);
  2202. s->dsp.idct_put (dest, line_size, block);
  2203. }
  2204. /* add block[] to dest[] */
  2205. static inline void add_dct(MpegEncContext *s,
  2206. int16_t *block, int i, uint8_t *dest, int line_size)
  2207. {
  2208. if (s->block_last_index[i] >= 0) {
  2209. s->dsp.idct_add (dest, line_size, block);
  2210. }
  2211. }
  2212. static inline void add_dequant_dct(MpegEncContext *s,
  2213. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2214. {
  2215. if (s->block_last_index[i] >= 0) {
  2216. s->dct_unquantize_inter(s, block, i, qscale);
  2217. s->dsp.idct_add (dest, line_size, block);
  2218. }
  2219. }
  2220. /**
  2221. * Clean dc, ac, coded_block for the current non-intra MB.
  2222. */
  2223. void ff_clean_intra_table_entries(MpegEncContext *s)
  2224. {
  2225. int wrap = s->b8_stride;
  2226. int xy = s->block_index[0];
  2227. s->dc_val[0][xy ] =
  2228. s->dc_val[0][xy + 1 ] =
  2229. s->dc_val[0][xy + wrap] =
  2230. s->dc_val[0][xy + 1 + wrap] = 1024;
  2231. /* ac pred */
  2232. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2233. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2234. if (s->msmpeg4_version>=3) {
  2235. s->coded_block[xy ] =
  2236. s->coded_block[xy + 1 ] =
  2237. s->coded_block[xy + wrap] =
  2238. s->coded_block[xy + 1 + wrap] = 0;
  2239. }
  2240. /* chroma */
  2241. wrap = s->mb_stride;
  2242. xy = s->mb_x + s->mb_y * wrap;
  2243. s->dc_val[1][xy] =
  2244. s->dc_val[2][xy] = 1024;
  2245. /* ac pred */
  2246. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2247. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2248. s->mbintra_table[xy]= 0;
  2249. }
  2250. /* generic function called after a macroblock has been parsed by the
  2251. decoder or after it has been encoded by the encoder.
  2252. Important variables used:
  2253. s->mb_intra : true if intra macroblock
  2254. s->mv_dir : motion vector direction
  2255. s->mv_type : motion vector type
  2256. s->mv : motion vector
  2257. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2258. */
  2259. static av_always_inline
  2260. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2261. int lowres_flag, int is_mpeg12)
  2262. {
  2263. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2264. if (CONFIG_XVMC &&
  2265. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2266. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2267. return;
  2268. }
  2269. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2270. /* print DCT coefficients */
  2271. int i,j;
  2272. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2273. for(i=0; i<6; i++){
  2274. for(j=0; j<64; j++){
  2275. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  2276. }
  2277. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2278. }
  2279. }
  2280. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2281. /* update DC predictors for P macroblocks */
  2282. if (!s->mb_intra) {
  2283. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2284. if(s->mbintra_table[mb_xy])
  2285. ff_clean_intra_table_entries(s);
  2286. } else {
  2287. s->last_dc[0] =
  2288. s->last_dc[1] =
  2289. s->last_dc[2] = 128 << s->intra_dc_precision;
  2290. }
  2291. }
  2292. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2293. s->mbintra_table[mb_xy]=1;
  2294. if ( (s->flags&CODEC_FLAG_PSNR)
  2295. || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
  2296. || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  2297. uint8_t *dest_y, *dest_cb, *dest_cr;
  2298. int dct_linesize, dct_offset;
  2299. op_pixels_func (*op_pix)[4];
  2300. qpel_mc_func (*op_qpix)[16];
  2301. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2302. const int uvlinesize = s->current_picture.f.linesize[1];
  2303. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2304. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2305. /* avoid copy if macroblock skipped in last frame too */
  2306. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2307. if(!s->encoding){
  2308. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2309. if (s->mb_skipped) {
  2310. s->mb_skipped= 0;
  2311. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2312. *mbskip_ptr = 1;
  2313. } else if(!s->current_picture.reference) {
  2314. *mbskip_ptr = 1;
  2315. } else{
  2316. *mbskip_ptr = 0; /* not skipped */
  2317. }
  2318. }
  2319. dct_linesize = linesize << s->interlaced_dct;
  2320. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2321. if(readable){
  2322. dest_y= s->dest[0];
  2323. dest_cb= s->dest[1];
  2324. dest_cr= s->dest[2];
  2325. }else{
  2326. dest_y = s->b_scratchpad;
  2327. dest_cb= s->b_scratchpad+16*linesize;
  2328. dest_cr= s->b_scratchpad+32*linesize;
  2329. }
  2330. if (!s->mb_intra) {
  2331. /* motion handling */
  2332. /* decoding or more than one mb_type (MC was already done otherwise) */
  2333. if(!s->encoding){
  2334. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2335. if (s->mv_dir & MV_DIR_FORWARD) {
  2336. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2337. ff_MPV_lowest_referenced_row(s, 0),
  2338. 0);
  2339. }
  2340. if (s->mv_dir & MV_DIR_BACKWARD) {
  2341. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2342. ff_MPV_lowest_referenced_row(s, 1),
  2343. 0);
  2344. }
  2345. }
  2346. if(lowres_flag){
  2347. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2348. if (s->mv_dir & MV_DIR_FORWARD) {
  2349. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2350. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2351. }
  2352. if (s->mv_dir & MV_DIR_BACKWARD) {
  2353. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2354. }
  2355. }else{
  2356. op_qpix = s->me.qpel_put;
  2357. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2358. op_pix = s->hdsp.put_pixels_tab;
  2359. }else{
  2360. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2361. }
  2362. if (s->mv_dir & MV_DIR_FORWARD) {
  2363. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2364. op_pix = s->hdsp.avg_pixels_tab;
  2365. op_qpix= s->me.qpel_avg;
  2366. }
  2367. if (s->mv_dir & MV_DIR_BACKWARD) {
  2368. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2369. }
  2370. }
  2371. }
  2372. /* skip dequant / idct if we are really late ;) */
  2373. if(s->avctx->skip_idct){
  2374. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2375. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2376. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2377. goto skip_idct;
  2378. }
  2379. /* add dct residue */
  2380. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2381. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2382. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2383. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2384. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2385. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2386. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2387. if (s->chroma_y_shift){
  2388. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2389. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2390. }else{
  2391. dct_linesize >>= 1;
  2392. dct_offset >>=1;
  2393. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2394. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2395. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2396. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2397. }
  2398. }
  2399. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2400. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2401. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2402. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2403. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2404. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2405. if(s->chroma_y_shift){//Chroma420
  2406. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2407. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2408. }else{
  2409. //chroma422
  2410. dct_linesize = uvlinesize << s->interlaced_dct;
  2411. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2412. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2413. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2414. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2415. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2416. if(!s->chroma_x_shift){//Chroma444
  2417. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2418. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2419. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2420. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2421. }
  2422. }
  2423. }//fi gray
  2424. }
  2425. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2426. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2427. }
  2428. } else {
  2429. /* dct only in intra block */
  2430. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2431. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2432. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2433. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2434. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2435. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2436. if(s->chroma_y_shift){
  2437. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2438. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2439. }else{
  2440. dct_offset >>=1;
  2441. dct_linesize >>=1;
  2442. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2443. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2444. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2445. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2446. }
  2447. }
  2448. }else{
  2449. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2450. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2451. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2452. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2453. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2454. if(s->chroma_y_shift){
  2455. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2456. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2457. }else{
  2458. dct_linesize = uvlinesize << s->interlaced_dct;
  2459. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2460. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2461. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2462. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2463. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2464. if(!s->chroma_x_shift){//Chroma444
  2465. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2466. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2467. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2468. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2469. }
  2470. }
  2471. }//gray
  2472. }
  2473. }
  2474. skip_idct:
  2475. if(!readable){
  2476. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2477. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2478. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2479. }
  2480. }
  2481. }
  2482. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2483. #if !CONFIG_SMALL
  2484. if(s->out_format == FMT_MPEG1) {
  2485. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2486. else MPV_decode_mb_internal(s, block, 0, 1);
  2487. } else
  2488. #endif
  2489. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2490. else MPV_decode_mb_internal(s, block, 0, 0);
  2491. }
  2492. /**
  2493. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2494. */
  2495. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  2496. Picture *last, int y, int h, int picture_structure,
  2497. int first_field, int draw_edges, int low_delay,
  2498. int v_edge_pos, int h_edge_pos)
  2499. {
  2500. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  2501. int hshift = desc->log2_chroma_w;
  2502. int vshift = desc->log2_chroma_h;
  2503. const int field_pic = picture_structure != PICT_FRAME;
  2504. if(field_pic){
  2505. h <<= 1;
  2506. y <<= 1;
  2507. }
  2508. if (!avctx->hwaccel &&
  2509. !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  2510. draw_edges &&
  2511. cur->reference &&
  2512. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  2513. int *linesize = cur->f.linesize;
  2514. int sides = 0, edge_h;
  2515. if (y==0) sides |= EDGE_TOP;
  2516. if (y + h >= v_edge_pos)
  2517. sides |= EDGE_BOTTOM;
  2518. edge_h= FFMIN(h, v_edge_pos - y);
  2519. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  2520. linesize[0], h_edge_pos, edge_h,
  2521. EDGE_WIDTH, EDGE_WIDTH, sides);
  2522. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  2523. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  2524. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2525. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  2526. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  2527. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2528. }
  2529. h = FFMIN(h, avctx->height - y);
  2530. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2531. if (avctx->draw_horiz_band) {
  2532. AVFrame *src;
  2533. int offset[AV_NUM_DATA_POINTERS];
  2534. int i;
  2535. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  2536. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2537. src = &cur->f;
  2538. else if (last)
  2539. src = &last->f;
  2540. else
  2541. return;
  2542. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  2543. picture_structure == PICT_FRAME &&
  2544. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  2545. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2546. offset[i] = 0;
  2547. }else{
  2548. offset[0]= y * src->linesize[0];
  2549. offset[1]=
  2550. offset[2]= (y >> vshift) * src->linesize[1];
  2551. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2552. offset[i] = 0;
  2553. }
  2554. emms_c();
  2555. avctx->draw_horiz_band(avctx, src, offset,
  2556. y, picture_structure, h);
  2557. }
  2558. }
  2559. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2560. {
  2561. int draw_edges = s->unrestricted_mv && !s->intra_only;
  2562. ff_draw_horiz_band(s->avctx, &s->dsp, s->current_picture_ptr,
  2563. s->last_picture_ptr, y, h, s->picture_structure,
  2564. s->first_field, draw_edges, s->low_delay,
  2565. s->v_edge_pos, s->h_edge_pos);
  2566. }
  2567. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2568. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2569. const int uvlinesize = s->current_picture.f.linesize[1];
  2570. const int mb_size= 4 - s->avctx->lowres;
  2571. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2572. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2573. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2574. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2575. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2576. 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;
  2577. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2578. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2579. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2580. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2581. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2582. {
  2583. if(s->picture_structure==PICT_FRAME){
  2584. s->dest[0] += s->mb_y * linesize << mb_size;
  2585. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2586. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2587. }else{
  2588. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2589. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2590. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2591. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2592. }
  2593. }
  2594. }
  2595. /**
  2596. * Permute an 8x8 block.
  2597. * @param block the block which will be permuted according to the given permutation vector
  2598. * @param permutation the permutation vector
  2599. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2600. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2601. * (inverse) permutated to scantable order!
  2602. */
  2603. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2604. {
  2605. int i;
  2606. int16_t temp[64];
  2607. if(last<=0) return;
  2608. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2609. for(i=0; i<=last; i++){
  2610. const int j= scantable[i];
  2611. temp[j]= block[j];
  2612. block[j]=0;
  2613. }
  2614. for(i=0; i<=last; i++){
  2615. const int j= scantable[i];
  2616. const int perm_j= permutation[j];
  2617. block[perm_j]= temp[j];
  2618. }
  2619. }
  2620. void ff_mpeg_flush(AVCodecContext *avctx){
  2621. int i;
  2622. MpegEncContext *s = avctx->priv_data;
  2623. if(s==NULL || s->picture==NULL)
  2624. return;
  2625. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2626. ff_mpeg_unref_picture(s, &s->picture[i]);
  2627. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2628. ff_mpeg_unref_picture(s, &s->current_picture);
  2629. ff_mpeg_unref_picture(s, &s->last_picture);
  2630. ff_mpeg_unref_picture(s, &s->next_picture);
  2631. s->mb_x= s->mb_y= 0;
  2632. s->closed_gop= 0;
  2633. s->parse_context.state= -1;
  2634. s->parse_context.frame_start_found= 0;
  2635. s->parse_context.overread= 0;
  2636. s->parse_context.overread_index= 0;
  2637. s->parse_context.index= 0;
  2638. s->parse_context.last_index= 0;
  2639. s->bitstream_buffer_size=0;
  2640. s->pp_time=0;
  2641. }
  2642. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2643. int16_t *block, int n, int qscale)
  2644. {
  2645. int i, level, nCoeffs;
  2646. const uint16_t *quant_matrix;
  2647. nCoeffs= s->block_last_index[n];
  2648. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2649. /* XXX: only mpeg1 */
  2650. quant_matrix = s->intra_matrix;
  2651. for(i=1;i<=nCoeffs;i++) {
  2652. int j= s->intra_scantable.permutated[i];
  2653. level = block[j];
  2654. if (level) {
  2655. if (level < 0) {
  2656. level = -level;
  2657. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2658. level = (level - 1) | 1;
  2659. level = -level;
  2660. } else {
  2661. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2662. level = (level - 1) | 1;
  2663. }
  2664. block[j] = level;
  2665. }
  2666. }
  2667. }
  2668. static void dct_unquantize_mpeg1_inter_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. quant_matrix = s->inter_matrix;
  2675. for(i=0; i<=nCoeffs; i++) {
  2676. int j= s->intra_scantable.permutated[i];
  2677. level = block[j];
  2678. if (level) {
  2679. if (level < 0) {
  2680. level = -level;
  2681. level = (((level << 1) + 1) * qscale *
  2682. ((int) (quant_matrix[j]))) >> 4;
  2683. level = (level - 1) | 1;
  2684. level = -level;
  2685. } else {
  2686. level = (((level << 1) + 1) * qscale *
  2687. ((int) (quant_matrix[j]))) >> 4;
  2688. level = (level - 1) | 1;
  2689. }
  2690. block[j] = level;
  2691. }
  2692. }
  2693. }
  2694. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2695. int16_t *block, int n, int qscale)
  2696. {
  2697. int i, level, nCoeffs;
  2698. const uint16_t *quant_matrix;
  2699. if(s->alternate_scan) nCoeffs= 63;
  2700. else nCoeffs= s->block_last_index[n];
  2701. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2702. quant_matrix = s->intra_matrix;
  2703. for(i=1;i<=nCoeffs;i++) {
  2704. int j= s->intra_scantable.permutated[i];
  2705. level = block[j];
  2706. if (level) {
  2707. if (level < 0) {
  2708. level = -level;
  2709. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2710. level = -level;
  2711. } else {
  2712. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2713. }
  2714. block[j] = level;
  2715. }
  2716. }
  2717. }
  2718. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2719. int16_t *block, int n, int qscale)
  2720. {
  2721. int i, level, nCoeffs;
  2722. const uint16_t *quant_matrix;
  2723. int sum=-1;
  2724. if(s->alternate_scan) nCoeffs= 63;
  2725. else nCoeffs= s->block_last_index[n];
  2726. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2727. sum += block[0];
  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. sum+=level;
  2742. }
  2743. }
  2744. block[63]^=sum&1;
  2745. }
  2746. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2747. int16_t *block, int n, int qscale)
  2748. {
  2749. int i, level, nCoeffs;
  2750. const uint16_t *quant_matrix;
  2751. int sum=-1;
  2752. if(s->alternate_scan) nCoeffs= 63;
  2753. else nCoeffs= s->block_last_index[n];
  2754. quant_matrix = s->inter_matrix;
  2755. for(i=0; 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 = (((level << 1) + 1) * qscale *
  2762. ((int) (quant_matrix[j]))) >> 4;
  2763. level = -level;
  2764. } else {
  2765. level = (((level << 1) + 1) * qscale *
  2766. ((int) (quant_matrix[j]))) >> 4;
  2767. }
  2768. block[j] = level;
  2769. sum+=level;
  2770. }
  2771. }
  2772. block[63]^=sum&1;
  2773. }
  2774. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2775. int16_t *block, int n, int qscale)
  2776. {
  2777. int i, level, qmul, qadd;
  2778. int nCoeffs;
  2779. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  2780. qmul = qscale << 1;
  2781. if (!s->h263_aic) {
  2782. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2783. qadd = (qscale - 1) | 1;
  2784. }else{
  2785. qadd = 0;
  2786. }
  2787. if(s->ac_pred)
  2788. nCoeffs=63;
  2789. else
  2790. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2791. for(i=1; i<=nCoeffs; i++) {
  2792. level = block[i];
  2793. if (level) {
  2794. if (level < 0) {
  2795. level = level * qmul - qadd;
  2796. } else {
  2797. level = level * qmul + qadd;
  2798. }
  2799. block[i] = level;
  2800. }
  2801. }
  2802. }
  2803. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2804. int16_t *block, int n, int qscale)
  2805. {
  2806. int i, level, qmul, qadd;
  2807. int nCoeffs;
  2808. av_assert2(s->block_last_index[n]>=0);
  2809. qadd = (qscale - 1) | 1;
  2810. qmul = qscale << 1;
  2811. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2812. for(i=0; i<=nCoeffs; i++) {
  2813. level = block[i];
  2814. if (level) {
  2815. if (level < 0) {
  2816. level = level * qmul - qadd;
  2817. } else {
  2818. level = level * qmul + qadd;
  2819. }
  2820. block[i] = level;
  2821. }
  2822. }
  2823. }
  2824. /**
  2825. * set qscale and update qscale dependent variables.
  2826. */
  2827. void ff_set_qscale(MpegEncContext * s, int qscale)
  2828. {
  2829. if (qscale < 1)
  2830. qscale = 1;
  2831. else if (qscale > 31)
  2832. qscale = 31;
  2833. s->qscale = qscale;
  2834. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2835. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2836. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2837. }
  2838. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2839. {
  2840. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2841. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2842. }
  2843. #if CONFIG_ERROR_RESILIENCE
  2844. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2845. {
  2846. ERContext *er = &s->er;
  2847. er->cur_pic = s->current_picture_ptr;
  2848. er->last_pic = s->last_picture_ptr;
  2849. er->next_pic = s->next_picture_ptr;
  2850. er->pp_time = s->pp_time;
  2851. er->pb_time = s->pb_time;
  2852. er->quarter_sample = s->quarter_sample;
  2853. er->partitioned_frame = s->partitioned_frame;
  2854. ff_er_frame_start(er);
  2855. }
  2856. #endif /* CONFIG_ERROR_RESILIENCE */