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.

3058 lines
114KB

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