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.

3370 lines
123KB

  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. for (i = 0; i < 3; i++)
  1014. av_freep(&s->visualization_buffer[i]);
  1015. return 0;
  1016. }
  1017. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1018. {
  1019. int i, err = 0;
  1020. if (s->slice_context_count > 1) {
  1021. for (i = 0; i < s->slice_context_count; i++) {
  1022. free_duplicate_context(s->thread_context[i]);
  1023. }
  1024. for (i = 1; i < s->slice_context_count; i++) {
  1025. av_freep(&s->thread_context[i]);
  1026. }
  1027. } else
  1028. free_duplicate_context(s);
  1029. if ((err = free_context_frame(s)) < 0)
  1030. return err;
  1031. if (s->picture)
  1032. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1033. s->picture[i].needs_realloc = 1;
  1034. }
  1035. s->last_picture_ptr =
  1036. s->next_picture_ptr =
  1037. s->current_picture_ptr = NULL;
  1038. // init
  1039. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1040. s->mb_height = (s->height + 31) / 32 * 2;
  1041. else if (s->codec_id != AV_CODEC_ID_H264)
  1042. s->mb_height = (s->height + 15) / 16;
  1043. if ((s->width || s->height) &&
  1044. av_image_check_size(s->width, s->height, 0, s->avctx))
  1045. return AVERROR_INVALIDDATA;
  1046. if ((err = init_context_frame(s)))
  1047. goto fail;
  1048. s->thread_context[0] = s;
  1049. if (s->width && s->height) {
  1050. int nb_slices = s->slice_context_count;
  1051. if (nb_slices > 1) {
  1052. for (i = 1; i < nb_slices; i++) {
  1053. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1054. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1055. }
  1056. for (i = 0; i < nb_slices; i++) {
  1057. if (init_duplicate_context(s->thread_context[i]) < 0)
  1058. goto fail;
  1059. s->thread_context[i]->start_mb_y =
  1060. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1061. s->thread_context[i]->end_mb_y =
  1062. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1063. }
  1064. } else {
  1065. if (init_duplicate_context(s) < 0)
  1066. goto fail;
  1067. s->start_mb_y = 0;
  1068. s->end_mb_y = s->mb_height;
  1069. }
  1070. s->slice_context_count = nb_slices;
  1071. }
  1072. return 0;
  1073. fail:
  1074. ff_MPV_common_end(s);
  1075. return err;
  1076. }
  1077. /* init common structure for both encoder and decoder */
  1078. void ff_MPV_common_end(MpegEncContext *s)
  1079. {
  1080. int i;
  1081. if (s->slice_context_count > 1) {
  1082. for (i = 0; i < s->slice_context_count; i++) {
  1083. free_duplicate_context(s->thread_context[i]);
  1084. }
  1085. for (i = 1; i < s->slice_context_count; i++) {
  1086. av_freep(&s->thread_context[i]);
  1087. }
  1088. s->slice_context_count = 1;
  1089. } else free_duplicate_context(s);
  1090. av_freep(&s->parse_context.buffer);
  1091. s->parse_context.buffer_size = 0;
  1092. av_freep(&s->bitstream_buffer);
  1093. s->allocated_bitstream_buffer_size = 0;
  1094. av_freep(&s->avctx->stats_out);
  1095. av_freep(&s->ac_stats);
  1096. if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
  1097. if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
  1098. s->q_chroma_intra_matrix= NULL;
  1099. s->q_chroma_intra_matrix16= NULL;
  1100. av_freep(&s->q_intra_matrix);
  1101. av_freep(&s->q_inter_matrix);
  1102. av_freep(&s->q_intra_matrix16);
  1103. av_freep(&s->q_inter_matrix16);
  1104. av_freep(&s->input_picture);
  1105. av_freep(&s->reordered_input_picture);
  1106. av_freep(&s->dct_offset);
  1107. if (s->picture) {
  1108. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1109. free_picture_tables(&s->picture[i]);
  1110. ff_mpeg_unref_picture(s, &s->picture[i]);
  1111. }
  1112. }
  1113. av_freep(&s->picture);
  1114. free_picture_tables(&s->last_picture);
  1115. ff_mpeg_unref_picture(s, &s->last_picture);
  1116. free_picture_tables(&s->current_picture);
  1117. ff_mpeg_unref_picture(s, &s->current_picture);
  1118. free_picture_tables(&s->next_picture);
  1119. ff_mpeg_unref_picture(s, &s->next_picture);
  1120. free_picture_tables(&s->new_picture);
  1121. ff_mpeg_unref_picture(s, &s->new_picture);
  1122. free_context_frame(s);
  1123. s->context_initialized = 0;
  1124. s->last_picture_ptr =
  1125. s->next_picture_ptr =
  1126. s->current_picture_ptr = NULL;
  1127. s->linesize = s->uvlinesize = 0;
  1128. }
  1129. void ff_init_rl(RLTable *rl,
  1130. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1131. {
  1132. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1133. uint8_t index_run[MAX_RUN + 1];
  1134. int last, run, level, start, end, i;
  1135. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1136. if (static_store && rl->max_level[0])
  1137. return;
  1138. /* compute max_level[], max_run[] and index_run[] */
  1139. for (last = 0; last < 2; last++) {
  1140. if (last == 0) {
  1141. start = 0;
  1142. end = rl->last;
  1143. } else {
  1144. start = rl->last;
  1145. end = rl->n;
  1146. }
  1147. memset(max_level, 0, MAX_RUN + 1);
  1148. memset(max_run, 0, MAX_LEVEL + 1);
  1149. memset(index_run, rl->n, MAX_RUN + 1);
  1150. for (i = start; i < end; i++) {
  1151. run = rl->table_run[i];
  1152. level = rl->table_level[i];
  1153. if (index_run[run] == rl->n)
  1154. index_run[run] = i;
  1155. if (level > max_level[run])
  1156. max_level[run] = level;
  1157. if (run > max_run[level])
  1158. max_run[level] = run;
  1159. }
  1160. if (static_store)
  1161. rl->max_level[last] = static_store[last];
  1162. else
  1163. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1164. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1165. if (static_store)
  1166. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1167. else
  1168. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1169. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1170. if (static_store)
  1171. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1172. else
  1173. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1174. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1175. }
  1176. }
  1177. void ff_init_vlc_rl(RLTable *rl)
  1178. {
  1179. int i, q;
  1180. for (q = 0; q < 32; q++) {
  1181. int qmul = q * 2;
  1182. int qadd = (q - 1) | 1;
  1183. if (q == 0) {
  1184. qmul = 1;
  1185. qadd = 0;
  1186. }
  1187. for (i = 0; i < rl->vlc.table_size; i++) {
  1188. int code = rl->vlc.table[i][0];
  1189. int len = rl->vlc.table[i][1];
  1190. int level, run;
  1191. if (len == 0) { // illegal code
  1192. run = 66;
  1193. level = MAX_LEVEL;
  1194. } else if (len < 0) { // more bits needed
  1195. run = 0;
  1196. level = code;
  1197. } else {
  1198. if (code == rl->n) { // esc
  1199. run = 66;
  1200. level = 0;
  1201. } else {
  1202. run = rl->table_run[code] + 1;
  1203. level = rl->table_level[code] * qmul + qadd;
  1204. if (code >= rl->last) run += 192;
  1205. }
  1206. }
  1207. rl->rl_vlc[q][i].len = len;
  1208. rl->rl_vlc[q][i].level = level;
  1209. rl->rl_vlc[q][i].run = run;
  1210. }
  1211. }
  1212. }
  1213. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1214. {
  1215. int i;
  1216. /* release non reference frames */
  1217. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1218. if (!s->picture[i].reference &&
  1219. (remove_current || &s->picture[i] != s->current_picture_ptr)) {
  1220. ff_mpeg_unref_picture(s, &s->picture[i]);
  1221. }
  1222. }
  1223. }
  1224. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1225. {
  1226. if ( (s->avctx->active_thread_type & FF_THREAD_FRAME)
  1227. && pic->f.qscale_table //check if the frame has anything allocated
  1228. && pic->period_since_free < s->avctx->thread_count)
  1229. return 0;
  1230. if (pic == s->last_picture_ptr)
  1231. return 0;
  1232. if (pic->f.data[0] == NULL)
  1233. return 1;
  1234. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1235. return 1;
  1236. return 0;
  1237. }
  1238. static int find_unused_picture(MpegEncContext *s, int shared)
  1239. {
  1240. int i;
  1241. if (shared) {
  1242. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1243. if (s->picture[i].f.data[0] == NULL && &s->picture[i] != s->last_picture_ptr)
  1244. return i;
  1245. }
  1246. } else {
  1247. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1248. if (pic_is_unused(s, &s->picture[i]))
  1249. return i;
  1250. }
  1251. }
  1252. av_log(s->avctx, AV_LOG_FATAL,
  1253. "Internal error, picture buffer overflow\n");
  1254. /* We could return -1, but the codec would crash trying to draw into a
  1255. * non-existing frame anyway. This is safer than waiting for a random crash.
  1256. * Also the return of this is never useful, an encoder must only allocate
  1257. * as much as allowed in the specification. This has no relationship to how
  1258. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1259. * enough for such valid streams).
  1260. * Plus, a decoder has to check stream validity and remove frames if too
  1261. * many reference frames are around. Waiting for "OOM" is not correct at
  1262. * all. Similarly, missing reference frames have to be replaced by
  1263. * interpolated/MC frames, anything else is a bug in the codec ...
  1264. */
  1265. abort();
  1266. return -1;
  1267. }
  1268. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1269. {
  1270. int ret = find_unused_picture(s, shared);
  1271. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1272. if (s->picture[ret].needs_realloc) {
  1273. s->picture[ret].needs_realloc = 0;
  1274. free_picture_tables(&s->picture[ret]);
  1275. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1276. avcodec_get_frame_defaults(&s->picture[ret].f);
  1277. }
  1278. }
  1279. return ret;
  1280. }
  1281. static void update_noise_reduction(MpegEncContext *s)
  1282. {
  1283. int intra, i;
  1284. for (intra = 0; intra < 2; intra++) {
  1285. if (s->dct_count[intra] > (1 << 16)) {
  1286. for (i = 0; i < 64; i++) {
  1287. s->dct_error_sum[intra][i] >>= 1;
  1288. }
  1289. s->dct_count[intra] >>= 1;
  1290. }
  1291. for (i = 0; i < 64; i++) {
  1292. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1293. s->dct_count[intra] +
  1294. s->dct_error_sum[intra][i] / 2) /
  1295. (s->dct_error_sum[intra][i] + 1);
  1296. }
  1297. }
  1298. }
  1299. /**
  1300. * generic function for encode/decode called after coding/decoding
  1301. * the header and before a frame is coded/decoded.
  1302. */
  1303. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1304. {
  1305. int i, ret;
  1306. Picture *pic;
  1307. s->mb_skipped = 0;
  1308. if (!ff_thread_can_start_frame(avctx)) {
  1309. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1310. return -1;
  1311. }
  1312. /* mark & release old frames */
  1313. if (s->out_format != FMT_H264 || s->codec_id == AV_CODEC_ID_SVQ3) {
  1314. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1315. s->last_picture_ptr != s->next_picture_ptr &&
  1316. s->last_picture_ptr->f.data[0]) {
  1317. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1318. }
  1319. /* release forgotten pictures */
  1320. /* if (mpeg124/h263) */
  1321. if (!s->encoding) {
  1322. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1323. if (&s->picture[i] != s->last_picture_ptr &&
  1324. &s->picture[i] != s->next_picture_ptr &&
  1325. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1326. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1327. av_log(avctx, AV_LOG_ERROR,
  1328. "releasing zombie picture\n");
  1329. ff_mpeg_unref_picture(s, &s->picture[i]);
  1330. }
  1331. }
  1332. }
  1333. }
  1334. if (!s->encoding) {
  1335. ff_release_unused_pictures(s, 1);
  1336. if (s->current_picture_ptr &&
  1337. s->current_picture_ptr->f.data[0] == NULL) {
  1338. // we already have a unused image
  1339. // (maybe it was set before reading the header)
  1340. pic = s->current_picture_ptr;
  1341. } else {
  1342. i = ff_find_unused_picture(s, 0);
  1343. if (i < 0) {
  1344. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1345. return i;
  1346. }
  1347. pic = &s->picture[i];
  1348. }
  1349. pic->reference = 0;
  1350. if (!s->droppable) {
  1351. if (s->codec_id == AV_CODEC_ID_H264)
  1352. pic->reference = s->picture_structure;
  1353. else if (s->pict_type != AV_PICTURE_TYPE_B)
  1354. pic->reference = 3;
  1355. }
  1356. pic->f.coded_picture_number = s->coded_picture_number++;
  1357. if (ff_alloc_picture(s, pic, 0) < 0)
  1358. return -1;
  1359. s->current_picture_ptr = pic;
  1360. // FIXME use only the vars from current_pic
  1361. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1362. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1363. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1364. if (s->picture_structure != PICT_FRAME)
  1365. s->current_picture_ptr->f.top_field_first =
  1366. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1367. }
  1368. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1369. !s->progressive_sequence;
  1370. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1371. }
  1372. s->current_picture_ptr->f.pict_type = s->pict_type;
  1373. // if (s->flags && CODEC_FLAG_QSCALE)
  1374. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1375. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1376. ff_mpeg_unref_picture(s, &s->current_picture);
  1377. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1378. s->current_picture_ptr)) < 0)
  1379. return ret;
  1380. if (s->codec_id != AV_CODEC_ID_H264 && s->pict_type != AV_PICTURE_TYPE_B) {
  1381. s->last_picture_ptr = s->next_picture_ptr;
  1382. if (!s->droppable)
  1383. s->next_picture_ptr = s->current_picture_ptr;
  1384. }
  1385. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1386. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1387. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1388. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1389. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1390. s->pict_type, s->droppable);
  1391. if (s->codec_id != AV_CODEC_ID_H264) {
  1392. if ((s->last_picture_ptr == NULL ||
  1393. s->last_picture_ptr->f.data[0] == NULL) &&
  1394. (s->pict_type != AV_PICTURE_TYPE_I ||
  1395. s->picture_structure != PICT_FRAME)) {
  1396. int h_chroma_shift, v_chroma_shift;
  1397. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1398. &h_chroma_shift, &v_chroma_shift);
  1399. if (s->pict_type != AV_PICTURE_TYPE_I)
  1400. av_log(avctx, AV_LOG_ERROR,
  1401. "warning: first frame is no keyframe\n");
  1402. else if (s->picture_structure != PICT_FRAME)
  1403. av_log(avctx, AV_LOG_INFO,
  1404. "allocate dummy last picture for field based first keyframe\n");
  1405. /* Allocate a dummy frame */
  1406. i = ff_find_unused_picture(s, 0);
  1407. if (i < 0) {
  1408. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1409. return i;
  1410. }
  1411. s->last_picture_ptr = &s->picture[i];
  1412. s->last_picture_ptr->f.key_frame = 0;
  1413. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1414. s->last_picture_ptr = NULL;
  1415. return -1;
  1416. }
  1417. memset(s->last_picture_ptr->f.data[0], 0x80,
  1418. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1419. memset(s->last_picture_ptr->f.data[1], 0x80,
  1420. (avctx->height >> v_chroma_shift) *
  1421. s->last_picture_ptr->f.linesize[1]);
  1422. memset(s->last_picture_ptr->f.data[2], 0x80,
  1423. (avctx->height >> v_chroma_shift) *
  1424. s->last_picture_ptr->f.linesize[2]);
  1425. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1426. for(i=0; i<avctx->height; i++)
  1427. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1428. }
  1429. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1430. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1431. }
  1432. if ((s->next_picture_ptr == NULL ||
  1433. s->next_picture_ptr->f.data[0] == NULL) &&
  1434. s->pict_type == AV_PICTURE_TYPE_B) {
  1435. /* Allocate a dummy frame */
  1436. i = ff_find_unused_picture(s, 0);
  1437. if (i < 0) {
  1438. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1439. return i;
  1440. }
  1441. s->next_picture_ptr = &s->picture[i];
  1442. s->next_picture_ptr->f.key_frame = 0;
  1443. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1444. s->next_picture_ptr = NULL;
  1445. return -1;
  1446. }
  1447. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1448. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1449. }
  1450. }
  1451. #if 0 // BUFREF-FIXME
  1452. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1453. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1454. #endif
  1455. if (s->codec_id != AV_CODEC_ID_H264) {
  1456. if (s->last_picture_ptr) {
  1457. ff_mpeg_unref_picture(s, &s->last_picture);
  1458. if (s->last_picture_ptr->f.data[0] &&
  1459. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1460. s->last_picture_ptr)) < 0)
  1461. return ret;
  1462. }
  1463. if (s->next_picture_ptr) {
  1464. ff_mpeg_unref_picture(s, &s->next_picture);
  1465. if (s->next_picture_ptr->f.data[0] &&
  1466. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1467. s->next_picture_ptr)) < 0)
  1468. return ret;
  1469. }
  1470. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1471. s->last_picture_ptr->f.data[0]));
  1472. }
  1473. if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
  1474. int i;
  1475. for (i = 0; i < 4; i++) {
  1476. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1477. s->current_picture.f.data[i] +=
  1478. s->current_picture.f.linesize[i];
  1479. }
  1480. s->current_picture.f.linesize[i] *= 2;
  1481. s->last_picture.f.linesize[i] *= 2;
  1482. s->next_picture.f.linesize[i] *= 2;
  1483. }
  1484. }
  1485. s->err_recognition = avctx->err_recognition;
  1486. /* set dequantizer, we can't do it during init as
  1487. * it might change for mpeg4 and we can't do it in the header
  1488. * decode as init is not called for mpeg4 there yet */
  1489. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1490. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1491. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1492. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1493. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1494. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1495. } else {
  1496. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1497. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1498. }
  1499. if (s->dct_error_sum) {
  1500. assert(s->avctx->noise_reduction && s->encoding);
  1501. update_noise_reduction(s);
  1502. }
  1503. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1504. return ff_xvmc_field_start(s, avctx);
  1505. return 0;
  1506. }
  1507. /* generic function for encode/decode called after a
  1508. * frame has been coded/decoded. */
  1509. void ff_MPV_frame_end(MpegEncContext *s)
  1510. {
  1511. int i;
  1512. /* redraw edges for the frame if decoding didn't complete */
  1513. // just to make sure that all data is rendered.
  1514. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1515. ff_xvmc_field_end(s);
  1516. } else if ((s->er.error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND)) &&
  1517. !s->avctx->hwaccel &&
  1518. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1519. s->unrestricted_mv &&
  1520. s->current_picture.reference &&
  1521. !s->intra_only &&
  1522. !(s->flags & CODEC_FLAG_EMU_EDGE) &&
  1523. !s->avctx->lowres
  1524. ) {
  1525. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1526. int hshift = desc->log2_chroma_w;
  1527. int vshift = desc->log2_chroma_h;
  1528. s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
  1529. s->h_edge_pos, s->v_edge_pos,
  1530. EDGE_WIDTH, EDGE_WIDTH,
  1531. EDGE_TOP | EDGE_BOTTOM);
  1532. s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
  1533. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1534. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1535. EDGE_TOP | EDGE_BOTTOM);
  1536. s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
  1537. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1538. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1539. EDGE_TOP | EDGE_BOTTOM);
  1540. }
  1541. emms_c();
  1542. s->last_pict_type = s->pict_type;
  1543. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1544. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1545. s->last_non_b_pict_type = s->pict_type;
  1546. }
  1547. #if 0
  1548. /* copy back current_picture variables */
  1549. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1550. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1551. s->picture[i] = s->current_picture;
  1552. break;
  1553. }
  1554. }
  1555. assert(i < MAX_PICTURE_COUNT);
  1556. #endif
  1557. if (s->encoding) {
  1558. /* release non-reference frames */
  1559. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1560. if (!s->picture[i].reference)
  1561. ff_mpeg_unref_picture(s, &s->picture[i]);
  1562. }
  1563. }
  1564. // clear copies, to avoid confusion
  1565. #if 0
  1566. memset(&s->last_picture, 0, sizeof(Picture));
  1567. memset(&s->next_picture, 0, sizeof(Picture));
  1568. memset(&s->current_picture, 0, sizeof(Picture));
  1569. #endif
  1570. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1571. if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.reference)
  1572. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1573. }
  1574. /**
  1575. * Draw a line from (ex, ey) -> (sx, sy).
  1576. * @param w width of the image
  1577. * @param h height of the image
  1578. * @param stride stride/linesize of the image
  1579. * @param color color of the arrow
  1580. */
  1581. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1582. int w, int h, int stride, int color)
  1583. {
  1584. int x, y, fr, f;
  1585. sx = av_clip(sx, 0, w - 1);
  1586. sy = av_clip(sy, 0, h - 1);
  1587. ex = av_clip(ex, 0, w - 1);
  1588. ey = av_clip(ey, 0, h - 1);
  1589. buf[sy * stride + sx] += color;
  1590. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1591. if (sx > ex) {
  1592. FFSWAP(int, sx, ex);
  1593. FFSWAP(int, sy, ey);
  1594. }
  1595. buf += sx + sy * stride;
  1596. ex -= sx;
  1597. f = ((ey - sy) << 16) / ex;
  1598. for (x = 0; x <= ex; x++) {
  1599. y = (x * f) >> 16;
  1600. fr = (x * f) & 0xFFFF;
  1601. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1602. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1603. }
  1604. } else {
  1605. if (sy > ey) {
  1606. FFSWAP(int, sx, ex);
  1607. FFSWAP(int, sy, ey);
  1608. }
  1609. buf += sx + sy * stride;
  1610. ey -= sy;
  1611. if (ey)
  1612. f = ((ex - sx) << 16) / ey;
  1613. else
  1614. f = 0;
  1615. for(y= 0; y <= ey; y++){
  1616. x = (y*f) >> 16;
  1617. fr = (y*f) & 0xFFFF;
  1618. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1619. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1620. }
  1621. }
  1622. }
  1623. /**
  1624. * Draw an arrow from (ex, ey) -> (sx, sy).
  1625. * @param w width of the image
  1626. * @param h height of the image
  1627. * @param stride stride/linesize of the image
  1628. * @param color color of the arrow
  1629. */
  1630. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1631. int ey, int w, int h, int stride, int color)
  1632. {
  1633. int dx,dy;
  1634. sx = av_clip(sx, -100, w + 100);
  1635. sy = av_clip(sy, -100, h + 100);
  1636. ex = av_clip(ex, -100, w + 100);
  1637. ey = av_clip(ey, -100, h + 100);
  1638. dx = ex - sx;
  1639. dy = ey - sy;
  1640. if (dx * dx + dy * dy > 3 * 3) {
  1641. int rx = dx + dy;
  1642. int ry = -dx + dy;
  1643. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1644. // FIXME subpixel accuracy
  1645. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1646. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1647. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1648. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1649. }
  1650. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1651. }
  1652. /**
  1653. * Print debugging info for the given picture.
  1654. */
  1655. void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, uint8_t *mbskip_table,
  1656. uint8_t *visualization_buffer[3], int *low_delay,
  1657. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1658. {
  1659. AVFrame *pict;
  1660. if (avctx->hwaccel || !p || !p->mb_type
  1661. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1662. return;
  1663. pict = &p->f;
  1664. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1665. int x,y;
  1666. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1667. av_get_picture_type_char(pict->pict_type));
  1668. for (y = 0; y < mb_height; y++) {
  1669. for (x = 0; x < mb_width; x++) {
  1670. if (avctx->debug & FF_DEBUG_SKIP) {
  1671. int count = mbskip_table[x + y * mb_stride];
  1672. if (count > 9)
  1673. count = 9;
  1674. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1675. }
  1676. if (avctx->debug & FF_DEBUG_QP) {
  1677. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1678. p->qscale_table[x + y * mb_stride]);
  1679. }
  1680. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1681. int mb_type = p->mb_type[x + y * mb_stride];
  1682. // Type & MV direction
  1683. if (IS_PCM(mb_type))
  1684. av_log(avctx, AV_LOG_DEBUG, "P");
  1685. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1686. av_log(avctx, AV_LOG_DEBUG, "A");
  1687. else if (IS_INTRA4x4(mb_type))
  1688. av_log(avctx, AV_LOG_DEBUG, "i");
  1689. else if (IS_INTRA16x16(mb_type))
  1690. av_log(avctx, AV_LOG_DEBUG, "I");
  1691. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1692. av_log(avctx, AV_LOG_DEBUG, "d");
  1693. else if (IS_DIRECT(mb_type))
  1694. av_log(avctx, AV_LOG_DEBUG, "D");
  1695. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1696. av_log(avctx, AV_LOG_DEBUG, "g");
  1697. else if (IS_GMC(mb_type))
  1698. av_log(avctx, AV_LOG_DEBUG, "G");
  1699. else if (IS_SKIP(mb_type))
  1700. av_log(avctx, AV_LOG_DEBUG, "S");
  1701. else if (!USES_LIST(mb_type, 1))
  1702. av_log(avctx, AV_LOG_DEBUG, ">");
  1703. else if (!USES_LIST(mb_type, 0))
  1704. av_log(avctx, AV_LOG_DEBUG, "<");
  1705. else {
  1706. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1707. av_log(avctx, AV_LOG_DEBUG, "X");
  1708. }
  1709. // segmentation
  1710. if (IS_8X8(mb_type))
  1711. av_log(avctx, AV_LOG_DEBUG, "+");
  1712. else if (IS_16X8(mb_type))
  1713. av_log(avctx, AV_LOG_DEBUG, "-");
  1714. else if (IS_8X16(mb_type))
  1715. av_log(avctx, AV_LOG_DEBUG, "|");
  1716. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1717. av_log(avctx, AV_LOG_DEBUG, " ");
  1718. else
  1719. av_log(avctx, AV_LOG_DEBUG, "?");
  1720. if (IS_INTERLACED(mb_type))
  1721. av_log(avctx, AV_LOG_DEBUG, "=");
  1722. else
  1723. av_log(avctx, AV_LOG_DEBUG, " ");
  1724. }
  1725. }
  1726. av_log(avctx, AV_LOG_DEBUG, "\n");
  1727. }
  1728. }
  1729. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1730. (avctx->debug_mv)) {
  1731. const int shift = 1 + quarter_sample;
  1732. int mb_y;
  1733. uint8_t *ptr;
  1734. int i;
  1735. int h_chroma_shift, v_chroma_shift, block_height;
  1736. const int width = avctx->width;
  1737. const int height = avctx->height;
  1738. const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
  1739. const int mv_stride = (mb_width << mv_sample_log2) +
  1740. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1741. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1742. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1743. for (i = 0; i < 3; i++) {
  1744. size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16):
  1745. pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift;
  1746. visualization_buffer[i]= av_realloc(visualization_buffer[i], size);
  1747. memcpy(visualization_buffer[i], pict->data[i], size);
  1748. pict->data[i] = visualization_buffer[i];
  1749. }
  1750. pict->opaque = NULL;
  1751. ptr = pict->data[0];
  1752. block_height = 16 >> v_chroma_shift;
  1753. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1754. int mb_x;
  1755. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1756. const int mb_index = mb_x + mb_y * mb_stride;
  1757. if ((avctx->debug_mv) && p->motion_val[0]) {
  1758. int type;
  1759. for (type = 0; type < 3; type++) {
  1760. int direction = 0;
  1761. switch (type) {
  1762. case 0:
  1763. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1764. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1765. continue;
  1766. direction = 0;
  1767. break;
  1768. case 1:
  1769. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1770. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1771. continue;
  1772. direction = 0;
  1773. break;
  1774. case 2:
  1775. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1776. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1777. continue;
  1778. direction = 1;
  1779. break;
  1780. }
  1781. if (!USES_LIST(p->mb_type[mb_index], direction))
  1782. continue;
  1783. if (IS_8X8(p->mb_type[mb_index])) {
  1784. int i;
  1785. for (i = 0; i < 4; i++) {
  1786. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1787. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1788. int xy = (mb_x * 2 + (i & 1) +
  1789. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1790. int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
  1791. int my = (p->motion_val[direction][xy][1] >> shift) + sy;
  1792. draw_arrow(ptr, sx, sy, mx, my, width,
  1793. height, pict->linesize[0], 100);
  1794. }
  1795. } else if (IS_16X8(p->mb_type[mb_index])) {
  1796. int i;
  1797. for (i = 0; i < 2; i++) {
  1798. int sx = mb_x * 16 + 8;
  1799. int sy = mb_y * 16 + 4 + 8 * i;
  1800. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1801. int mx = (p->motion_val[direction][xy][0] >> shift);
  1802. int my = (p->motion_val[direction][xy][1] >> shift);
  1803. if (IS_INTERLACED(p->mb_type[mb_index]))
  1804. my *= 2;
  1805. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1806. height, pict->linesize[0], 100);
  1807. }
  1808. } else if (IS_8X16(p->mb_type[mb_index])) {
  1809. int i;
  1810. for (i = 0; i < 2; i++) {
  1811. int sx = mb_x * 16 + 4 + 8 * i;
  1812. int sy = mb_y * 16 + 8;
  1813. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1814. int mx = p->motion_val[direction][xy][0] >> shift;
  1815. int my = p->motion_val[direction][xy][1] >> shift;
  1816. if (IS_INTERLACED(p->mb_type[mb_index]))
  1817. my *= 2;
  1818. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1819. height, pict->linesize[0], 100);
  1820. }
  1821. } else {
  1822. int sx= mb_x * 16 + 8;
  1823. int sy= mb_y * 16 + 8;
  1824. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1825. int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
  1826. int my= (p->motion_val[direction][xy][1]>>shift) + sy;
  1827. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
  1828. }
  1829. }
  1830. }
  1831. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1832. uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
  1833. 0x0101010101010101ULL;
  1834. int y;
  1835. for (y = 0; y < block_height; y++) {
  1836. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1837. (block_height * mb_y + y) *
  1838. pict->linesize[1]) = c;
  1839. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1840. (block_height * mb_y + y) *
  1841. pict->linesize[2]) = c;
  1842. }
  1843. }
  1844. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1845. p->motion_val[0]) {
  1846. int mb_type = p->mb_type[mb_index];
  1847. uint64_t u,v;
  1848. int y;
  1849. #define COLOR(theta, r) \
  1850. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1851. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1852. u = v = 128;
  1853. if (IS_PCM(mb_type)) {
  1854. COLOR(120, 48)
  1855. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1856. IS_INTRA16x16(mb_type)) {
  1857. COLOR(30, 48)
  1858. } else if (IS_INTRA4x4(mb_type)) {
  1859. COLOR(90, 48)
  1860. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1861. // COLOR(120, 48)
  1862. } else if (IS_DIRECT(mb_type)) {
  1863. COLOR(150, 48)
  1864. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1865. COLOR(170, 48)
  1866. } else if (IS_GMC(mb_type)) {
  1867. COLOR(190, 48)
  1868. } else if (IS_SKIP(mb_type)) {
  1869. // COLOR(180, 48)
  1870. } else if (!USES_LIST(mb_type, 1)) {
  1871. COLOR(240, 48)
  1872. } else if (!USES_LIST(mb_type, 0)) {
  1873. COLOR(0, 48)
  1874. } else {
  1875. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1876. COLOR(300,48)
  1877. }
  1878. u *= 0x0101010101010101ULL;
  1879. v *= 0x0101010101010101ULL;
  1880. for (y = 0; y < block_height; y++) {
  1881. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1882. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1883. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1884. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1885. }
  1886. // segmentation
  1887. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1888. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1889. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1890. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1891. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1892. }
  1893. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1894. for (y = 0; y < 16; y++)
  1895. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1896. pict->linesize[0]] ^= 0x80;
  1897. }
  1898. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1899. int dm = 1 << (mv_sample_log2 - 2);
  1900. for (i = 0; i < 4; i++) {
  1901. int sx = mb_x * 16 + 8 * (i & 1);
  1902. int sy = mb_y * 16 + 8 * (i >> 1);
  1903. int xy = (mb_x * 2 + (i & 1) +
  1904. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1905. // FIXME bidir
  1906. int32_t *mv = (int32_t *) &p->motion_val[0][xy];
  1907. if (mv[0] != mv[dm] ||
  1908. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1909. for (y = 0; y < 8; y++)
  1910. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1911. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1912. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1913. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1914. }
  1915. }
  1916. if (IS_INTERLACED(mb_type) &&
  1917. avctx->codec->id == AV_CODEC_ID_H264) {
  1918. // hmm
  1919. }
  1920. }
  1921. mbskip_table[mb_index] = 0;
  1922. }
  1923. }
  1924. }
  1925. }
  1926. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1927. {
  1928. ff_print_debug_info2(s->avctx, p, s->mbskip_table, s->visualization_buffer, &s->low_delay,
  1929. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1930. }
  1931. static inline int hpel_motion_lowres(MpegEncContext *s,
  1932. uint8_t *dest, uint8_t *src,
  1933. int field_based, int field_select,
  1934. int src_x, int src_y,
  1935. int width, int height, int stride,
  1936. int h_edge_pos, int v_edge_pos,
  1937. int w, int h, h264_chroma_mc_func *pix_op,
  1938. int motion_x, int motion_y)
  1939. {
  1940. const int lowres = s->avctx->lowres;
  1941. const int op_index = FFMIN(lowres, 2);
  1942. const int s_mask = (2 << lowres) - 1;
  1943. int emu = 0;
  1944. int sx, sy;
  1945. if (s->quarter_sample) {
  1946. motion_x /= 2;
  1947. motion_y /= 2;
  1948. }
  1949. sx = motion_x & s_mask;
  1950. sy = motion_y & s_mask;
  1951. src_x += motion_x >> lowres + 1;
  1952. src_y += motion_y >> lowres + 1;
  1953. src += src_y * stride + src_x;
  1954. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  1955. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  1956. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
  1957. (h + 1) << field_based, src_x,
  1958. src_y << field_based,
  1959. h_edge_pos,
  1960. v_edge_pos);
  1961. src = s->edge_emu_buffer;
  1962. emu = 1;
  1963. }
  1964. sx = (sx << 2) >> lowres;
  1965. sy = (sy << 2) >> lowres;
  1966. if (field_select)
  1967. src += s->linesize;
  1968. pix_op[op_index](dest, src, stride, h, sx, sy);
  1969. return emu;
  1970. }
  1971. /* apply one mpeg motion vector to the three components */
  1972. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1973. uint8_t *dest_y,
  1974. uint8_t *dest_cb,
  1975. uint8_t *dest_cr,
  1976. int field_based,
  1977. int bottom_field,
  1978. int field_select,
  1979. uint8_t **ref_picture,
  1980. h264_chroma_mc_func *pix_op,
  1981. int motion_x, int motion_y,
  1982. int h, int mb_y)
  1983. {
  1984. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1985. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
  1986. uvsx, uvsy;
  1987. const int lowres = s->avctx->lowres;
  1988. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 2);
  1989. const int block_s = 8>>lowres;
  1990. const int s_mask = (2 << lowres) - 1;
  1991. const int h_edge_pos = s->h_edge_pos >> lowres;
  1992. const int v_edge_pos = s->v_edge_pos >> lowres;
  1993. linesize = s->current_picture.f.linesize[0] << field_based;
  1994. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1995. // FIXME obviously not perfect but qpel will not work in lowres anyway
  1996. if (s->quarter_sample) {
  1997. motion_x /= 2;
  1998. motion_y /= 2;
  1999. }
  2000. if(field_based){
  2001. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  2002. }
  2003. sx = motion_x & s_mask;
  2004. sy = motion_y & s_mask;
  2005. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  2006. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  2007. if (s->out_format == FMT_H263) {
  2008. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  2009. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  2010. uvsrc_x = src_x >> 1;
  2011. uvsrc_y = src_y >> 1;
  2012. } else if (s->out_format == FMT_H261) {
  2013. // even chroma mv's are full pel in H261
  2014. mx = motion_x / 4;
  2015. my = motion_y / 4;
  2016. uvsx = (2 * mx) & s_mask;
  2017. uvsy = (2 * my) & s_mask;
  2018. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  2019. uvsrc_y = mb_y * block_s + (my >> lowres);
  2020. } else {
  2021. if(s->chroma_y_shift){
  2022. mx = motion_x / 2;
  2023. my = motion_y / 2;
  2024. uvsx = mx & s_mask;
  2025. uvsy = my & s_mask;
  2026. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  2027. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  2028. } else {
  2029. if(s->chroma_x_shift){
  2030. //Chroma422
  2031. mx = motion_x / 2;
  2032. uvsx = mx & s_mask;
  2033. uvsy = motion_y & s_mask;
  2034. uvsrc_y = src_y;
  2035. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2036. } else {
  2037. //Chroma444
  2038. uvsx = motion_x & s_mask;
  2039. uvsy = motion_y & s_mask;
  2040. uvsrc_x = src_x;
  2041. uvsrc_y = src_y;
  2042. }
  2043. }
  2044. }
  2045. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2046. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2047. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2048. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
  2049. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2050. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2051. linesize >> field_based, 17, 17 + field_based,
  2052. src_x, src_y << field_based, h_edge_pos,
  2053. v_edge_pos);
  2054. ptr_y = s->edge_emu_buffer;
  2055. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2056. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  2057. s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,
  2058. 9 + field_based,
  2059. uvsrc_x, uvsrc_y << field_based,
  2060. h_edge_pos >> 1, v_edge_pos >> 1);
  2061. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,
  2062. 9 + field_based,
  2063. uvsrc_x, uvsrc_y << field_based,
  2064. h_edge_pos >> 1, v_edge_pos >> 1);
  2065. ptr_cb = uvbuf;
  2066. ptr_cr = uvbuf + 16;
  2067. }
  2068. }
  2069. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  2070. if (bottom_field) {
  2071. dest_y += s->linesize;
  2072. dest_cb += s->uvlinesize;
  2073. dest_cr += s->uvlinesize;
  2074. }
  2075. if (field_select) {
  2076. ptr_y += s->linesize;
  2077. ptr_cb += s->uvlinesize;
  2078. ptr_cr += s->uvlinesize;
  2079. }
  2080. sx = (sx << 2) >> lowres;
  2081. sy = (sy << 2) >> lowres;
  2082. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2083. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2084. uvsx = (uvsx << 2) >> lowres;
  2085. uvsy = (uvsy << 2) >> lowres;
  2086. if (h >> s->chroma_y_shift) {
  2087. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  2088. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  2089. }
  2090. }
  2091. // FIXME h261 lowres loop filter
  2092. }
  2093. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2094. uint8_t *dest_cb, uint8_t *dest_cr,
  2095. uint8_t **ref_picture,
  2096. h264_chroma_mc_func * pix_op,
  2097. int mx, int my)
  2098. {
  2099. const int lowres = s->avctx->lowres;
  2100. const int op_index = FFMIN(lowres, 2);
  2101. const int block_s = 8 >> lowres;
  2102. const int s_mask = (2 << lowres) - 1;
  2103. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2104. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2105. int emu = 0, src_x, src_y, offset, sx, sy;
  2106. uint8_t *ptr;
  2107. if (s->quarter_sample) {
  2108. mx /= 2;
  2109. my /= 2;
  2110. }
  2111. /* In case of 8X8, we construct a single chroma motion vector
  2112. with a special rounding */
  2113. mx = ff_h263_round_chroma(mx);
  2114. my = ff_h263_round_chroma(my);
  2115. sx = mx & s_mask;
  2116. sy = my & s_mask;
  2117. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2118. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2119. offset = src_y * s->uvlinesize + src_x;
  2120. ptr = ref_picture[1] + offset;
  2121. if (s->flags & CODEC_FLAG_EMU_EDGE) {
  2122. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2123. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2124. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
  2125. 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  2126. ptr = s->edge_emu_buffer;
  2127. emu = 1;
  2128. }
  2129. }
  2130. sx = (sx << 2) >> lowres;
  2131. sy = (sy << 2) >> lowres;
  2132. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2133. ptr = ref_picture[2] + offset;
  2134. if (emu) {
  2135. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
  2136. src_x, src_y, h_edge_pos, v_edge_pos);
  2137. ptr = s->edge_emu_buffer;
  2138. }
  2139. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2140. }
  2141. /**
  2142. * motion compensation of a single macroblock
  2143. * @param s context
  2144. * @param dest_y luma destination pointer
  2145. * @param dest_cb chroma cb/u destination pointer
  2146. * @param dest_cr chroma cr/v destination pointer
  2147. * @param dir direction (0->forward, 1->backward)
  2148. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2149. * @param pix_op halfpel motion compensation function (average or put normally)
  2150. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2151. */
  2152. static inline void MPV_motion_lowres(MpegEncContext *s,
  2153. uint8_t *dest_y, uint8_t *dest_cb,
  2154. uint8_t *dest_cr,
  2155. int dir, uint8_t **ref_picture,
  2156. h264_chroma_mc_func *pix_op)
  2157. {
  2158. int mx, my;
  2159. int mb_x, mb_y, i;
  2160. const int lowres = s->avctx->lowres;
  2161. const int block_s = 8 >>lowres;
  2162. mb_x = s->mb_x;
  2163. mb_y = s->mb_y;
  2164. switch (s->mv_type) {
  2165. case MV_TYPE_16X16:
  2166. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2167. 0, 0, 0,
  2168. ref_picture, pix_op,
  2169. s->mv[dir][0][0], s->mv[dir][0][1],
  2170. 2 * block_s, mb_y);
  2171. break;
  2172. case MV_TYPE_8X8:
  2173. mx = 0;
  2174. my = 0;
  2175. for (i = 0; i < 4; i++) {
  2176. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2177. s->linesize) * block_s,
  2178. ref_picture[0], 0, 0,
  2179. (2 * mb_x + (i & 1)) * block_s,
  2180. (2 * mb_y + (i >> 1)) * block_s,
  2181. s->width, s->height, s->linesize,
  2182. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2183. block_s, block_s, pix_op,
  2184. s->mv[dir][i][0], s->mv[dir][i][1]);
  2185. mx += s->mv[dir][i][0];
  2186. my += s->mv[dir][i][1];
  2187. }
  2188. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2189. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2190. pix_op, mx, my);
  2191. break;
  2192. case MV_TYPE_FIELD:
  2193. if (s->picture_structure == PICT_FRAME) {
  2194. /* top field */
  2195. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2196. 1, 0, s->field_select[dir][0],
  2197. ref_picture, pix_op,
  2198. s->mv[dir][0][0], s->mv[dir][0][1],
  2199. block_s, mb_y);
  2200. /* bottom field */
  2201. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2202. 1, 1, s->field_select[dir][1],
  2203. ref_picture, pix_op,
  2204. s->mv[dir][1][0], s->mv[dir][1][1],
  2205. block_s, mb_y);
  2206. } else {
  2207. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2208. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2209. ref_picture = s->current_picture_ptr->f.data;
  2210. }
  2211. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2212. 0, 0, s->field_select[dir][0],
  2213. ref_picture, pix_op,
  2214. s->mv[dir][0][0],
  2215. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2216. }
  2217. break;
  2218. case MV_TYPE_16X8:
  2219. for (i = 0; i < 2; i++) {
  2220. uint8_t **ref2picture;
  2221. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2222. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2223. ref2picture = ref_picture;
  2224. } else {
  2225. ref2picture = s->current_picture_ptr->f.data;
  2226. }
  2227. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2228. 0, 0, s->field_select[dir][i],
  2229. ref2picture, pix_op,
  2230. s->mv[dir][i][0], s->mv[dir][i][1] +
  2231. 2 * block_s * i, block_s, mb_y >> 1);
  2232. dest_y += 2 * block_s * s->linesize;
  2233. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2234. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2235. }
  2236. break;
  2237. case MV_TYPE_DMV:
  2238. if (s->picture_structure == PICT_FRAME) {
  2239. for (i = 0; i < 2; i++) {
  2240. int j;
  2241. for (j = 0; j < 2; j++) {
  2242. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2243. 1, j, j ^ i,
  2244. ref_picture, pix_op,
  2245. s->mv[dir][2 * i + j][0],
  2246. s->mv[dir][2 * i + j][1],
  2247. block_s, mb_y);
  2248. }
  2249. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2250. }
  2251. } else {
  2252. for (i = 0; i < 2; i++) {
  2253. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2254. 0, 0, s->picture_structure != i + 1,
  2255. ref_picture, pix_op,
  2256. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2257. 2 * block_s, mb_y >> 1);
  2258. // after put we make avg of the same block
  2259. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2260. // opposite parity is always in the same
  2261. // frame if this is second field
  2262. if (!s->first_field) {
  2263. ref_picture = s->current_picture_ptr->f.data;
  2264. }
  2265. }
  2266. }
  2267. break;
  2268. default:
  2269. av_assert2(0);
  2270. }
  2271. }
  2272. /**
  2273. * find the lowest MB row referenced in the MVs
  2274. */
  2275. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2276. {
  2277. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2278. int my, off, i, mvs;
  2279. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2280. goto unhandled;
  2281. switch (s->mv_type) {
  2282. case MV_TYPE_16X16:
  2283. mvs = 1;
  2284. break;
  2285. case MV_TYPE_16X8:
  2286. mvs = 2;
  2287. break;
  2288. case MV_TYPE_8X8:
  2289. mvs = 4;
  2290. break;
  2291. default:
  2292. goto unhandled;
  2293. }
  2294. for (i = 0; i < mvs; i++) {
  2295. my = s->mv[dir][i][1]<<qpel_shift;
  2296. my_max = FFMAX(my_max, my);
  2297. my_min = FFMIN(my_min, my);
  2298. }
  2299. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2300. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2301. unhandled:
  2302. return s->mb_height-1;
  2303. }
  2304. /* put block[] to dest[] */
  2305. static inline void put_dct(MpegEncContext *s,
  2306. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2307. {
  2308. s->dct_unquantize_intra(s, block, i, qscale);
  2309. s->dsp.idct_put (dest, line_size, block);
  2310. }
  2311. /* add block[] to dest[] */
  2312. static inline void add_dct(MpegEncContext *s,
  2313. int16_t *block, int i, uint8_t *dest, int line_size)
  2314. {
  2315. if (s->block_last_index[i] >= 0) {
  2316. s->dsp.idct_add (dest, line_size, block);
  2317. }
  2318. }
  2319. static inline void add_dequant_dct(MpegEncContext *s,
  2320. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2321. {
  2322. if (s->block_last_index[i] >= 0) {
  2323. s->dct_unquantize_inter(s, block, i, qscale);
  2324. s->dsp.idct_add (dest, line_size, block);
  2325. }
  2326. }
  2327. /**
  2328. * Clean dc, ac, coded_block for the current non-intra MB.
  2329. */
  2330. void ff_clean_intra_table_entries(MpegEncContext *s)
  2331. {
  2332. int wrap = s->b8_stride;
  2333. int xy = s->block_index[0];
  2334. s->dc_val[0][xy ] =
  2335. s->dc_val[0][xy + 1 ] =
  2336. s->dc_val[0][xy + wrap] =
  2337. s->dc_val[0][xy + 1 + wrap] = 1024;
  2338. /* ac pred */
  2339. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2340. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2341. if (s->msmpeg4_version>=3) {
  2342. s->coded_block[xy ] =
  2343. s->coded_block[xy + 1 ] =
  2344. s->coded_block[xy + wrap] =
  2345. s->coded_block[xy + 1 + wrap] = 0;
  2346. }
  2347. /* chroma */
  2348. wrap = s->mb_stride;
  2349. xy = s->mb_x + s->mb_y * wrap;
  2350. s->dc_val[1][xy] =
  2351. s->dc_val[2][xy] = 1024;
  2352. /* ac pred */
  2353. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2354. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2355. s->mbintra_table[xy]= 0;
  2356. }
  2357. /* generic function called after a macroblock has been parsed by the
  2358. decoder or after it has been encoded by the encoder.
  2359. Important variables used:
  2360. s->mb_intra : true if intra macroblock
  2361. s->mv_dir : motion vector direction
  2362. s->mv_type : motion vector type
  2363. s->mv : motion vector
  2364. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2365. */
  2366. static av_always_inline
  2367. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2368. int lowres_flag, int is_mpeg12)
  2369. {
  2370. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2371. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  2372. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  2373. return;
  2374. }
  2375. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2376. /* print DCT coefficients */
  2377. int i,j;
  2378. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2379. for(i=0; i<6; i++){
  2380. for(j=0; j<64; j++){
  2381. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  2382. }
  2383. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2384. }
  2385. }
  2386. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2387. /* update DC predictors for P macroblocks */
  2388. if (!s->mb_intra) {
  2389. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2390. if(s->mbintra_table[mb_xy])
  2391. ff_clean_intra_table_entries(s);
  2392. } else {
  2393. s->last_dc[0] =
  2394. s->last_dc[1] =
  2395. s->last_dc[2] = 128 << s->intra_dc_precision;
  2396. }
  2397. }
  2398. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2399. s->mbintra_table[mb_xy]=1;
  2400. 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
  2401. uint8_t *dest_y, *dest_cb, *dest_cr;
  2402. int dct_linesize, dct_offset;
  2403. op_pixels_func (*op_pix)[4];
  2404. qpel_mc_func (*op_qpix)[16];
  2405. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2406. const int uvlinesize = s->current_picture.f.linesize[1];
  2407. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2408. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2409. /* avoid copy if macroblock skipped in last frame too */
  2410. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2411. if(!s->encoding){
  2412. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2413. if (s->mb_skipped) {
  2414. s->mb_skipped= 0;
  2415. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2416. *mbskip_ptr = 1;
  2417. } else if(!s->current_picture.reference) {
  2418. *mbskip_ptr = 1;
  2419. } else{
  2420. *mbskip_ptr = 0; /* not skipped */
  2421. }
  2422. }
  2423. dct_linesize = linesize << s->interlaced_dct;
  2424. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2425. if(readable){
  2426. dest_y= s->dest[0];
  2427. dest_cb= s->dest[1];
  2428. dest_cr= s->dest[2];
  2429. }else{
  2430. dest_y = s->b_scratchpad;
  2431. dest_cb= s->b_scratchpad+16*linesize;
  2432. dest_cr= s->b_scratchpad+32*linesize;
  2433. }
  2434. if (!s->mb_intra) {
  2435. /* motion handling */
  2436. /* decoding or more than one mb_type (MC was already done otherwise) */
  2437. if(!s->encoding){
  2438. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2439. if (s->mv_dir & MV_DIR_FORWARD) {
  2440. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2441. ff_MPV_lowest_referenced_row(s, 0),
  2442. 0);
  2443. }
  2444. if (s->mv_dir & MV_DIR_BACKWARD) {
  2445. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2446. ff_MPV_lowest_referenced_row(s, 1),
  2447. 0);
  2448. }
  2449. }
  2450. if(lowres_flag){
  2451. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2452. if (s->mv_dir & MV_DIR_FORWARD) {
  2453. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2454. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2455. }
  2456. if (s->mv_dir & MV_DIR_BACKWARD) {
  2457. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2458. }
  2459. }else{
  2460. op_qpix= s->me.qpel_put;
  2461. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2462. op_pix = s->dsp.put_pixels_tab;
  2463. }else{
  2464. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2465. }
  2466. if (s->mv_dir & MV_DIR_FORWARD) {
  2467. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2468. op_pix = s->dsp.avg_pixels_tab;
  2469. op_qpix= s->me.qpel_avg;
  2470. }
  2471. if (s->mv_dir & MV_DIR_BACKWARD) {
  2472. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2473. }
  2474. }
  2475. }
  2476. /* skip dequant / idct if we are really late ;) */
  2477. if(s->avctx->skip_idct){
  2478. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2479. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2480. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2481. goto skip_idct;
  2482. }
  2483. /* add dct residue */
  2484. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2485. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2486. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2487. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2488. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2489. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2490. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2491. if (s->chroma_y_shift){
  2492. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2493. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2494. }else{
  2495. dct_linesize >>= 1;
  2496. dct_offset >>=1;
  2497. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2498. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2499. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2500. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2501. }
  2502. }
  2503. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2504. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2505. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2506. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2507. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2508. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2509. if(s->chroma_y_shift){//Chroma420
  2510. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2511. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2512. }else{
  2513. //chroma422
  2514. dct_linesize = uvlinesize << s->interlaced_dct;
  2515. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2516. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2517. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2518. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2519. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2520. if(!s->chroma_x_shift){//Chroma444
  2521. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2522. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2523. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2524. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2525. }
  2526. }
  2527. }//fi gray
  2528. }
  2529. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2530. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2531. }
  2532. } else {
  2533. /* dct only in intra block */
  2534. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2535. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2536. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2537. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2538. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2539. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2540. if(s->chroma_y_shift){
  2541. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2542. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2543. }else{
  2544. dct_offset >>=1;
  2545. dct_linesize >>=1;
  2546. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2547. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2548. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2549. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2550. }
  2551. }
  2552. }else{
  2553. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2554. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2555. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2556. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2557. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2558. if(s->chroma_y_shift){
  2559. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2560. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2561. }else{
  2562. dct_linesize = uvlinesize << s->interlaced_dct;
  2563. dct_offset = s->interlaced_dct? uvlinesize : uvlinesize*block_size;
  2564. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2565. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2566. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2567. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2568. if(!s->chroma_x_shift){//Chroma444
  2569. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2570. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2571. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2572. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2573. }
  2574. }
  2575. }//gray
  2576. }
  2577. }
  2578. skip_idct:
  2579. if(!readable){
  2580. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2581. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2582. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2583. }
  2584. }
  2585. }
  2586. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2587. #if !CONFIG_SMALL
  2588. if(s->out_format == FMT_MPEG1) {
  2589. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2590. else MPV_decode_mb_internal(s, block, 0, 1);
  2591. } else
  2592. #endif
  2593. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2594. else MPV_decode_mb_internal(s, block, 0, 0);
  2595. }
  2596. /**
  2597. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2598. */
  2599. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  2600. Picture *last, int y, int h, int picture_structure,
  2601. int first_field, int draw_edges, int low_delay,
  2602. int v_edge_pos, int h_edge_pos)
  2603. {
  2604. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  2605. int hshift = desc->log2_chroma_w;
  2606. int vshift = desc->log2_chroma_h;
  2607. const int field_pic = picture_structure != PICT_FRAME;
  2608. if(field_pic){
  2609. h <<= 1;
  2610. y <<= 1;
  2611. }
  2612. if (!avctx->hwaccel &&
  2613. !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  2614. draw_edges &&
  2615. cur->reference &&
  2616. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  2617. int *linesize = cur->f.linesize;
  2618. int sides = 0, edge_h;
  2619. if (y==0) sides |= EDGE_TOP;
  2620. if (y + h >= v_edge_pos)
  2621. sides |= EDGE_BOTTOM;
  2622. edge_h= FFMIN(h, v_edge_pos - y);
  2623. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  2624. linesize[0], h_edge_pos, edge_h,
  2625. EDGE_WIDTH, EDGE_WIDTH, sides);
  2626. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  2627. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  2628. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2629. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  2630. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  2631. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  2632. }
  2633. h = FFMIN(h, avctx->height - y);
  2634. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2635. if (avctx->draw_horiz_band) {
  2636. AVFrame *src;
  2637. int offset[AV_NUM_DATA_POINTERS];
  2638. int i;
  2639. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  2640. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2641. src = &cur->f;
  2642. else if (last)
  2643. src = &last->f;
  2644. else
  2645. return;
  2646. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  2647. picture_structure == PICT_FRAME &&
  2648. avctx->codec_id != AV_CODEC_ID_H264 &&
  2649. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  2650. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2651. offset[i] = 0;
  2652. }else{
  2653. offset[0]= y * src->linesize[0];
  2654. offset[1]=
  2655. offset[2]= (y >> vshift) * src->linesize[1];
  2656. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2657. offset[i] = 0;
  2658. }
  2659. emms_c();
  2660. avctx->draw_horiz_band(avctx, src, offset,
  2661. y, picture_structure, h);
  2662. }
  2663. }
  2664. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2665. {
  2666. int draw_edges = s->unrestricted_mv && !s->intra_only;
  2667. ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
  2668. &s->last_picture, y, h, s->picture_structure,
  2669. s->first_field, draw_edges, s->low_delay,
  2670. s->v_edge_pos, s->h_edge_pos);
  2671. }
  2672. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2673. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2674. const int uvlinesize = s->current_picture.f.linesize[1];
  2675. const int mb_size= 4 - s->avctx->lowres;
  2676. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2677. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2678. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2679. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2680. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2681. 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;
  2682. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2683. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2684. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2685. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2686. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2687. {
  2688. if(s->picture_structure==PICT_FRAME){
  2689. s->dest[0] += s->mb_y * linesize << mb_size;
  2690. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2691. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2692. }else{
  2693. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2694. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2695. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2696. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2697. }
  2698. }
  2699. }
  2700. /**
  2701. * Permute an 8x8 block.
  2702. * @param block the block which will be permuted according to the given permutation vector
  2703. * @param permutation the permutation vector
  2704. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2705. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2706. * (inverse) permutated to scantable order!
  2707. */
  2708. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2709. {
  2710. int i;
  2711. int16_t temp[64];
  2712. if(last<=0) return;
  2713. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2714. for(i=0; i<=last; i++){
  2715. const int j= scantable[i];
  2716. temp[j]= block[j];
  2717. block[j]=0;
  2718. }
  2719. for(i=0; i<=last; i++){
  2720. const int j= scantable[i];
  2721. const int perm_j= permutation[j];
  2722. block[perm_j]= temp[j];
  2723. }
  2724. }
  2725. void ff_mpeg_flush(AVCodecContext *avctx){
  2726. int i;
  2727. MpegEncContext *s = avctx->priv_data;
  2728. if(s==NULL || s->picture==NULL)
  2729. return;
  2730. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2731. ff_mpeg_unref_picture(s, &s->picture[i]);
  2732. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2733. s->mb_x= s->mb_y= 0;
  2734. s->closed_gop= 0;
  2735. s->parse_context.state= -1;
  2736. s->parse_context.frame_start_found= 0;
  2737. s->parse_context.overread= 0;
  2738. s->parse_context.overread_index= 0;
  2739. s->parse_context.index= 0;
  2740. s->parse_context.last_index= 0;
  2741. s->bitstream_buffer_size=0;
  2742. s->pp_time=0;
  2743. }
  2744. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2745. int16_t *block, int n, int qscale)
  2746. {
  2747. int i, level, nCoeffs;
  2748. const uint16_t *quant_matrix;
  2749. nCoeffs= s->block_last_index[n];
  2750. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2751. /* XXX: only mpeg1 */
  2752. quant_matrix = s->intra_matrix;
  2753. for(i=1;i<=nCoeffs;i++) {
  2754. int j= s->intra_scantable.permutated[i];
  2755. level = block[j];
  2756. if (level) {
  2757. if (level < 0) {
  2758. level = -level;
  2759. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2760. level = (level - 1) | 1;
  2761. level = -level;
  2762. } else {
  2763. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2764. level = (level - 1) | 1;
  2765. }
  2766. block[j] = level;
  2767. }
  2768. }
  2769. }
  2770. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2771. int16_t *block, int n, int qscale)
  2772. {
  2773. int i, level, nCoeffs;
  2774. const uint16_t *quant_matrix;
  2775. nCoeffs= s->block_last_index[n];
  2776. quant_matrix = s->inter_matrix;
  2777. for(i=0; i<=nCoeffs; i++) {
  2778. int j= s->intra_scantable.permutated[i];
  2779. level = block[j];
  2780. if (level) {
  2781. if (level < 0) {
  2782. level = -level;
  2783. level = (((level << 1) + 1) * qscale *
  2784. ((int) (quant_matrix[j]))) >> 4;
  2785. level = (level - 1) | 1;
  2786. level = -level;
  2787. } else {
  2788. level = (((level << 1) + 1) * qscale *
  2789. ((int) (quant_matrix[j]))) >> 4;
  2790. level = (level - 1) | 1;
  2791. }
  2792. block[j] = level;
  2793. }
  2794. }
  2795. }
  2796. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2797. int16_t *block, int n, int qscale)
  2798. {
  2799. int i, level, nCoeffs;
  2800. const uint16_t *quant_matrix;
  2801. if(s->alternate_scan) nCoeffs= 63;
  2802. else nCoeffs= s->block_last_index[n];
  2803. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2804. quant_matrix = s->intra_matrix;
  2805. for(i=1;i<=nCoeffs;i++) {
  2806. int j= s->intra_scantable.permutated[i];
  2807. level = block[j];
  2808. if (level) {
  2809. if (level < 0) {
  2810. level = -level;
  2811. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2812. level = -level;
  2813. } else {
  2814. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2815. }
  2816. block[j] = level;
  2817. }
  2818. }
  2819. }
  2820. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2821. int16_t *block, int n, int qscale)
  2822. {
  2823. int i, level, nCoeffs;
  2824. const uint16_t *quant_matrix;
  2825. int sum=-1;
  2826. if(s->alternate_scan) nCoeffs= 63;
  2827. else nCoeffs= s->block_last_index[n];
  2828. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2829. sum += block[0];
  2830. quant_matrix = s->intra_matrix;
  2831. for(i=1;i<=nCoeffs;i++) {
  2832. int j= s->intra_scantable.permutated[i];
  2833. level = block[j];
  2834. if (level) {
  2835. if (level < 0) {
  2836. level = -level;
  2837. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2838. level = -level;
  2839. } else {
  2840. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2841. }
  2842. block[j] = level;
  2843. sum+=level;
  2844. }
  2845. }
  2846. block[63]^=sum&1;
  2847. }
  2848. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2849. int16_t *block, int n, int qscale)
  2850. {
  2851. int i, level, nCoeffs;
  2852. const uint16_t *quant_matrix;
  2853. int sum=-1;
  2854. if(s->alternate_scan) nCoeffs= 63;
  2855. else nCoeffs= s->block_last_index[n];
  2856. quant_matrix = s->inter_matrix;
  2857. for(i=0; i<=nCoeffs; i++) {
  2858. int j= s->intra_scantable.permutated[i];
  2859. level = block[j];
  2860. if (level) {
  2861. if (level < 0) {
  2862. level = -level;
  2863. level = (((level << 1) + 1) * qscale *
  2864. ((int) (quant_matrix[j]))) >> 4;
  2865. level = -level;
  2866. } else {
  2867. level = (((level << 1) + 1) * qscale *
  2868. ((int) (quant_matrix[j]))) >> 4;
  2869. }
  2870. block[j] = level;
  2871. sum+=level;
  2872. }
  2873. }
  2874. block[63]^=sum&1;
  2875. }
  2876. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2877. int16_t *block, int n, int qscale)
  2878. {
  2879. int i, level, qmul, qadd;
  2880. int nCoeffs;
  2881. assert(s->block_last_index[n]>=0);
  2882. qmul = qscale << 1;
  2883. if (!s->h263_aic) {
  2884. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  2885. qadd = (qscale - 1) | 1;
  2886. }else{
  2887. qadd = 0;
  2888. }
  2889. if(s->ac_pred)
  2890. nCoeffs=63;
  2891. else
  2892. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2893. for(i=1; i<=nCoeffs; i++) {
  2894. level = block[i];
  2895. if (level) {
  2896. if (level < 0) {
  2897. level = level * qmul - qadd;
  2898. } else {
  2899. level = level * qmul + qadd;
  2900. }
  2901. block[i] = level;
  2902. }
  2903. }
  2904. }
  2905. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2906. int16_t *block, int n, int qscale)
  2907. {
  2908. int i, level, qmul, qadd;
  2909. int nCoeffs;
  2910. assert(s->block_last_index[n]>=0);
  2911. qadd = (qscale - 1) | 1;
  2912. qmul = qscale << 1;
  2913. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2914. for(i=0; i<=nCoeffs; i++) {
  2915. level = block[i];
  2916. if (level) {
  2917. if (level < 0) {
  2918. level = level * qmul - qadd;
  2919. } else {
  2920. level = level * qmul + qadd;
  2921. }
  2922. block[i] = level;
  2923. }
  2924. }
  2925. }
  2926. /**
  2927. * set qscale and update qscale dependent variables.
  2928. */
  2929. void ff_set_qscale(MpegEncContext * s, int qscale)
  2930. {
  2931. if (qscale < 1)
  2932. qscale = 1;
  2933. else if (qscale > 31)
  2934. qscale = 31;
  2935. s->qscale = qscale;
  2936. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2937. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2938. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2939. }
  2940. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2941. {
  2942. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2943. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2944. }
  2945. #if CONFIG_ERROR_RESILIENCE
  2946. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2947. {
  2948. ERContext *er = &s->er;
  2949. er->cur_pic = s->current_picture_ptr;
  2950. er->last_pic = s->last_picture_ptr;
  2951. er->next_pic = s->next_picture_ptr;
  2952. er->pp_time = s->pp_time;
  2953. er->pb_time = s->pb_time;
  2954. er->quarter_sample = s->quarter_sample;
  2955. er->partitioned_frame = s->partitioned_frame;
  2956. ff_er_frame_start(er);
  2957. }
  2958. #endif /* CONFIG_ERROR_RESILIENCE */