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.

3106 lines
115KB

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