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.

2744 lines
99KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; 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/imgutils.h"
  29. #include "avcodec.h"
  30. #include "dsputil.h"
  31. #include "internal.h"
  32. #include "mathops.h"
  33. #include "mpegvideo.h"
  34. #include "mjpegenc.h"
  35. #include "msmpeg4.h"
  36. #include "xvmc_internal.h"
  37. #include "thread.h"
  38. #include <limits.h>
  39. //#undef NDEBUG
  40. //#include <assert.h>
  41. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  42. DCTELEM *block, int n, int qscale);
  43. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. /* enable all paranoid tests for rounding, overflows, etc... */
  56. //#define PARANOID
  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. const enum AVPixelFormat ff_hwaccel_pixfmt_list_420[] = {
  118. AV_PIX_FMT_DXVA2_VLD,
  119. AV_PIX_FMT_VAAPI_VLD,
  120. AV_PIX_FMT_VDA_VLD,
  121. AV_PIX_FMT_YUV420P,
  122. AV_PIX_FMT_NONE
  123. };
  124. const uint8_t *avpriv_mpv_find_start_code(const uint8_t *restrict p,
  125. const uint8_t *end,
  126. uint32_t * restrict state)
  127. {
  128. int i;
  129. assert(p <= end);
  130. if (p >= end)
  131. return end;
  132. for (i = 0; i < 3; i++) {
  133. uint32_t tmp = *state << 8;
  134. *state = tmp + *(p++);
  135. if (tmp == 0x100 || p == end)
  136. return p;
  137. }
  138. while (p < end) {
  139. if (p[-1] > 1 ) p += 3;
  140. else if (p[-2] ) p += 2;
  141. else if (p[-3]|(p[-1]-1)) p++;
  142. else {
  143. p++;
  144. break;
  145. }
  146. }
  147. p = FFMIN(p, end) - 4;
  148. *state = AV_RB32(p);
  149. return p + 4;
  150. }
  151. /* init common dct for both encoder and decoder */
  152. av_cold int ff_dct_common_init(MpegEncContext *s)
  153. {
  154. ff_dsputil_init(&s->dsp, s->avctx);
  155. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  156. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  157. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  158. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  159. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  160. if (s->flags & CODEC_FLAG_BITEXACT)
  161. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  162. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  163. #if ARCH_X86
  164. ff_MPV_common_init_x86(s);
  165. #elif ARCH_ALPHA
  166. ff_MPV_common_init_axp(s);
  167. #elif ARCH_ARM
  168. ff_MPV_common_init_arm(s);
  169. #elif HAVE_ALTIVEC
  170. ff_MPV_common_init_altivec(s);
  171. #elif ARCH_BFIN
  172. ff_MPV_common_init_bfin(s);
  173. #endif
  174. /* load & permutate scantables
  175. * note: only wmv uses different ones
  176. */
  177. if (s->alternate_scan) {
  178. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  179. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  180. } else {
  181. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  182. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  183. }
  184. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  185. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  186. return 0;
  187. }
  188. void ff_copy_picture(Picture *dst, Picture *src)
  189. {
  190. *dst = *src;
  191. dst->f.type = FF_BUFFER_TYPE_COPY;
  192. }
  193. /**
  194. * Release a frame buffer
  195. */
  196. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  197. {
  198. /* WM Image / Screen codecs allocate internal buffers with different
  199. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  200. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  201. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  202. s->codec_id != AV_CODEC_ID_MSS2)
  203. ff_thread_release_buffer(s->avctx, &pic->f);
  204. else
  205. avcodec_default_release_buffer(s->avctx, &pic->f);
  206. av_freep(&pic->f.hwaccel_picture_private);
  207. }
  208. int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
  209. {
  210. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  211. // edge emu needs blocksize + filter length - 1
  212. // (= 17x17 for halfpel / 21x21 for h264)
  213. // linesize * interlaced * MBsize
  214. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 21,
  215. fail);
  216. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 2,
  217. fail)
  218. s->me.temp = s->me.scratchpad;
  219. s->rd_scratchpad = s->me.scratchpad;
  220. s->b_scratchpad = s->me.scratchpad;
  221. s->obmc_scratchpad = s->me.scratchpad + 16;
  222. return 0;
  223. fail:
  224. av_freep(&s->edge_emu_buffer);
  225. return AVERROR(ENOMEM);
  226. }
  227. /**
  228. * Allocate a frame buffer
  229. */
  230. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  231. {
  232. int r, ret;
  233. if (s->avctx->hwaccel) {
  234. assert(!pic->f.hwaccel_picture_private);
  235. if (s->avctx->hwaccel->priv_data_size) {
  236. pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  237. if (!pic->f.hwaccel_picture_private) {
  238. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  239. return -1;
  240. }
  241. }
  242. }
  243. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  244. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  245. s->codec_id != AV_CODEC_ID_MSS2)
  246. r = ff_thread_get_buffer(s->avctx, &pic->f);
  247. else
  248. r = avcodec_default_get_buffer(s->avctx, &pic->f);
  249. if (r < 0 || !pic->f.type || !pic->f.data[0]) {
  250. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
  251. r, pic->f.type, pic->f.data[0]);
  252. av_freep(&pic->f.hwaccel_picture_private);
  253. return -1;
  254. }
  255. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  256. s->uvlinesize != pic->f.linesize[1])) {
  257. av_log(s->avctx, AV_LOG_ERROR,
  258. "get_buffer() failed (stride changed)\n");
  259. free_frame_buffer(s, pic);
  260. return -1;
  261. }
  262. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  263. av_log(s->avctx, AV_LOG_ERROR,
  264. "get_buffer() failed (uv stride mismatch)\n");
  265. free_frame_buffer(s, pic);
  266. return -1;
  267. }
  268. if (!s->edge_emu_buffer &&
  269. (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  270. av_log(s->avctx, AV_LOG_ERROR,
  271. "get_buffer() failed to allocate context scratch buffers.\n");
  272. free_frame_buffer(s, pic);
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. /**
  278. * Allocate a Picture.
  279. * The pixels are allocated/set by calling get_buffer() if shared = 0
  280. */
  281. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  282. {
  283. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  284. // the + 1 is needed so memset(,,stride*height) does not sig11
  285. const int mb_array_size = s->mb_stride * s->mb_height;
  286. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  287. const int b4_array_size = s->b4_stride * s->mb_height * 4;
  288. int i;
  289. int r = -1;
  290. if (shared) {
  291. assert(pic->f.data[0]);
  292. assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
  293. pic->f.type = FF_BUFFER_TYPE_SHARED;
  294. } else {
  295. assert(!pic->f.data[0]);
  296. if (alloc_frame_buffer(s, pic) < 0)
  297. return -1;
  298. s->linesize = pic->f.linesize[0];
  299. s->uvlinesize = pic->f.linesize[1];
  300. }
  301. if (pic->f.qscale_table == NULL) {
  302. if (s->encoding) {
  303. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var,
  304. mb_array_size * sizeof(int16_t), fail)
  305. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var,
  306. mb_array_size * sizeof(int16_t), fail)
  307. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean,
  308. mb_array_size * sizeof(int8_t ), fail)
  309. }
  310. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table,
  311. mb_array_size * sizeof(uint8_t) + 2, fail)// the + 2 is for the slice end check
  312. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base,
  313. (big_mb_num + s->mb_stride) * sizeof(uint8_t),
  314. fail)
  315. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base,
  316. (big_mb_num + s->mb_stride) * sizeof(uint32_t),
  317. fail)
  318. pic->f.mb_type = pic->mb_type_base + 2 * s->mb_stride + 1;
  319. pic->f.qscale_table = pic->qscale_table_base + 2 * s->mb_stride + 1;
  320. if (s->out_format == FMT_H264) {
  321. for (i = 0; i < 2; i++) {
  322. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
  323. 2 * (b4_array_size + 4) * sizeof(int16_t),
  324. fail)
  325. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  326. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
  327. 4 * mb_array_size * sizeof(uint8_t), fail)
  328. }
  329. pic->f.motion_subsample_log2 = 2;
  330. } else if (s->out_format == FMT_H263 || s->encoding ||
  331. (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
  332. for (i = 0; i < 2; i++) {
  333. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
  334. 2 * (b8_array_size + 4) * sizeof(int16_t),
  335. fail)
  336. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  337. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
  338. 4 * mb_array_size * sizeof(uint8_t), fail)
  339. }
  340. pic->f.motion_subsample_log2 = 3;
  341. }
  342. if (s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  343. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff,
  344. 64 * mb_array_size * sizeof(DCTELEM) * 6, fail)
  345. }
  346. pic->f.qstride = s->mb_stride;
  347. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
  348. 1 * sizeof(AVPanScan), fail)
  349. }
  350. pic->owner2 = s;
  351. return 0;
  352. fail: // for the FF_ALLOCZ_OR_GOTO macro
  353. if (r >= 0)
  354. free_frame_buffer(s, pic);
  355. return -1;
  356. }
  357. /**
  358. * Deallocate a picture.
  359. */
  360. static void free_picture(MpegEncContext *s, Picture *pic)
  361. {
  362. int i;
  363. if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
  364. free_frame_buffer(s, pic);
  365. }
  366. av_freep(&pic->mb_var);
  367. av_freep(&pic->mc_mb_var);
  368. av_freep(&pic->mb_mean);
  369. av_freep(&pic->f.mbskip_table);
  370. av_freep(&pic->qscale_table_base);
  371. pic->f.qscale_table = NULL;
  372. av_freep(&pic->mb_type_base);
  373. pic->f.mb_type = NULL;
  374. av_freep(&pic->f.dct_coeff);
  375. av_freep(&pic->f.pan_scan);
  376. pic->f.mb_type = NULL;
  377. for (i = 0; i < 2; i++) {
  378. av_freep(&pic->motion_val_base[i]);
  379. av_freep(&pic->f.ref_index[i]);
  380. pic->f.motion_val[i] = NULL;
  381. }
  382. if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
  383. for (i = 0; i < 4; i++) {
  384. pic->f.base[i] =
  385. pic->f.data[i] = NULL;
  386. }
  387. pic->f.type = 0;
  388. }
  389. }
  390. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base)
  391. {
  392. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  393. int c_size = s->mb_stride * (s->mb_height + 1);
  394. int yc_size = y_size + 2 * c_size;
  395. int i;
  396. s->edge_emu_buffer =
  397. s->me.scratchpad =
  398. s->me.temp =
  399. s->rd_scratchpad =
  400. s->b_scratchpad =
  401. s->obmc_scratchpad = NULL;
  402. if (s->encoding) {
  403. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  404. ME_MAP_SIZE * sizeof(uint32_t), fail)
  405. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  406. ME_MAP_SIZE * sizeof(uint32_t), fail)
  407. if (s->avctx->noise_reduction) {
  408. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  409. 2 * 64 * sizeof(int), fail)
  410. }
  411. }
  412. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(DCTELEM), fail)
  413. s->block = s->blocks[0];
  414. for (i = 0; i < 12; i++) {
  415. s->pblocks[i] = &s->block[i];
  416. }
  417. if (s->out_format == FMT_H263) {
  418. /* ac values */
  419. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  420. yc_size * sizeof(int16_t) * 16, fail);
  421. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  422. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  423. s->ac_val[2] = s->ac_val[1] + c_size;
  424. }
  425. return 0;
  426. fail:
  427. return -1; // free() through ff_MPV_common_end()
  428. }
  429. static void free_duplicate_context(MpegEncContext *s)
  430. {
  431. if (s == NULL)
  432. return;
  433. av_freep(&s->edge_emu_buffer);
  434. av_freep(&s->me.scratchpad);
  435. s->me.temp =
  436. s->rd_scratchpad =
  437. s->b_scratchpad =
  438. s->obmc_scratchpad = NULL;
  439. av_freep(&s->dct_error_sum);
  440. av_freep(&s->me.map);
  441. av_freep(&s->me.score_map);
  442. av_freep(&s->blocks);
  443. av_freep(&s->ac_val_base);
  444. s->block = NULL;
  445. }
  446. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  447. {
  448. #define COPY(a) bak->a = src->a
  449. COPY(edge_emu_buffer);
  450. COPY(me.scratchpad);
  451. COPY(me.temp);
  452. COPY(rd_scratchpad);
  453. COPY(b_scratchpad);
  454. COPY(obmc_scratchpad);
  455. COPY(me.map);
  456. COPY(me.score_map);
  457. COPY(blocks);
  458. COPY(block);
  459. COPY(start_mb_y);
  460. COPY(end_mb_y);
  461. COPY(me.map_generation);
  462. COPY(pb);
  463. COPY(dct_error_sum);
  464. COPY(dct_count[0]);
  465. COPY(dct_count[1]);
  466. COPY(ac_val_base);
  467. COPY(ac_val[0]);
  468. COPY(ac_val[1]);
  469. COPY(ac_val[2]);
  470. #undef COPY
  471. }
  472. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  473. {
  474. MpegEncContext bak;
  475. int i, ret;
  476. // FIXME copy only needed parts
  477. // START_TIMER
  478. backup_duplicate_context(&bak, dst);
  479. memcpy(dst, src, sizeof(MpegEncContext));
  480. backup_duplicate_context(dst, &bak);
  481. for (i = 0; i < 12; i++) {
  482. dst->pblocks[i] = &dst->block[i];
  483. }
  484. if (!dst->edge_emu_buffer &&
  485. (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
  486. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  487. "scratch buffers.\n");
  488. return ret;
  489. }
  490. // STOP_TIMER("update_duplicate_context")
  491. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  492. return 0;
  493. }
  494. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  495. const AVCodecContext *src)
  496. {
  497. int i;
  498. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  499. if (dst == src || !s1->context_initialized)
  500. return 0;
  501. // FIXME can parameters change on I-frames?
  502. // in that case dst may need a reinit
  503. if (!s->context_initialized) {
  504. memcpy(s, s1, sizeof(MpegEncContext));
  505. s->avctx = dst;
  506. s->picture_range_start += MAX_PICTURE_COUNT;
  507. s->picture_range_end += MAX_PICTURE_COUNT;
  508. s->bitstream_buffer = NULL;
  509. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  510. ff_MPV_common_init(s);
  511. }
  512. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  513. int err;
  514. s->context_reinit = 0;
  515. s->height = s1->height;
  516. s->width = s1->width;
  517. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  518. return err;
  519. }
  520. s->avctx->coded_height = s1->avctx->coded_height;
  521. s->avctx->coded_width = s1->avctx->coded_width;
  522. s->avctx->width = s1->avctx->width;
  523. s->avctx->height = s1->avctx->height;
  524. s->coded_picture_number = s1->coded_picture_number;
  525. s->picture_number = s1->picture_number;
  526. s->input_picture_number = s1->input_picture_number;
  527. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  528. memcpy(&s->last_picture, &s1->last_picture,
  529. (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
  530. // reset s->picture[].f.extended_data to s->picture[].f.data
  531. for (i = 0; i < s->picture_count; i++)
  532. s->picture[i].f.extended_data = s->picture[i].f.data;
  533. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  534. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  535. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  536. // Error/bug resilience
  537. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  538. s->workaround_bugs = s1->workaround_bugs;
  539. // MPEG4 timing info
  540. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  541. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  542. // B-frame info
  543. s->max_b_frames = s1->max_b_frames;
  544. s->low_delay = s1->low_delay;
  545. s->droppable = s1->droppable;
  546. // DivX handling (doesn't work)
  547. s->divx_packed = s1->divx_packed;
  548. if (s1->bitstream_buffer) {
  549. if (s1->bitstream_buffer_size +
  550. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  551. av_fast_malloc(&s->bitstream_buffer,
  552. &s->allocated_bitstream_buffer_size,
  553. s1->allocated_bitstream_buffer_size);
  554. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  555. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  556. s1->bitstream_buffer_size);
  557. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  558. FF_INPUT_BUFFER_PADDING_SIZE);
  559. }
  560. // linesize dependend scratch buffer allocation
  561. if (!s->edge_emu_buffer)
  562. if (s1->linesize) {
  563. if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
  564. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  565. "scratch buffers.\n");
  566. return AVERROR(ENOMEM);
  567. }
  568. } else {
  569. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  570. "be allocated due to unknown size.\n");
  571. return AVERROR_BUG;
  572. }
  573. // MPEG2/interlacing info
  574. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  575. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  576. if (!s1->first_field) {
  577. s->last_pict_type = s1->pict_type;
  578. if (s1->current_picture_ptr)
  579. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  580. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  581. s->last_non_b_pict_type = s1->pict_type;
  582. }
  583. }
  584. return 0;
  585. }
  586. /**
  587. * Set the given MpegEncContext to common defaults
  588. * (same for encoding and decoding).
  589. * The changed fields will not depend upon the
  590. * prior state of the MpegEncContext.
  591. */
  592. void ff_MPV_common_defaults(MpegEncContext *s)
  593. {
  594. s->y_dc_scale_table =
  595. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  596. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  597. s->progressive_frame = 1;
  598. s->progressive_sequence = 1;
  599. s->picture_structure = PICT_FRAME;
  600. s->coded_picture_number = 0;
  601. s->picture_number = 0;
  602. s->input_picture_number = 0;
  603. s->picture_in_gop_number = 0;
  604. s->f_code = 1;
  605. s->b_code = 1;
  606. s->picture_range_start = 0;
  607. s->picture_range_end = MAX_PICTURE_COUNT;
  608. s->slice_context_count = 1;
  609. }
  610. /**
  611. * Set the given MpegEncContext to defaults for decoding.
  612. * the changed fields will not depend upon
  613. * the prior state of the MpegEncContext.
  614. */
  615. void ff_MPV_decode_defaults(MpegEncContext *s)
  616. {
  617. ff_MPV_common_defaults(s);
  618. }
  619. /**
  620. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  621. */
  622. static int init_context_frame(MpegEncContext *s)
  623. {
  624. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  625. s->mb_width = (s->width + 15) / 16;
  626. s->mb_stride = s->mb_width + 1;
  627. s->b8_stride = s->mb_width * 2 + 1;
  628. s->b4_stride = s->mb_width * 4 + 1;
  629. mb_array_size = s->mb_height * s->mb_stride;
  630. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  631. /* set default edge pos, will be overriden
  632. * in decode_header if needed */
  633. s->h_edge_pos = s->mb_width * 16;
  634. s->v_edge_pos = s->mb_height * 16;
  635. s->mb_num = s->mb_width * s->mb_height;
  636. s->block_wrap[0] =
  637. s->block_wrap[1] =
  638. s->block_wrap[2] =
  639. s->block_wrap[3] = s->b8_stride;
  640. s->block_wrap[4] =
  641. s->block_wrap[5] = s->mb_stride;
  642. y_size = s->b8_stride * (2 * s->mb_height + 1);
  643. c_size = s->mb_stride * (s->mb_height + 1);
  644. yc_size = y_size + 2 * c_size;
  645. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  646. fail); // error ressilience code looks cleaner with this
  647. for (y = 0; y < s->mb_height; y++)
  648. for (x = 0; x < s->mb_width; x++)
  649. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  650. s->mb_index2xy[s->mb_height * s->mb_width] =
  651. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  652. if (s->encoding) {
  653. /* Allocate MV tables */
  654. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  655. mv_table_size * 2 * sizeof(int16_t), fail);
  656. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  657. mv_table_size * 2 * sizeof(int16_t), fail);
  658. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  659. mv_table_size * 2 * sizeof(int16_t), fail);
  660. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  661. mv_table_size * 2 * sizeof(int16_t), fail);
  662. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  663. mv_table_size * 2 * sizeof(int16_t), fail);
  664. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  665. mv_table_size * 2 * sizeof(int16_t), fail);
  666. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  667. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  668. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  669. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  670. s->mb_stride + 1;
  671. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  672. s->mb_stride + 1;
  673. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  674. /* Allocate MB type table */
  675. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  676. sizeof(uint16_t), fail); // needed for encoding
  677. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  678. sizeof(int), fail);
  679. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  680. mb_array_size * sizeof(float), fail);
  681. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  682. mb_array_size * sizeof(float), fail);
  683. }
  684. FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
  685. mb_array_size * sizeof(uint8_t), fail);
  686. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
  687. mb_array_size * sizeof(uint8_t), fail);
  688. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  689. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  690. /* interlaced direct mode decoding tables */
  691. for (i = 0; i < 2; i++) {
  692. int j, k;
  693. for (j = 0; j < 2; j++) {
  694. for (k = 0; k < 2; k++) {
  695. FF_ALLOCZ_OR_GOTO(s->avctx,
  696. s->b_field_mv_table_base[i][j][k],
  697. mv_table_size * 2 * sizeof(int16_t),
  698. fail);
  699. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  700. s->mb_stride + 1;
  701. }
  702. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  703. mb_array_size * 2 * sizeof(uint8_t), fail);
  704. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  705. mv_table_size * 2 * sizeof(int16_t), fail);
  706. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  707. + s->mb_stride + 1;
  708. }
  709. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  710. mb_array_size * 2 * sizeof(uint8_t), fail);
  711. }
  712. }
  713. if (s->out_format == FMT_H263) {
  714. /* cbp values */
  715. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  716. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  717. /* cbp, ac_pred, pred_dir */
  718. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  719. mb_array_size * sizeof(uint8_t), fail);
  720. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  721. mb_array_size * sizeof(uint8_t), fail);
  722. }
  723. if (s->h263_pred || s->h263_plus || !s->encoding) {
  724. /* dc values */
  725. // MN: we need these for error resilience of intra-frames
  726. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  727. yc_size * sizeof(int16_t), fail);
  728. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  729. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  730. s->dc_val[2] = s->dc_val[1] + c_size;
  731. for (i = 0; i < yc_size; i++)
  732. s->dc_val_base[i] = 1024;
  733. }
  734. /* which mb is a intra block */
  735. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  736. memset(s->mbintra_table, 1, mb_array_size);
  737. /* init macroblock skip table */
  738. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  739. // Note the + 1 is for a quicker mpeg4 slice_end detection
  740. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  741. s->avctx->debug_mv) {
  742. s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
  743. 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
  744. s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
  745. 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
  746. s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
  747. 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
  748. }
  749. return 0;
  750. fail:
  751. return AVERROR(ENOMEM);
  752. }
  753. /**
  754. * init common structure for both encoder and decoder.
  755. * this assumes that some variables like width/height are already set
  756. */
  757. av_cold int ff_MPV_common_init(MpegEncContext *s)
  758. {
  759. int i;
  760. int nb_slices = (HAVE_THREADS &&
  761. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  762. s->avctx->thread_count : 1;
  763. if (s->encoding && s->avctx->slices)
  764. nb_slices = s->avctx->slices;
  765. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  766. s->mb_height = (s->height + 31) / 32 * 2;
  767. else if (s->codec_id != AV_CODEC_ID_H264)
  768. s->mb_height = (s->height + 15) / 16;
  769. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  770. av_log(s->avctx, AV_LOG_ERROR,
  771. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  772. return -1;
  773. }
  774. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  775. int max_slices;
  776. if (s->mb_height)
  777. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  778. else
  779. max_slices = MAX_THREADS;
  780. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  781. " reducing to %d\n", nb_slices, max_slices);
  782. nb_slices = max_slices;
  783. }
  784. if ((s->width || s->height) &&
  785. av_image_check_size(s->width, s->height, 0, s->avctx))
  786. return -1;
  787. ff_dct_common_init(s);
  788. s->flags = s->avctx->flags;
  789. s->flags2 = s->avctx->flags2;
  790. if (s->width && s->height) {
  791. /* set chroma shifts */
  792. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  793. &s->chroma_x_shift,
  794. &s->chroma_y_shift);
  795. /* convert fourcc to upper case */
  796. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  797. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  798. s->avctx->coded_frame = &s->current_picture.f;
  799. if (s->encoding) {
  800. if (s->msmpeg4_version) {
  801. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  802. 2 * 2 * (MAX_LEVEL + 1) *
  803. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  804. }
  805. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  806. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
  807. 64 * 32 * sizeof(int), fail);
  808. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
  809. 64 * 32 * sizeof(int), fail);
  810. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
  811. 64 * 32 * 2 * sizeof(uint16_t), fail);
  812. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
  813. 64 * 32 * 2 * sizeof(uint16_t), fail);
  814. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  815. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  816. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  817. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  818. if (s->avctx->noise_reduction) {
  819. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  820. 2 * 64 * sizeof(uint16_t), fail);
  821. }
  822. }
  823. }
  824. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  825. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  826. s->picture_count * sizeof(Picture), fail);
  827. for (i = 0; i < s->picture_count; i++) {
  828. avcodec_get_frame_defaults(&s->picture[i].f);
  829. }
  830. if (s->width && s->height) {
  831. if (init_context_frame(s))
  832. goto fail;
  833. s->parse_context.state = -1;
  834. }
  835. s->context_initialized = 1;
  836. s->thread_context[0] = s;
  837. if (s->width && s->height) {
  838. if (nb_slices > 1) {
  839. for (i = 1; i < nb_slices; i++) {
  840. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  841. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  842. }
  843. for (i = 0; i < nb_slices; i++) {
  844. if (init_duplicate_context(s->thread_context[i], s) < 0)
  845. goto fail;
  846. s->thread_context[i]->start_mb_y =
  847. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  848. s->thread_context[i]->end_mb_y =
  849. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  850. }
  851. } else {
  852. if (init_duplicate_context(s, s) < 0)
  853. goto fail;
  854. s->start_mb_y = 0;
  855. s->end_mb_y = s->mb_height;
  856. }
  857. s->slice_context_count = nb_slices;
  858. }
  859. return 0;
  860. fail:
  861. ff_MPV_common_end(s);
  862. return -1;
  863. }
  864. /**
  865. * Frees and resets MpegEncContext fields depending on the resolution.
  866. * Is used during resolution changes to avoid a full reinitialization of the
  867. * codec.
  868. */
  869. static int free_context_frame(MpegEncContext *s)
  870. {
  871. int i, j, k;
  872. av_freep(&s->mb_type);
  873. av_freep(&s->p_mv_table_base);
  874. av_freep(&s->b_forw_mv_table_base);
  875. av_freep(&s->b_back_mv_table_base);
  876. av_freep(&s->b_bidir_forw_mv_table_base);
  877. av_freep(&s->b_bidir_back_mv_table_base);
  878. av_freep(&s->b_direct_mv_table_base);
  879. s->p_mv_table = NULL;
  880. s->b_forw_mv_table = NULL;
  881. s->b_back_mv_table = NULL;
  882. s->b_bidir_forw_mv_table = NULL;
  883. s->b_bidir_back_mv_table = NULL;
  884. s->b_direct_mv_table = NULL;
  885. for (i = 0; i < 2; i++) {
  886. for (j = 0; j < 2; j++) {
  887. for (k = 0; k < 2; k++) {
  888. av_freep(&s->b_field_mv_table_base[i][j][k]);
  889. s->b_field_mv_table[i][j][k] = NULL;
  890. }
  891. av_freep(&s->b_field_select_table[i][j]);
  892. av_freep(&s->p_field_mv_table_base[i][j]);
  893. s->p_field_mv_table[i][j] = NULL;
  894. }
  895. av_freep(&s->p_field_select_table[i]);
  896. }
  897. av_freep(&s->dc_val_base);
  898. av_freep(&s->coded_block_base);
  899. av_freep(&s->mbintra_table);
  900. av_freep(&s->cbp_table);
  901. av_freep(&s->pred_dir_table);
  902. av_freep(&s->mbskip_table);
  903. av_freep(&s->error_status_table);
  904. av_freep(&s->er_temp_buffer);
  905. av_freep(&s->mb_index2xy);
  906. av_freep(&s->lambda_table);
  907. av_freep(&s->cplx_tab);
  908. av_freep(&s->bits_tab);
  909. s->linesize = s->uvlinesize = 0;
  910. for (i = 0; i < 3; i++)
  911. av_freep(&s->visualization_buffer[i]);
  912. return 0;
  913. }
  914. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  915. {
  916. int i, err = 0;
  917. if (s->slice_context_count > 1) {
  918. for (i = 0; i < s->slice_context_count; i++) {
  919. free_duplicate_context(s->thread_context[i]);
  920. }
  921. for (i = 1; i < s->slice_context_count; i++) {
  922. av_freep(&s->thread_context[i]);
  923. }
  924. } else
  925. free_duplicate_context(s);
  926. free_context_frame(s);
  927. if (s->picture)
  928. for (i = 0; i < s->picture_count; i++) {
  929. s->picture[i].needs_realloc = 1;
  930. }
  931. s->last_picture_ptr =
  932. s->next_picture_ptr =
  933. s->current_picture_ptr = NULL;
  934. // init
  935. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  936. s->mb_height = (s->height + 31) / 32 * 2;
  937. else if (s->codec_id != AV_CODEC_ID_H264)
  938. s->mb_height = (s->height + 15) / 16;
  939. if ((s->width || s->height) &&
  940. av_image_check_size(s->width, s->height, 0, s->avctx))
  941. return AVERROR_INVALIDDATA;
  942. if ((err = init_context_frame(s)))
  943. goto fail;
  944. s->thread_context[0] = s;
  945. if (s->width && s->height) {
  946. int nb_slices = s->slice_context_count;
  947. if (nb_slices > 1) {
  948. for (i = 1; i < nb_slices; i++) {
  949. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  950. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  951. }
  952. for (i = 0; i < nb_slices; i++) {
  953. if (init_duplicate_context(s->thread_context[i], s) < 0)
  954. goto fail;
  955. s->thread_context[i]->start_mb_y =
  956. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  957. s->thread_context[i]->end_mb_y =
  958. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  959. }
  960. } else {
  961. if (init_duplicate_context(s, s) < 0)
  962. goto fail;
  963. s->start_mb_y = 0;
  964. s->end_mb_y = s->mb_height;
  965. }
  966. s->slice_context_count = nb_slices;
  967. }
  968. return 0;
  969. fail:
  970. ff_MPV_common_end(s);
  971. return err;
  972. }
  973. /* init common structure for both encoder and decoder */
  974. void ff_MPV_common_end(MpegEncContext *s)
  975. {
  976. int i;
  977. if (s->slice_context_count > 1) {
  978. for (i = 0; i < s->slice_context_count; i++) {
  979. free_duplicate_context(s->thread_context[i]);
  980. }
  981. for (i = 1; i < s->slice_context_count; i++) {
  982. av_freep(&s->thread_context[i]);
  983. }
  984. s->slice_context_count = 1;
  985. } else free_duplicate_context(s);
  986. av_freep(&s->parse_context.buffer);
  987. s->parse_context.buffer_size = 0;
  988. av_freep(&s->bitstream_buffer);
  989. s->allocated_bitstream_buffer_size = 0;
  990. av_freep(&s->avctx->stats_out);
  991. av_freep(&s->ac_stats);
  992. av_freep(&s->q_intra_matrix);
  993. av_freep(&s->q_inter_matrix);
  994. av_freep(&s->q_intra_matrix16);
  995. av_freep(&s->q_inter_matrix16);
  996. av_freep(&s->input_picture);
  997. av_freep(&s->reordered_input_picture);
  998. av_freep(&s->dct_offset);
  999. if (s->picture && !s->avctx->internal->is_copy) {
  1000. for (i = 0; i < s->picture_count; i++) {
  1001. free_picture(s, &s->picture[i]);
  1002. }
  1003. }
  1004. av_freep(&s->picture);
  1005. free_context_frame(s);
  1006. if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
  1007. avcodec_default_free_buffers(s->avctx);
  1008. s->context_initialized = 0;
  1009. s->last_picture_ptr =
  1010. s->next_picture_ptr =
  1011. s->current_picture_ptr = NULL;
  1012. s->linesize = s->uvlinesize = 0;
  1013. }
  1014. void ff_init_rl(RLTable *rl,
  1015. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1016. {
  1017. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1018. uint8_t index_run[MAX_RUN + 1];
  1019. int last, run, level, start, end, i;
  1020. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1021. if (static_store && rl->max_level[0])
  1022. return;
  1023. /* compute max_level[], max_run[] and index_run[] */
  1024. for (last = 0; last < 2; last++) {
  1025. if (last == 0) {
  1026. start = 0;
  1027. end = rl->last;
  1028. } else {
  1029. start = rl->last;
  1030. end = rl->n;
  1031. }
  1032. memset(max_level, 0, MAX_RUN + 1);
  1033. memset(max_run, 0, MAX_LEVEL + 1);
  1034. memset(index_run, rl->n, MAX_RUN + 1);
  1035. for (i = start; i < end; i++) {
  1036. run = rl->table_run[i];
  1037. level = rl->table_level[i];
  1038. if (index_run[run] == rl->n)
  1039. index_run[run] = i;
  1040. if (level > max_level[run])
  1041. max_level[run] = level;
  1042. if (run > max_run[level])
  1043. max_run[level] = run;
  1044. }
  1045. if (static_store)
  1046. rl->max_level[last] = static_store[last];
  1047. else
  1048. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1049. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1050. if (static_store)
  1051. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1052. else
  1053. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1054. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1055. if (static_store)
  1056. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1057. else
  1058. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1059. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1060. }
  1061. }
  1062. void ff_init_vlc_rl(RLTable *rl)
  1063. {
  1064. int i, q;
  1065. for (q = 0; q < 32; q++) {
  1066. int qmul = q * 2;
  1067. int qadd = (q - 1) | 1;
  1068. if (q == 0) {
  1069. qmul = 1;
  1070. qadd = 0;
  1071. }
  1072. for (i = 0; i < rl->vlc.table_size; i++) {
  1073. int code = rl->vlc.table[i][0];
  1074. int len = rl->vlc.table[i][1];
  1075. int level, run;
  1076. if (len == 0) { // illegal code
  1077. run = 66;
  1078. level = MAX_LEVEL;
  1079. } else if (len < 0) { // more bits needed
  1080. run = 0;
  1081. level = code;
  1082. } else {
  1083. if (code == rl->n) { // esc
  1084. run = 66;
  1085. level = 0;
  1086. } else {
  1087. run = rl->table_run[code] + 1;
  1088. level = rl->table_level[code] * qmul + qadd;
  1089. if (code >= rl->last) run += 192;
  1090. }
  1091. }
  1092. rl->rl_vlc[q][i].len = len;
  1093. rl->rl_vlc[q][i].level = level;
  1094. rl->rl_vlc[q][i].run = run;
  1095. }
  1096. }
  1097. }
  1098. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1099. {
  1100. int i;
  1101. /* release non reference frames */
  1102. for (i = 0; i < s->picture_count; i++) {
  1103. if (s->picture[i].f.data[0] && !s->picture[i].f.reference &&
  1104. (!s->picture[i].owner2 || s->picture[i].owner2 == s) &&
  1105. (remove_current || &s->picture[i] != s->current_picture_ptr)
  1106. /* && s->picture[i].type!= FF_BUFFER_TYPE_SHARED */) {
  1107. free_frame_buffer(s, &s->picture[i]);
  1108. }
  1109. }
  1110. }
  1111. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1112. {
  1113. if (pic->f.data[0] == NULL)
  1114. return 1;
  1115. if (pic->needs_realloc && !(pic->f.reference & DELAYED_PIC_REF))
  1116. if (!pic->owner2 || pic->owner2 == s)
  1117. return 1;
  1118. return 0;
  1119. }
  1120. static int find_unused_picture(MpegEncContext *s, int shared)
  1121. {
  1122. int i;
  1123. if (shared) {
  1124. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  1125. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
  1126. return i;
  1127. }
  1128. } else {
  1129. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  1130. if (pic_is_unused(s, &s->picture[i]) && s->picture[i].f.type != 0)
  1131. return i; // FIXME
  1132. }
  1133. for (i = s->picture_range_start; i < s->picture_range_end; i++) {
  1134. if (pic_is_unused(s, &s->picture[i]))
  1135. return i;
  1136. }
  1137. }
  1138. return AVERROR_INVALIDDATA;
  1139. }
  1140. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1141. {
  1142. int ret = find_unused_picture(s, shared);
  1143. if (ret >= 0 && ret < s->picture_range_end) {
  1144. if (s->picture[ret].needs_realloc) {
  1145. s->picture[ret].needs_realloc = 0;
  1146. free_picture(s, &s->picture[ret]);
  1147. avcodec_get_frame_defaults(&s->picture[ret].f);
  1148. }
  1149. }
  1150. return ret;
  1151. }
  1152. static void update_noise_reduction(MpegEncContext *s)
  1153. {
  1154. int intra, i;
  1155. for (intra = 0; intra < 2; intra++) {
  1156. if (s->dct_count[intra] > (1 << 16)) {
  1157. for (i = 0; i < 64; i++) {
  1158. s->dct_error_sum[intra][i] >>= 1;
  1159. }
  1160. s->dct_count[intra] >>= 1;
  1161. }
  1162. for (i = 0; i < 64; i++) {
  1163. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1164. s->dct_count[intra] +
  1165. s->dct_error_sum[intra][i] / 2) /
  1166. (s->dct_error_sum[intra][i] + 1);
  1167. }
  1168. }
  1169. }
  1170. /**
  1171. * generic function for encode/decode called after coding/decoding
  1172. * the header and before a frame is coded/decoded.
  1173. */
  1174. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1175. {
  1176. int i;
  1177. Picture *pic;
  1178. s->mb_skipped = 0;
  1179. /* mark & release old frames */
  1180. if (s->out_format != FMT_H264 || s->codec_id == AV_CODEC_ID_SVQ3) {
  1181. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1182. s->last_picture_ptr != s->next_picture_ptr &&
  1183. s->last_picture_ptr->f.data[0]) {
  1184. if (s->last_picture_ptr->owner2 == s)
  1185. free_frame_buffer(s, s->last_picture_ptr);
  1186. }
  1187. /* release forgotten pictures */
  1188. /* if (mpeg124/h263) */
  1189. if (!s->encoding) {
  1190. for (i = 0; i < s->picture_count; i++) {
  1191. if (s->picture[i].owner2 == s && s->picture[i].f.data[0] &&
  1192. &s->picture[i] != s->last_picture_ptr &&
  1193. &s->picture[i] != s->next_picture_ptr &&
  1194. s->picture[i].f.reference && !s->picture[i].needs_realloc) {
  1195. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1196. av_log(avctx, AV_LOG_ERROR,
  1197. "releasing zombie picture\n");
  1198. free_frame_buffer(s, &s->picture[i]);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. if (!s->encoding) {
  1204. ff_release_unused_pictures(s, 1);
  1205. if (s->current_picture_ptr &&
  1206. s->current_picture_ptr->f.data[0] == NULL) {
  1207. // we already have a unused image
  1208. // (maybe it was set before reading the header)
  1209. pic = s->current_picture_ptr;
  1210. } else {
  1211. i = ff_find_unused_picture(s, 0);
  1212. if (i < 0) {
  1213. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1214. return i;
  1215. }
  1216. pic = &s->picture[i];
  1217. }
  1218. pic->f.reference = 0;
  1219. if (!s->droppable) {
  1220. if (s->codec_id == AV_CODEC_ID_H264)
  1221. pic->f.reference = s->picture_structure;
  1222. else if (s->pict_type != AV_PICTURE_TYPE_B)
  1223. pic->f.reference = 3;
  1224. }
  1225. pic->f.coded_picture_number = s->coded_picture_number++;
  1226. if (ff_alloc_picture(s, pic, 0) < 0)
  1227. return -1;
  1228. s->current_picture_ptr = pic;
  1229. // FIXME use only the vars from current_pic
  1230. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1231. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1232. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1233. if (s->picture_structure != PICT_FRAME)
  1234. s->current_picture_ptr->f.top_field_first =
  1235. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1236. }
  1237. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1238. !s->progressive_sequence;
  1239. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1240. }
  1241. s->current_picture_ptr->f.pict_type = s->pict_type;
  1242. // if (s->flags && CODEC_FLAG_QSCALE)
  1243. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1244. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1245. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  1246. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1247. s->last_picture_ptr = s->next_picture_ptr;
  1248. if (!s->droppable)
  1249. s->next_picture_ptr = s->current_picture_ptr;
  1250. }
  1251. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1252. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1253. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1254. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1255. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1256. s->pict_type, s->droppable);
  1257. if (s->codec_id != AV_CODEC_ID_H264) {
  1258. if ((s->last_picture_ptr == NULL ||
  1259. s->last_picture_ptr->f.data[0] == NULL) &&
  1260. (s->pict_type != AV_PICTURE_TYPE_I ||
  1261. s->picture_structure != PICT_FRAME)) {
  1262. if (s->pict_type != AV_PICTURE_TYPE_I)
  1263. av_log(avctx, AV_LOG_ERROR,
  1264. "warning: first frame is no keyframe\n");
  1265. else if (s->picture_structure != PICT_FRAME)
  1266. av_log(avctx, AV_LOG_INFO,
  1267. "allocate dummy last picture for field based first keyframe\n");
  1268. /* Allocate a dummy frame */
  1269. i = ff_find_unused_picture(s, 0);
  1270. if (i < 0) {
  1271. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1272. return i;
  1273. }
  1274. s->last_picture_ptr = &s->picture[i];
  1275. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1276. s->last_picture_ptr = NULL;
  1277. return -1;
  1278. }
  1279. ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 0);
  1280. ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 1);
  1281. s->last_picture_ptr->f.reference = 3;
  1282. }
  1283. if ((s->next_picture_ptr == NULL ||
  1284. s->next_picture_ptr->f.data[0] == NULL) &&
  1285. s->pict_type == AV_PICTURE_TYPE_B) {
  1286. /* Allocate a dummy frame */
  1287. i = ff_find_unused_picture(s, 0);
  1288. if (i < 0) {
  1289. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1290. return i;
  1291. }
  1292. s->next_picture_ptr = &s->picture[i];
  1293. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1294. s->next_picture_ptr = NULL;
  1295. return -1;
  1296. }
  1297. ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 0);
  1298. ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 1);
  1299. s->next_picture_ptr->f.reference = 3;
  1300. }
  1301. }
  1302. if (s->last_picture_ptr)
  1303. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1304. if (s->next_picture_ptr)
  1305. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1306. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1307. if (s->next_picture_ptr)
  1308. s->next_picture_ptr->owner2 = s;
  1309. if (s->last_picture_ptr)
  1310. s->last_picture_ptr->owner2 = s;
  1311. }
  1312. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1313. s->last_picture_ptr->f.data[0]));
  1314. if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
  1315. int i;
  1316. for (i = 0; i < 4; i++) {
  1317. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1318. s->current_picture.f.data[i] +=
  1319. s->current_picture.f.linesize[i];
  1320. }
  1321. s->current_picture.f.linesize[i] *= 2;
  1322. s->last_picture.f.linesize[i] *= 2;
  1323. s->next_picture.f.linesize[i] *= 2;
  1324. }
  1325. }
  1326. s->err_recognition = avctx->err_recognition;
  1327. /* set dequantizer, we can't do it during init as
  1328. * it might change for mpeg4 and we can't do it in the header
  1329. * decode as init is not called for mpeg4 there yet */
  1330. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1331. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1332. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1333. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1334. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1335. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1336. } else {
  1337. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1338. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1339. }
  1340. if (s->dct_error_sum) {
  1341. assert(s->avctx->noise_reduction && s->encoding);
  1342. update_noise_reduction(s);
  1343. }
  1344. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1345. return ff_xvmc_field_start(s, avctx);
  1346. return 0;
  1347. }
  1348. /* generic function for encode/decode called after a
  1349. * frame has been coded/decoded. */
  1350. void ff_MPV_frame_end(MpegEncContext *s)
  1351. {
  1352. int i;
  1353. /* redraw edges for the frame if decoding didn't complete */
  1354. // just to make sure that all data is rendered.
  1355. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1356. ff_xvmc_field_end(s);
  1357. } else if ((s->error_count || s->encoding) &&
  1358. !s->avctx->hwaccel &&
  1359. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1360. s->unrestricted_mv &&
  1361. s->current_picture.f.reference &&
  1362. !s->intra_only &&
  1363. !(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1364. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1365. int hshift = desc->log2_chroma_w;
  1366. int vshift = desc->log2_chroma_h;
  1367. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1368. s->h_edge_pos, s->v_edge_pos,
  1369. EDGE_WIDTH, EDGE_WIDTH,
  1370. EDGE_TOP | EDGE_BOTTOM);
  1371. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1372. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1373. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1374. EDGE_TOP | EDGE_BOTTOM);
  1375. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1376. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1377. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1378. EDGE_TOP | EDGE_BOTTOM);
  1379. }
  1380. emms_c();
  1381. s->last_pict_type = s->pict_type;
  1382. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1383. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1384. s->last_non_b_pict_type = s->pict_type;
  1385. }
  1386. #if 0
  1387. /* copy back current_picture variables */
  1388. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1389. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1390. s->picture[i] = s->current_picture;
  1391. break;
  1392. }
  1393. }
  1394. assert(i < MAX_PICTURE_COUNT);
  1395. #endif
  1396. if (s->encoding) {
  1397. /* release non-reference frames */
  1398. for (i = 0; i < s->picture_count; i++) {
  1399. if (s->picture[i].f.data[0] && !s->picture[i].f.reference
  1400. /* && s->picture[i].type != FF_BUFFER_TYPE_SHARED */) {
  1401. free_frame_buffer(s, &s->picture[i]);
  1402. }
  1403. }
  1404. }
  1405. // clear copies, to avoid confusion
  1406. #if 0
  1407. memset(&s->last_picture, 0, sizeof(Picture));
  1408. memset(&s->next_picture, 0, sizeof(Picture));
  1409. memset(&s->current_picture, 0, sizeof(Picture));
  1410. #endif
  1411. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1412. if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) {
  1413. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  1414. }
  1415. }
  1416. /**
  1417. * Draw a line from (ex, ey) -> (sx, sy).
  1418. * @param w width of the image
  1419. * @param h height of the image
  1420. * @param stride stride/linesize of the image
  1421. * @param color color of the arrow
  1422. */
  1423. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1424. int w, int h, int stride, int color)
  1425. {
  1426. int x, y, fr, f;
  1427. sx = av_clip(sx, 0, w - 1);
  1428. sy = av_clip(sy, 0, h - 1);
  1429. ex = av_clip(ex, 0, w - 1);
  1430. ey = av_clip(ey, 0, h - 1);
  1431. buf[sy * stride + sx] += color;
  1432. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1433. if (sx > ex) {
  1434. FFSWAP(int, sx, ex);
  1435. FFSWAP(int, sy, ey);
  1436. }
  1437. buf += sx + sy * stride;
  1438. ex -= sx;
  1439. f = ((ey - sy) << 16) / ex;
  1440. for (x = 0; x = ex; x++) {
  1441. y = (x * f) >> 16;
  1442. fr = (x * f) & 0xFFFF;
  1443. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1444. buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1445. }
  1446. } else {
  1447. if (sy > ey) {
  1448. FFSWAP(int, sx, ex);
  1449. FFSWAP(int, sy, ey);
  1450. }
  1451. buf += sx + sy * stride;
  1452. ey -= sy;
  1453. if (ey)
  1454. f = ((ex - sx) << 16) / ey;
  1455. else
  1456. f = 0;
  1457. for (y = 0; y = ey; y++) {
  1458. x = (y * f) >> 16;
  1459. fr = (y * f) & 0xFFFF;
  1460. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1461. buf[y * stride + x + 1] += (color * fr ) >> 16;
  1462. }
  1463. }
  1464. }
  1465. /**
  1466. * Draw an arrow from (ex, ey) -> (sx, sy).
  1467. * @param w width of the image
  1468. * @param h height of the image
  1469. * @param stride stride/linesize of the image
  1470. * @param color color of the arrow
  1471. */
  1472. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1473. int ey, int w, int h, int stride, int color)
  1474. {
  1475. int dx,dy;
  1476. sx = av_clip(sx, -100, w + 100);
  1477. sy = av_clip(sy, -100, h + 100);
  1478. ex = av_clip(ex, -100, w + 100);
  1479. ey = av_clip(ey, -100, h + 100);
  1480. dx = ex - sx;
  1481. dy = ey - sy;
  1482. if (dx * dx + dy * dy > 3 * 3) {
  1483. int rx = dx + dy;
  1484. int ry = -dx + dy;
  1485. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1486. // FIXME subpixel accuracy
  1487. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1488. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1489. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1490. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1491. }
  1492. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1493. }
  1494. /**
  1495. * Print debugging info for the given picture.
  1496. */
  1497. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
  1498. {
  1499. if (s->avctx->hwaccel || !pict || !pict->mb_type)
  1500. return;
  1501. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1502. int x,y;
  1503. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1504. switch (pict->pict_type) {
  1505. case AV_PICTURE_TYPE_I:
  1506. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1507. break;
  1508. case AV_PICTURE_TYPE_P:
  1509. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1510. break;
  1511. case AV_PICTURE_TYPE_B:
  1512. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1513. break;
  1514. case AV_PICTURE_TYPE_S:
  1515. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1516. break;
  1517. case AV_PICTURE_TYPE_SI:
  1518. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1519. break;
  1520. case AV_PICTURE_TYPE_SP:
  1521. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1522. break;
  1523. }
  1524. for (y = 0; y < s->mb_height; y++) {
  1525. for (x = 0; x < s->mb_width; x++) {
  1526. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1527. int count = s->mbskip_table[x + y * s->mb_stride];
  1528. if (count > 9)
  1529. count = 9;
  1530. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1531. }
  1532. if (s->avctx->debug & FF_DEBUG_QP) {
  1533. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1534. pict->qscale_table[x + y * s->mb_stride]);
  1535. }
  1536. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1537. int mb_type = pict->mb_type[x + y * s->mb_stride];
  1538. // Type & MV direction
  1539. if (IS_PCM(mb_type))
  1540. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1541. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1542. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1543. else if (IS_INTRA4x4(mb_type))
  1544. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1545. else if (IS_INTRA16x16(mb_type))
  1546. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1547. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1548. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1549. else if (IS_DIRECT(mb_type))
  1550. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1551. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1552. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1553. else if (IS_GMC(mb_type))
  1554. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1555. else if (IS_SKIP(mb_type))
  1556. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1557. else if (!USES_LIST(mb_type, 1))
  1558. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1559. else if (!USES_LIST(mb_type, 0))
  1560. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1561. else {
  1562. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1563. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1564. }
  1565. // segmentation
  1566. if (IS_8X8(mb_type))
  1567. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1568. else if (IS_16X8(mb_type))
  1569. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1570. else if (IS_8X16(mb_type))
  1571. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1572. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1573. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1574. else
  1575. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1576. if (IS_INTERLACED(mb_type))
  1577. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1578. else
  1579. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1580. }
  1581. }
  1582. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1583. }
  1584. }
  1585. if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1586. (s->avctx->debug_mv)) {
  1587. const int shift = 1 + s->quarter_sample;
  1588. int mb_y;
  1589. uint8_t *ptr;
  1590. int i;
  1591. int h_chroma_shift, v_chroma_shift, block_height;
  1592. const int width = s->avctx->width;
  1593. const int height = s->avctx->height;
  1594. const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
  1595. const int mv_stride = (s->mb_width << mv_sample_log2) +
  1596. (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1);
  1597. s->low_delay = 0; // needed to see the vectors without trashing the buffers
  1598. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1599. &h_chroma_shift, &v_chroma_shift);
  1600. for (i = 0; i < 3; i++) {
  1601. memcpy(s->visualization_buffer[i], pict->data[i],
  1602. (i == 0) ? pict->linesize[i] * height:
  1603. pict->linesize[i] * height >> v_chroma_shift);
  1604. pict->data[i] = s->visualization_buffer[i];
  1605. }
  1606. pict->type = FF_BUFFER_TYPE_COPY;
  1607. ptr = pict->data[0];
  1608. block_height = 16 >> v_chroma_shift;
  1609. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1610. int mb_x;
  1611. for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
  1612. const int mb_index = mb_x + mb_y * s->mb_stride;
  1613. if ((s->avctx->debug_mv) && pict->motion_val) {
  1614. int type;
  1615. for (type = 0; type < 3; type++) {
  1616. int direction = 0;
  1617. switch (type) {
  1618. case 0:
  1619. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1620. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1621. continue;
  1622. direction = 0;
  1623. break;
  1624. case 1:
  1625. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1626. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1627. continue;
  1628. direction = 0;
  1629. break;
  1630. case 2:
  1631. if ((!(s->avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1632. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1633. continue;
  1634. direction = 1;
  1635. break;
  1636. }
  1637. if (!USES_LIST(pict->mb_type[mb_index], direction))
  1638. continue;
  1639. if (IS_8X8(pict->mb_type[mb_index])) {
  1640. int i;
  1641. for (i = 0; i < 4; i++) {
  1642. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1643. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1644. int xy = (mb_x * 2 + (i & 1) +
  1645. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1646. int mx = (pict->motion_val[direction][xy][0] >> shift) + sx;
  1647. int my = (pict->motion_val[direction][xy][1] >> shift) + sy;
  1648. draw_arrow(ptr, sx, sy, mx, my, width,
  1649. height, s->linesize, 100);
  1650. }
  1651. } else if (IS_16X8(pict->mb_type[mb_index])) {
  1652. int i;
  1653. for (i = 0; i < 2; i++) {
  1654. int sx = mb_x * 16 + 8;
  1655. int sy = mb_y * 16 + 4 + 8 * i;
  1656. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1657. int mx = (pict->motion_val[direction][xy][0] >> shift);
  1658. int my = (pict->motion_val[direction][xy][1] >> shift);
  1659. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1660. my *= 2;
  1661. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1662. height, s->linesize, 100);
  1663. }
  1664. } else if (IS_8X16(pict->mb_type[mb_index])) {
  1665. int i;
  1666. for (i = 0; i < 2; i++) {
  1667. int sx = mb_x * 16 + 4 + 8 * i;
  1668. int sy = mb_y * 16 + 8;
  1669. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1670. int mx = pict->motion_val[direction][xy][0] >> shift;
  1671. int my = pict->motion_val[direction][xy][1] >> shift;
  1672. if (IS_INTERLACED(pict->mb_type[mb_index]))
  1673. my *= 2;
  1674. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1675. height, s->linesize, 100);
  1676. }
  1677. } else {
  1678. int sx = mb_x * 16 + 8;
  1679. int sy = mb_y * 16 + 8;
  1680. int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1681. int mx = pict->motion_val[direction][xy][0] >> shift + sx;
  1682. int my = pict->motion_val[direction][xy][1] >> shift + sy;
  1683. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1684. }
  1685. }
  1686. }
  1687. if ((s->avctx->debug & FF_DEBUG_VIS_QP) && pict->motion_val) {
  1688. uint64_t c = (pict->qscale_table[mb_index] * 128 / 31) *
  1689. 0x0101010101010101ULL;
  1690. int y;
  1691. for (y = 0; y < block_height; y++) {
  1692. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1693. (block_height * mb_y + y) *
  1694. pict->linesize[1]) = c;
  1695. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1696. (block_height * mb_y + y) *
  1697. pict->linesize[2]) = c;
  1698. }
  1699. }
  1700. if ((s->avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1701. pict->motion_val) {
  1702. int mb_type = pict->mb_type[mb_index];
  1703. uint64_t u,v;
  1704. int y;
  1705. #define COLOR(theta, r) \
  1706. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1707. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1708. u = v = 128;
  1709. if (IS_PCM(mb_type)) {
  1710. COLOR(120, 48)
  1711. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1712. IS_INTRA16x16(mb_type)) {
  1713. COLOR(30, 48)
  1714. } else if (IS_INTRA4x4(mb_type)) {
  1715. COLOR(90, 48)
  1716. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1717. // COLOR(120, 48)
  1718. } else if (IS_DIRECT(mb_type)) {
  1719. COLOR(150, 48)
  1720. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1721. COLOR(170, 48)
  1722. } else if (IS_GMC(mb_type)) {
  1723. COLOR(190, 48)
  1724. } else if (IS_SKIP(mb_type)) {
  1725. // COLOR(180, 48)
  1726. } else if (!USES_LIST(mb_type, 1)) {
  1727. COLOR(240, 48)
  1728. } else if (!USES_LIST(mb_type, 0)) {
  1729. COLOR(0, 48)
  1730. } else {
  1731. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1732. COLOR(300,48)
  1733. }
  1734. u *= 0x0101010101010101ULL;
  1735. v *= 0x0101010101010101ULL;
  1736. for (y = 0; y < block_height; y++) {
  1737. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1738. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1739. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1740. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1741. }
  1742. // segmentation
  1743. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1744. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1745. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1746. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1747. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1748. }
  1749. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1750. for (y = 0; y < 16; y++)
  1751. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1752. pict->linesize[0]] ^= 0x80;
  1753. }
  1754. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1755. int dm = 1 << (mv_sample_log2 - 2);
  1756. for (i = 0; i < 4; i++) {
  1757. int sx = mb_x * 16 + 8 * (i & 1);
  1758. int sy = mb_y * 16 + 8 * (i >> 1);
  1759. int xy = (mb_x * 2 + (i & 1) +
  1760. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1761. // FIXME bidir
  1762. int32_t *mv = (int32_t *) &pict->motion_val[0][xy];
  1763. if (mv[0] != mv[dm] ||
  1764. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1765. for (y = 0; y < 8; y++)
  1766. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1767. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1768. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1769. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1770. }
  1771. }
  1772. if (IS_INTERLACED(mb_type) &&
  1773. s->codec_id == AV_CODEC_ID_H264) {
  1774. // hmm
  1775. }
  1776. }
  1777. s->mbskip_table[mb_index] = 0;
  1778. }
  1779. }
  1780. }
  1781. }
  1782. /**
  1783. * find the lowest MB row referenced in the MVs
  1784. */
  1785. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1786. {
  1787. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1788. int my, off, i, mvs;
  1789. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1790. goto unhandled;
  1791. switch (s->mv_type) {
  1792. case MV_TYPE_16X16:
  1793. mvs = 1;
  1794. break;
  1795. case MV_TYPE_16X8:
  1796. mvs = 2;
  1797. break;
  1798. case MV_TYPE_8X8:
  1799. mvs = 4;
  1800. break;
  1801. default:
  1802. goto unhandled;
  1803. }
  1804. for (i = 0; i < mvs; i++) {
  1805. my = s->mv[dir][i][1]<<qpel_shift;
  1806. my_max = FFMAX(my_max, my);
  1807. my_min = FFMIN(my_min, my);
  1808. }
  1809. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1810. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1811. unhandled:
  1812. return s->mb_height-1;
  1813. }
  1814. /* put block[] to dest[] */
  1815. static inline void put_dct(MpegEncContext *s,
  1816. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1817. {
  1818. s->dct_unquantize_intra(s, block, i, qscale);
  1819. s->dsp.idct_put (dest, line_size, block);
  1820. }
  1821. /* add block[] to dest[] */
  1822. static inline void add_dct(MpegEncContext *s,
  1823. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1824. {
  1825. if (s->block_last_index[i] >= 0) {
  1826. s->dsp.idct_add (dest, line_size, block);
  1827. }
  1828. }
  1829. static inline void add_dequant_dct(MpegEncContext *s,
  1830. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1831. {
  1832. if (s->block_last_index[i] >= 0) {
  1833. s->dct_unquantize_inter(s, block, i, qscale);
  1834. s->dsp.idct_add (dest, line_size, block);
  1835. }
  1836. }
  1837. /**
  1838. * Clean dc, ac, coded_block for the current non-intra MB.
  1839. */
  1840. void ff_clean_intra_table_entries(MpegEncContext *s)
  1841. {
  1842. int wrap = s->b8_stride;
  1843. int xy = s->block_index[0];
  1844. s->dc_val[0][xy ] =
  1845. s->dc_val[0][xy + 1 ] =
  1846. s->dc_val[0][xy + wrap] =
  1847. s->dc_val[0][xy + 1 + wrap] = 1024;
  1848. /* ac pred */
  1849. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1850. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1851. if (s->msmpeg4_version>=3) {
  1852. s->coded_block[xy ] =
  1853. s->coded_block[xy + 1 ] =
  1854. s->coded_block[xy + wrap] =
  1855. s->coded_block[xy + 1 + wrap] = 0;
  1856. }
  1857. /* chroma */
  1858. wrap = s->mb_stride;
  1859. xy = s->mb_x + s->mb_y * wrap;
  1860. s->dc_val[1][xy] =
  1861. s->dc_val[2][xy] = 1024;
  1862. /* ac pred */
  1863. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1864. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1865. s->mbintra_table[xy]= 0;
  1866. }
  1867. /* generic function called after a macroblock has been parsed by the
  1868. decoder or after it has been encoded by the encoder.
  1869. Important variables used:
  1870. s->mb_intra : true if intra macroblock
  1871. s->mv_dir : motion vector direction
  1872. s->mv_type : motion vector type
  1873. s->mv : motion vector
  1874. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1875. */
  1876. static av_always_inline
  1877. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1878. int is_mpeg12)
  1879. {
  1880. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1881. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1882. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1883. return;
  1884. }
  1885. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1886. /* save DCT coefficients */
  1887. int i,j;
  1888. DCTELEM *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  1889. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1890. for(i=0; i<6; i++){
  1891. for(j=0; j<64; j++){
  1892. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1893. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1894. }
  1895. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1896. }
  1897. }
  1898. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  1899. /* update DC predictors for P macroblocks */
  1900. if (!s->mb_intra) {
  1901. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1902. if(s->mbintra_table[mb_xy])
  1903. ff_clean_intra_table_entries(s);
  1904. } else {
  1905. s->last_dc[0] =
  1906. s->last_dc[1] =
  1907. s->last_dc[2] = 128 << s->intra_dc_precision;
  1908. }
  1909. }
  1910. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1911. s->mbintra_table[mb_xy]=1;
  1912. 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
  1913. uint8_t *dest_y, *dest_cb, *dest_cr;
  1914. int dct_linesize, dct_offset;
  1915. op_pixels_func (*op_pix)[4];
  1916. qpel_mc_func (*op_qpix)[16];
  1917. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1918. const int uvlinesize = s->current_picture.f.linesize[1];
  1919. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1920. const int block_size = 8;
  1921. /* avoid copy if macroblock skipped in last frame too */
  1922. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1923. if(!s->encoding){
  1924. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1925. if (s->mb_skipped) {
  1926. s->mb_skipped= 0;
  1927. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1928. *mbskip_ptr = 1;
  1929. } else if(!s->current_picture.f.reference) {
  1930. *mbskip_ptr = 1;
  1931. } else{
  1932. *mbskip_ptr = 0; /* not skipped */
  1933. }
  1934. }
  1935. dct_linesize = linesize << s->interlaced_dct;
  1936. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1937. if(readable){
  1938. dest_y= s->dest[0];
  1939. dest_cb= s->dest[1];
  1940. dest_cr= s->dest[2];
  1941. }else{
  1942. dest_y = s->b_scratchpad;
  1943. dest_cb= s->b_scratchpad+16*linesize;
  1944. dest_cr= s->b_scratchpad+32*linesize;
  1945. }
  1946. if (!s->mb_intra) {
  1947. /* motion handling */
  1948. /* decoding or more than one mb_type (MC was already done otherwise) */
  1949. if(!s->encoding){
  1950. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1951. if (s->mv_dir & MV_DIR_FORWARD) {
  1952. ff_thread_await_progress(&s->last_picture_ptr->f,
  1953. ff_MPV_lowest_referenced_row(s, 0),
  1954. 0);
  1955. }
  1956. if (s->mv_dir & MV_DIR_BACKWARD) {
  1957. ff_thread_await_progress(&s->next_picture_ptr->f,
  1958. ff_MPV_lowest_referenced_row(s, 1),
  1959. 0);
  1960. }
  1961. }
  1962. op_qpix= s->me.qpel_put;
  1963. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1964. op_pix = s->dsp.put_pixels_tab;
  1965. }else{
  1966. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1967. }
  1968. if (s->mv_dir & MV_DIR_FORWARD) {
  1969. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1970. op_pix = s->dsp.avg_pixels_tab;
  1971. op_qpix= s->me.qpel_avg;
  1972. }
  1973. if (s->mv_dir & MV_DIR_BACKWARD) {
  1974. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1975. }
  1976. }
  1977. /* skip dequant / idct if we are really late ;) */
  1978. if(s->avctx->skip_idct){
  1979. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1980. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1981. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1982. goto skip_idct;
  1983. }
  1984. /* add dct residue */
  1985. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1986. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1987. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1988. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1989. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1990. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1991. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1992. if (s->chroma_y_shift){
  1993. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1994. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1995. }else{
  1996. dct_linesize >>= 1;
  1997. dct_offset >>=1;
  1998. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1999. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2000. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2001. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2002. }
  2003. }
  2004. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2005. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2006. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2007. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2008. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2009. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2010. if(s->chroma_y_shift){//Chroma420
  2011. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2012. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2013. }else{
  2014. //chroma422
  2015. dct_linesize = uvlinesize << s->interlaced_dct;
  2016. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  2017. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2018. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2019. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2020. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2021. if(!s->chroma_x_shift){//Chroma444
  2022. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  2023. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  2024. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  2025. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  2026. }
  2027. }
  2028. }//fi gray
  2029. }
  2030. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2031. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2032. }
  2033. } else {
  2034. /* dct only in intra block */
  2035. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2036. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2037. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2038. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2039. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2040. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2041. if(s->chroma_y_shift){
  2042. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2043. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2044. }else{
  2045. dct_offset >>=1;
  2046. dct_linesize >>=1;
  2047. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2048. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2049. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2050. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2051. }
  2052. }
  2053. }else{
  2054. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2055. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2056. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2057. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2058. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2059. if(s->chroma_y_shift){
  2060. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2061. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2062. }else{
  2063. dct_linesize = uvlinesize << s->interlaced_dct;
  2064. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  2065. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2066. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2067. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2068. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2069. if(!s->chroma_x_shift){//Chroma444
  2070. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  2071. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  2072. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  2073. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  2074. }
  2075. }
  2076. }//gray
  2077. }
  2078. }
  2079. skip_idct:
  2080. if(!readable){
  2081. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2082. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2083. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2084. }
  2085. }
  2086. }
  2087. void ff_MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  2088. #if !CONFIG_SMALL
  2089. if(s->out_format == FMT_MPEG1) {
  2090. MPV_decode_mb_internal(s, block, 1);
  2091. } else
  2092. #endif
  2093. MPV_decode_mb_internal(s, block, 0);
  2094. }
  2095. /**
  2096. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2097. */
  2098. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2099. const int field_pic= s->picture_structure != PICT_FRAME;
  2100. if(field_pic){
  2101. h <<= 1;
  2102. y <<= 1;
  2103. }
  2104. if (!s->avctx->hwaccel
  2105. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2106. && s->unrestricted_mv
  2107. && s->current_picture.f.reference
  2108. && !s->intra_only
  2109. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2110. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  2111. int sides = 0, edge_h;
  2112. int hshift = desc->log2_chroma_w;
  2113. int vshift = desc->log2_chroma_h;
  2114. if (y==0) sides |= EDGE_TOP;
  2115. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2116. edge_h= FFMIN(h, s->v_edge_pos - y);
  2117. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
  2118. s->linesize, s->h_edge_pos, edge_h,
  2119. EDGE_WIDTH, EDGE_WIDTH, sides);
  2120. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
  2121. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2122. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2123. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
  2124. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2125. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2126. }
  2127. h= FFMIN(h, s->avctx->height - y);
  2128. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2129. if (s->avctx->draw_horiz_band) {
  2130. AVFrame *src;
  2131. int offset[AV_NUM_DATA_POINTERS];
  2132. int i;
  2133. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2134. src = &s->current_picture_ptr->f;
  2135. else if(s->last_picture_ptr)
  2136. src = &s->last_picture_ptr->f;
  2137. else
  2138. return;
  2139. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2140. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2141. offset[i] = 0;
  2142. }else{
  2143. offset[0]= y * s->linesize;
  2144. offset[1]=
  2145. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2146. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2147. offset[i] = 0;
  2148. }
  2149. emms_c();
  2150. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2151. y, s->picture_structure, h);
  2152. }
  2153. }
  2154. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2155. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2156. const int uvlinesize = s->current_picture.f.linesize[1];
  2157. const int mb_size= 4;
  2158. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2159. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2160. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2161. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2162. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2163. 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;
  2164. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2165. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2166. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2167. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2168. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2169. {
  2170. if(s->picture_structure==PICT_FRAME){
  2171. s->dest[0] += s->mb_y * linesize << mb_size;
  2172. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2173. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2174. }else{
  2175. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2176. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2177. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2178. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2179. }
  2180. }
  2181. }
  2182. void ff_mpeg_flush(AVCodecContext *avctx){
  2183. int i;
  2184. MpegEncContext *s = avctx->priv_data;
  2185. if(s==NULL || s->picture==NULL)
  2186. return;
  2187. for(i=0; i<s->picture_count; i++){
  2188. if (s->picture[i].f.data[0] &&
  2189. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  2190. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  2191. free_frame_buffer(s, &s->picture[i]);
  2192. }
  2193. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2194. s->mb_x= s->mb_y= 0;
  2195. s->parse_context.state= -1;
  2196. s->parse_context.frame_start_found= 0;
  2197. s->parse_context.overread= 0;
  2198. s->parse_context.overread_index= 0;
  2199. s->parse_context.index= 0;
  2200. s->parse_context.last_index= 0;
  2201. s->bitstream_buffer_size=0;
  2202. s->pp_time=0;
  2203. }
  2204. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2205. DCTELEM *block, int n, int qscale)
  2206. {
  2207. int i, level, nCoeffs;
  2208. const uint16_t *quant_matrix;
  2209. nCoeffs= s->block_last_index[n];
  2210. if (n < 4)
  2211. block[0] = block[0] * s->y_dc_scale;
  2212. else
  2213. block[0] = block[0] * s->c_dc_scale;
  2214. /* XXX: only mpeg1 */
  2215. quant_matrix = s->intra_matrix;
  2216. for(i=1;i<=nCoeffs;i++) {
  2217. int j= s->intra_scantable.permutated[i];
  2218. level = block[j];
  2219. if (level) {
  2220. if (level < 0) {
  2221. level = -level;
  2222. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2223. level = (level - 1) | 1;
  2224. level = -level;
  2225. } else {
  2226. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2227. level = (level - 1) | 1;
  2228. }
  2229. block[j] = level;
  2230. }
  2231. }
  2232. }
  2233. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2234. DCTELEM *block, int n, int qscale)
  2235. {
  2236. int i, level, nCoeffs;
  2237. const uint16_t *quant_matrix;
  2238. nCoeffs= s->block_last_index[n];
  2239. quant_matrix = s->inter_matrix;
  2240. for(i=0; i<=nCoeffs; i++) {
  2241. int j= s->intra_scantable.permutated[i];
  2242. level = block[j];
  2243. if (level) {
  2244. if (level < 0) {
  2245. level = -level;
  2246. level = (((level << 1) + 1) * qscale *
  2247. ((int) (quant_matrix[j]))) >> 4;
  2248. level = (level - 1) | 1;
  2249. level = -level;
  2250. } else {
  2251. level = (((level << 1) + 1) * qscale *
  2252. ((int) (quant_matrix[j]))) >> 4;
  2253. level = (level - 1) | 1;
  2254. }
  2255. block[j] = level;
  2256. }
  2257. }
  2258. }
  2259. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2260. DCTELEM *block, int n, int qscale)
  2261. {
  2262. int i, level, nCoeffs;
  2263. const uint16_t *quant_matrix;
  2264. if(s->alternate_scan) nCoeffs= 63;
  2265. else nCoeffs= s->block_last_index[n];
  2266. if (n < 4)
  2267. block[0] = block[0] * s->y_dc_scale;
  2268. else
  2269. block[0] = block[0] * s->c_dc_scale;
  2270. quant_matrix = s->intra_matrix;
  2271. for(i=1;i<=nCoeffs;i++) {
  2272. int j= s->intra_scantable.permutated[i];
  2273. level = block[j];
  2274. if (level) {
  2275. if (level < 0) {
  2276. level = -level;
  2277. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2278. level = -level;
  2279. } else {
  2280. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2281. }
  2282. block[j] = level;
  2283. }
  2284. }
  2285. }
  2286. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2287. DCTELEM *block, int n, int qscale)
  2288. {
  2289. int i, level, nCoeffs;
  2290. const uint16_t *quant_matrix;
  2291. int sum=-1;
  2292. if(s->alternate_scan) nCoeffs= 63;
  2293. else nCoeffs= s->block_last_index[n];
  2294. if (n < 4)
  2295. block[0] = block[0] * s->y_dc_scale;
  2296. else
  2297. block[0] = block[0] * s->c_dc_scale;
  2298. quant_matrix = s->intra_matrix;
  2299. for(i=1;i<=nCoeffs;i++) {
  2300. int j= s->intra_scantable.permutated[i];
  2301. level = block[j];
  2302. if (level) {
  2303. if (level < 0) {
  2304. level = -level;
  2305. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2306. level = -level;
  2307. } else {
  2308. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2309. }
  2310. block[j] = level;
  2311. sum+=level;
  2312. }
  2313. }
  2314. block[63]^=sum&1;
  2315. }
  2316. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2317. DCTELEM *block, int n, int qscale)
  2318. {
  2319. int i, level, nCoeffs;
  2320. const uint16_t *quant_matrix;
  2321. int sum=-1;
  2322. if(s->alternate_scan) nCoeffs= 63;
  2323. else nCoeffs= s->block_last_index[n];
  2324. quant_matrix = s->inter_matrix;
  2325. for(i=0; i<=nCoeffs; i++) {
  2326. int j= s->intra_scantable.permutated[i];
  2327. level = block[j];
  2328. if (level) {
  2329. if (level < 0) {
  2330. level = -level;
  2331. level = (((level << 1) + 1) * qscale *
  2332. ((int) (quant_matrix[j]))) >> 4;
  2333. level = -level;
  2334. } else {
  2335. level = (((level << 1) + 1) * qscale *
  2336. ((int) (quant_matrix[j]))) >> 4;
  2337. }
  2338. block[j] = level;
  2339. sum+=level;
  2340. }
  2341. }
  2342. block[63]^=sum&1;
  2343. }
  2344. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2345. DCTELEM *block, int n, int qscale)
  2346. {
  2347. int i, level, qmul, qadd;
  2348. int nCoeffs;
  2349. assert(s->block_last_index[n]>=0);
  2350. qmul = qscale << 1;
  2351. if (!s->h263_aic) {
  2352. if (n < 4)
  2353. block[0] = block[0] * s->y_dc_scale;
  2354. else
  2355. block[0] = block[0] * s->c_dc_scale;
  2356. qadd = (qscale - 1) | 1;
  2357. }else{
  2358. qadd = 0;
  2359. }
  2360. if(s->ac_pred)
  2361. nCoeffs=63;
  2362. else
  2363. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2364. for(i=1; i<=nCoeffs; i++) {
  2365. level = block[i];
  2366. if (level) {
  2367. if (level < 0) {
  2368. level = level * qmul - qadd;
  2369. } else {
  2370. level = level * qmul + qadd;
  2371. }
  2372. block[i] = level;
  2373. }
  2374. }
  2375. }
  2376. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2377. DCTELEM *block, int n, int qscale)
  2378. {
  2379. int i, level, qmul, qadd;
  2380. int nCoeffs;
  2381. assert(s->block_last_index[n]>=0);
  2382. qadd = (qscale - 1) | 1;
  2383. qmul = qscale << 1;
  2384. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2385. for(i=0; i<=nCoeffs; i++) {
  2386. level = block[i];
  2387. if (level) {
  2388. if (level < 0) {
  2389. level = level * qmul - qadd;
  2390. } else {
  2391. level = level * qmul + qadd;
  2392. }
  2393. block[i] = level;
  2394. }
  2395. }
  2396. }
  2397. /**
  2398. * set qscale and update qscale dependent variables.
  2399. */
  2400. void ff_set_qscale(MpegEncContext * s, int qscale)
  2401. {
  2402. if (qscale < 1)
  2403. qscale = 1;
  2404. else if (qscale > 31)
  2405. qscale = 31;
  2406. s->qscale = qscale;
  2407. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2408. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2409. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2410. }
  2411. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2412. {
  2413. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
  2414. ff_thread_report_progress(&s->current_picture_ptr->f, s->mb_y, 0);
  2415. }