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.

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