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.

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