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.

3356 lines
122KB

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