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.

3284 lines
119KB

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