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.

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