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.

3116 lines
116KB

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