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.

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