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.

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