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.

2635 lines
91KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; 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/avassert.h"
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #include "mpegvideo.h"
  35. #include "mjpegenc.h"
  36. #include "msmpeg4.h"
  37. #include "xvmc_internal.h"
  38. #include "thread.h"
  39. #include <limits.h>
  40. //#undef NDEBUG
  41. //#include <assert.h>
  42. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  43. int16_t *block, int n, int qscale);
  44. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  45. int16_t *block, int n, int qscale);
  46. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  47. int16_t *block, int n, int qscale);
  48. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  49. int16_t *block, int n, int qscale);
  50. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  51. int16_t *block, int n, int qscale);
  52. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  53. int16_t *block, int n, int qscale);
  54. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  55. int16_t *block, int n, int qscale);
  56. /* enable all paranoid tests for rounding, overflows, etc... */
  57. //#define PARANOID
  58. //#define DEBUG
  59. static const uint8_t ff_default_chroma_qscale_table[32] = {
  60. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  61. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  62. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  63. };
  64. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  65. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  71. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  72. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  73. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  74. };
  75. static const uint8_t mpeg2_dc_scale_table1[128] = {
  76. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  77. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  78. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  79. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  80. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  81. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  82. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  83. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  84. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  85. };
  86. static const uint8_t mpeg2_dc_scale_table2[128] = {
  87. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  88. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  89. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  90. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  91. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  92. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  93. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  94. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  95. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  96. };
  97. static const uint8_t mpeg2_dc_scale_table3[128] = {
  98. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  99. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  100. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  101. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  102. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  103. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  104. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  105. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  106. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  107. };
  108. const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
  109. ff_mpeg1_dc_scale_table,
  110. mpeg2_dc_scale_table1,
  111. mpeg2_dc_scale_table2,
  112. mpeg2_dc_scale_table3,
  113. };
  114. const enum AVPixelFormat ff_pixfmt_list_420[] = {
  115. AV_PIX_FMT_YUV420P,
  116. AV_PIX_FMT_NONE
  117. };
  118. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  119. int (*mv)[2][4][2],
  120. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  121. {
  122. MpegEncContext *s = opaque;
  123. s->mv_dir = mv_dir;
  124. s->mv_type = mv_type;
  125. s->mb_intra = mb_intra;
  126. s->mb_skipped = mb_skipped;
  127. s->mb_x = mb_x;
  128. s->mb_y = mb_y;
  129. memcpy(s->mv, mv, sizeof(*mv));
  130. ff_init_block_index(s);
  131. ff_update_block_index(s);
  132. s->dsp.clear_blocks(s->block[0]);
  133. s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  134. s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  135. s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  136. assert(ref == 0);
  137. ff_MPV_decode_mb(s, s->block);
  138. }
  139. /* init common dct for both encoder and decoder */
  140. av_cold int ff_dct_common_init(MpegEncContext *s)
  141. {
  142. ff_dsputil_init(&s->dsp, s->avctx);
  143. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  144. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  145. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  146. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  147. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  148. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  149. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  150. if (s->flags & CODEC_FLAG_BITEXACT)
  151. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  152. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  153. #if ARCH_X86
  154. ff_MPV_common_init_x86(s);
  155. #elif ARCH_ALPHA
  156. ff_MPV_common_init_axp(s);
  157. #elif ARCH_ARM
  158. ff_MPV_common_init_arm(s);
  159. #elif ARCH_BFIN
  160. ff_MPV_common_init_bfin(s);
  161. #elif ARCH_PPC
  162. ff_MPV_common_init_ppc(s);
  163. #endif
  164. /* load & permutate scantables
  165. * note: only wmv uses different ones
  166. */
  167. if (s->alternate_scan) {
  168. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  170. } else {
  171. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  172. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  173. }
  174. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  175. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  176. return 0;
  177. }
  178. int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
  179. {
  180. int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
  181. // edge emu needs blocksize + filter length - 1
  182. // (= 17x17 for halfpel / 21x21 for h264)
  183. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  184. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  185. // linesize * interlaced * MBsize
  186. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
  187. fail);
  188. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 2,
  189. fail)
  190. s->me.temp = s->me.scratchpad;
  191. s->rd_scratchpad = s->me.scratchpad;
  192. s->b_scratchpad = s->me.scratchpad;
  193. s->obmc_scratchpad = s->me.scratchpad + 16;
  194. return 0;
  195. fail:
  196. av_freep(&s->edge_emu_buffer);
  197. return AVERROR(ENOMEM);
  198. }
  199. /**
  200. * Allocate a frame buffer
  201. */
  202. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  203. {
  204. int r, ret;
  205. pic->tf.f = &pic->f;
  206. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  207. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  208. s->codec_id != AV_CODEC_ID_MSS2)
  209. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  210. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  211. else {
  212. pic->f.width = s->avctx->width;
  213. pic->f.height = s->avctx->height;
  214. pic->f.format = s->avctx->pix_fmt;
  215. r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
  216. }
  217. if (r < 0 || !pic->f.data[0]) {
  218. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  219. r, pic->f.data[0]);
  220. return -1;
  221. }
  222. if (s->avctx->hwaccel) {
  223. assert(!pic->hwaccel_picture_private);
  224. if (s->avctx->hwaccel->priv_data_size) {
  225. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
  226. if (!pic->hwaccel_priv_buf) {
  227. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  228. return -1;
  229. }
  230. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  231. }
  232. }
  233. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  234. s->uvlinesize != pic->f.linesize[1])) {
  235. av_log(s->avctx, AV_LOG_ERROR,
  236. "get_buffer() failed (stride changed)\n");
  237. ff_mpeg_unref_picture(s, pic);
  238. return -1;
  239. }
  240. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  241. av_log(s->avctx, AV_LOG_ERROR,
  242. "get_buffer() failed (uv stride mismatch)\n");
  243. ff_mpeg_unref_picture(s, pic);
  244. return -1;
  245. }
  246. if (!s->edge_emu_buffer &&
  247. (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  248. av_log(s->avctx, AV_LOG_ERROR,
  249. "get_buffer() failed to allocate context scratch buffers.\n");
  250. ff_mpeg_unref_picture(s, pic);
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. static void free_picture_tables(Picture *pic)
  256. {
  257. int i;
  258. av_buffer_unref(&pic->mb_var_buf);
  259. av_buffer_unref(&pic->mc_mb_var_buf);
  260. av_buffer_unref(&pic->mb_mean_buf);
  261. av_buffer_unref(&pic->mbskip_table_buf);
  262. av_buffer_unref(&pic->qscale_table_buf);
  263. av_buffer_unref(&pic->mb_type_buf);
  264. for (i = 0; i < 2; i++) {
  265. av_buffer_unref(&pic->motion_val_buf[i]);
  266. av_buffer_unref(&pic->ref_index_buf[i]);
  267. }
  268. }
  269. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  270. {
  271. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  272. const int mb_array_size = s->mb_stride * s->mb_height;
  273. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  274. int i;
  275. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  276. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  277. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  278. sizeof(uint32_t));
  279. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  280. return AVERROR(ENOMEM);
  281. if (s->encoding) {
  282. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  283. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  284. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  285. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  286. return AVERROR(ENOMEM);
  287. }
  288. if (s->out_format == FMT_H263 || s->encoding ||
  289. (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
  290. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  291. int ref_index_size = 4 * mb_array_size;
  292. for (i = 0; mv_size && i < 2; i++) {
  293. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  294. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  295. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  296. return AVERROR(ENOMEM);
  297. }
  298. }
  299. return 0;
  300. }
  301. static int make_tables_writable(Picture *pic)
  302. {
  303. int ret, i;
  304. #define MAKE_WRITABLE(table) \
  305. do {\
  306. if (pic->table &&\
  307. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  308. return ret;\
  309. } while (0)
  310. MAKE_WRITABLE(mb_var_buf);
  311. MAKE_WRITABLE(mc_mb_var_buf);
  312. MAKE_WRITABLE(mb_mean_buf);
  313. MAKE_WRITABLE(mbskip_table_buf);
  314. MAKE_WRITABLE(qscale_table_buf);
  315. MAKE_WRITABLE(mb_type_buf);
  316. for (i = 0; i < 2; i++) {
  317. MAKE_WRITABLE(motion_val_buf[i]);
  318. MAKE_WRITABLE(ref_index_buf[i]);
  319. }
  320. return 0;
  321. }
  322. /**
  323. * Allocate a Picture.
  324. * The pixels are allocated/set by calling get_buffer() if shared = 0
  325. */
  326. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  327. {
  328. int i, ret;
  329. if (shared) {
  330. assert(pic->f.data[0]);
  331. pic->shared = 1;
  332. } else {
  333. assert(!pic->f.data[0]);
  334. if (alloc_frame_buffer(s, pic) < 0)
  335. return -1;
  336. s->linesize = pic->f.linesize[0];
  337. s->uvlinesize = pic->f.linesize[1];
  338. }
  339. if (!pic->qscale_table_buf)
  340. ret = alloc_picture_tables(s, pic);
  341. else
  342. ret = make_tables_writable(pic);
  343. if (ret < 0)
  344. goto fail;
  345. if (s->encoding) {
  346. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  347. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  348. pic->mb_mean = pic->mb_mean_buf->data;
  349. }
  350. pic->mbskip_table = pic->mbskip_table_buf->data;
  351. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  352. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  353. if (pic->motion_val_buf[0]) {
  354. for (i = 0; i < 2; i++) {
  355. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  356. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  357. }
  358. }
  359. return 0;
  360. fail:
  361. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  362. ff_mpeg_unref_picture(s, pic);
  363. free_picture_tables(pic);
  364. return AVERROR(ENOMEM);
  365. }
  366. /**
  367. * Deallocate a picture.
  368. */
  369. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  370. {
  371. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  372. pic->tf.f = &pic->f;
  373. /* WM Image / Screen codecs allocate internal buffers with different
  374. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  375. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  376. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  377. s->codec_id != AV_CODEC_ID_MSS2)
  378. ff_thread_release_buffer(s->avctx, &pic->tf);
  379. else
  380. av_frame_unref(&pic->f);
  381. av_buffer_unref(&pic->hwaccel_priv_buf);
  382. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  383. }
  384. static int update_picture_tables(Picture *dst, Picture *src)
  385. {
  386. int i;
  387. #define UPDATE_TABLE(table)\
  388. do {\
  389. if (src->table &&\
  390. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  391. av_buffer_unref(&dst->table);\
  392. dst->table = av_buffer_ref(src->table);\
  393. if (!dst->table) {\
  394. free_picture_tables(dst);\
  395. return AVERROR(ENOMEM);\
  396. }\
  397. }\
  398. } while (0)
  399. UPDATE_TABLE(mb_var_buf);
  400. UPDATE_TABLE(mc_mb_var_buf);
  401. UPDATE_TABLE(mb_mean_buf);
  402. UPDATE_TABLE(mbskip_table_buf);
  403. UPDATE_TABLE(qscale_table_buf);
  404. UPDATE_TABLE(mb_type_buf);
  405. for (i = 0; i < 2; i++) {
  406. UPDATE_TABLE(motion_val_buf[i]);
  407. UPDATE_TABLE(ref_index_buf[i]);
  408. }
  409. dst->mb_var = src->mb_var;
  410. dst->mc_mb_var = src->mc_mb_var;
  411. dst->mb_mean = src->mb_mean;
  412. dst->mbskip_table = src->mbskip_table;
  413. dst->qscale_table = src->qscale_table;
  414. dst->mb_type = src->mb_type;
  415. for (i = 0; i < 2; i++) {
  416. dst->motion_val[i] = src->motion_val[i];
  417. dst->ref_index[i] = src->ref_index[i];
  418. }
  419. return 0;
  420. }
  421. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  422. {
  423. int ret;
  424. av_assert0(!dst->f.buf[0]);
  425. av_assert0(src->f.buf[0]);
  426. src->tf.f = &src->f;
  427. dst->tf.f = &dst->f;
  428. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  429. if (ret < 0)
  430. goto fail;
  431. ret = update_picture_tables(dst, src);
  432. if (ret < 0)
  433. goto fail;
  434. if (src->hwaccel_picture_private) {
  435. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  436. if (!dst->hwaccel_priv_buf)
  437. goto fail;
  438. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  439. }
  440. dst->field_picture = src->field_picture;
  441. dst->mb_var_sum = src->mb_var_sum;
  442. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  443. dst->b_frame_score = src->b_frame_score;
  444. dst->needs_realloc = src->needs_realloc;
  445. dst->reference = src->reference;
  446. dst->shared = src->shared;
  447. return 0;
  448. fail:
  449. ff_mpeg_unref_picture(s, dst);
  450. return ret;
  451. }
  452. static int init_duplicate_context(MpegEncContext *s)
  453. {
  454. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  455. int c_size = s->mb_stride * (s->mb_height + 1);
  456. int yc_size = y_size + 2 * c_size;
  457. int i;
  458. s->edge_emu_buffer =
  459. s->me.scratchpad =
  460. s->me.temp =
  461. s->rd_scratchpad =
  462. s->b_scratchpad =
  463. s->obmc_scratchpad = NULL;
  464. if (s->encoding) {
  465. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  466. ME_MAP_SIZE * sizeof(uint32_t), fail)
  467. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  468. ME_MAP_SIZE * sizeof(uint32_t), fail)
  469. if (s->avctx->noise_reduction) {
  470. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  471. 2 * 64 * sizeof(int), fail)
  472. }
  473. }
  474. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  475. s->block = s->blocks[0];
  476. for (i = 0; i < 12; i++) {
  477. s->pblocks[i] = &s->block[i];
  478. }
  479. if (s->out_format == FMT_H263) {
  480. /* ac values */
  481. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  482. yc_size * sizeof(int16_t) * 16, fail);
  483. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  484. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  485. s->ac_val[2] = s->ac_val[1] + c_size;
  486. }
  487. return 0;
  488. fail:
  489. return -1; // free() through ff_MPV_common_end()
  490. }
  491. static void free_duplicate_context(MpegEncContext *s)
  492. {
  493. if (s == NULL)
  494. return;
  495. av_freep(&s->edge_emu_buffer);
  496. av_freep(&s->me.scratchpad);
  497. s->me.temp =
  498. s->rd_scratchpad =
  499. s->b_scratchpad =
  500. s->obmc_scratchpad = NULL;
  501. av_freep(&s->dct_error_sum);
  502. av_freep(&s->me.map);
  503. av_freep(&s->me.score_map);
  504. av_freep(&s->blocks);
  505. av_freep(&s->ac_val_base);
  506. s->block = NULL;
  507. }
  508. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  509. {
  510. #define COPY(a) bak->a = src->a
  511. COPY(edge_emu_buffer);
  512. COPY(me.scratchpad);
  513. COPY(me.temp);
  514. COPY(rd_scratchpad);
  515. COPY(b_scratchpad);
  516. COPY(obmc_scratchpad);
  517. COPY(me.map);
  518. COPY(me.score_map);
  519. COPY(blocks);
  520. COPY(block);
  521. COPY(start_mb_y);
  522. COPY(end_mb_y);
  523. COPY(me.map_generation);
  524. COPY(pb);
  525. COPY(dct_error_sum);
  526. COPY(dct_count[0]);
  527. COPY(dct_count[1]);
  528. COPY(ac_val_base);
  529. COPY(ac_val[0]);
  530. COPY(ac_val[1]);
  531. COPY(ac_val[2]);
  532. #undef COPY
  533. }
  534. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  535. {
  536. MpegEncContext bak;
  537. int i, ret;
  538. // FIXME copy only needed parts
  539. // START_TIMER
  540. backup_duplicate_context(&bak, dst);
  541. memcpy(dst, src, sizeof(MpegEncContext));
  542. backup_duplicate_context(dst, &bak);
  543. for (i = 0; i < 12; i++) {
  544. dst->pblocks[i] = &dst->block[i];
  545. }
  546. if (!dst->edge_emu_buffer &&
  547. (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
  548. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  549. "scratch buffers.\n");
  550. return ret;
  551. }
  552. // STOP_TIMER("update_duplicate_context")
  553. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  554. return 0;
  555. }
  556. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  557. const AVCodecContext *src)
  558. {
  559. int i, ret;
  560. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  561. if (dst == src || !s1->context_initialized)
  562. return 0;
  563. // FIXME can parameters change on I-frames?
  564. // in that case dst may need a reinit
  565. if (!s->context_initialized) {
  566. memcpy(s, s1, sizeof(MpegEncContext));
  567. s->avctx = dst;
  568. s->bitstream_buffer = NULL;
  569. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  570. ff_MPV_common_init(s);
  571. }
  572. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  573. int err;
  574. s->context_reinit = 0;
  575. s->height = s1->height;
  576. s->width = s1->width;
  577. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  578. return err;
  579. }
  580. s->avctx->coded_height = s1->avctx->coded_height;
  581. s->avctx->coded_width = s1->avctx->coded_width;
  582. s->avctx->width = s1->avctx->width;
  583. s->avctx->height = s1->avctx->height;
  584. s->coded_picture_number = s1->coded_picture_number;
  585. s->picture_number = s1->picture_number;
  586. s->input_picture_number = s1->input_picture_number;
  587. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  588. ff_mpeg_unref_picture(s, &s->picture[i]);
  589. if (s1->picture[i].f.data[0] &&
  590. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  591. return ret;
  592. }
  593. #define UPDATE_PICTURE(pic)\
  594. do {\
  595. ff_mpeg_unref_picture(s, &s->pic);\
  596. if (s1->pic.f.data[0])\
  597. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  598. else\
  599. ret = update_picture_tables(&s->pic, &s1->pic);\
  600. if (ret < 0)\
  601. return ret;\
  602. } while (0)
  603. UPDATE_PICTURE(current_picture);
  604. UPDATE_PICTURE(last_picture);
  605. UPDATE_PICTURE(next_picture);
  606. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  607. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  608. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  609. // Error/bug resilience
  610. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  611. s->workaround_bugs = s1->workaround_bugs;
  612. // MPEG4 timing info
  613. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  614. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  615. // B-frame info
  616. s->max_b_frames = s1->max_b_frames;
  617. s->low_delay = s1->low_delay;
  618. s->droppable = s1->droppable;
  619. // DivX handling (doesn't work)
  620. s->divx_packed = s1->divx_packed;
  621. if (s1->bitstream_buffer) {
  622. if (s1->bitstream_buffer_size +
  623. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  624. av_fast_malloc(&s->bitstream_buffer,
  625. &s->allocated_bitstream_buffer_size,
  626. s1->allocated_bitstream_buffer_size);
  627. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  628. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  629. s1->bitstream_buffer_size);
  630. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  631. FF_INPUT_BUFFER_PADDING_SIZE);
  632. }
  633. // linesize dependend scratch buffer allocation
  634. if (!s->edge_emu_buffer)
  635. if (s1->linesize) {
  636. if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
  637. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  638. "scratch buffers.\n");
  639. return AVERROR(ENOMEM);
  640. }
  641. } else {
  642. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  643. "be allocated due to unknown size.\n");
  644. return AVERROR_BUG;
  645. }
  646. // MPEG2/interlacing info
  647. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  648. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  649. if (!s1->first_field) {
  650. s->last_pict_type = s1->pict_type;
  651. if (s1->current_picture_ptr)
  652. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  653. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  654. s->last_non_b_pict_type = s1->pict_type;
  655. }
  656. }
  657. return 0;
  658. }
  659. /**
  660. * Set the given MpegEncContext to common defaults
  661. * (same for encoding and decoding).
  662. * The changed fields will not depend upon the
  663. * prior state of the MpegEncContext.
  664. */
  665. void ff_MPV_common_defaults(MpegEncContext *s)
  666. {
  667. s->y_dc_scale_table =
  668. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  669. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  670. s->progressive_frame = 1;
  671. s->progressive_sequence = 1;
  672. s->picture_structure = PICT_FRAME;
  673. s->coded_picture_number = 0;
  674. s->picture_number = 0;
  675. s->input_picture_number = 0;
  676. s->picture_in_gop_number = 0;
  677. s->f_code = 1;
  678. s->b_code = 1;
  679. s->slice_context_count = 1;
  680. }
  681. /**
  682. * Set the given MpegEncContext to defaults for decoding.
  683. * the changed fields will not depend upon
  684. * the prior state of the MpegEncContext.
  685. */
  686. void ff_MPV_decode_defaults(MpegEncContext *s)
  687. {
  688. ff_MPV_common_defaults(s);
  689. }
  690. static int init_er(MpegEncContext *s)
  691. {
  692. ERContext *er = &s->er;
  693. int mb_array_size = s->mb_height * s->mb_stride;
  694. int i;
  695. er->avctx = s->avctx;
  696. er->dsp = &s->dsp;
  697. er->mb_index2xy = s->mb_index2xy;
  698. er->mb_num = s->mb_num;
  699. er->mb_width = s->mb_width;
  700. er->mb_height = s->mb_height;
  701. er->mb_stride = s->mb_stride;
  702. er->b8_stride = s->b8_stride;
  703. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  704. er->error_status_table = av_mallocz(mb_array_size);
  705. if (!er->er_temp_buffer || !er->error_status_table)
  706. goto fail;
  707. er->mbskip_table = s->mbskip_table;
  708. er->mbintra_table = s->mbintra_table;
  709. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  710. er->dc_val[i] = s->dc_val[i];
  711. er->decode_mb = mpeg_er_decode_mb;
  712. er->opaque = s;
  713. return 0;
  714. fail:
  715. av_freep(&er->er_temp_buffer);
  716. av_freep(&er->error_status_table);
  717. return AVERROR(ENOMEM);
  718. }
  719. /**
  720. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  721. */
  722. static int init_context_frame(MpegEncContext *s)
  723. {
  724. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  725. s->mb_width = (s->width + 15) / 16;
  726. s->mb_stride = s->mb_width + 1;
  727. s->b8_stride = s->mb_width * 2 + 1;
  728. s->b4_stride = s->mb_width * 4 + 1;
  729. mb_array_size = s->mb_height * s->mb_stride;
  730. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  731. /* set default edge pos, will be overriden
  732. * in decode_header if needed */
  733. s->h_edge_pos = s->mb_width * 16;
  734. s->v_edge_pos = s->mb_height * 16;
  735. s->mb_num = s->mb_width * s->mb_height;
  736. s->block_wrap[0] =
  737. s->block_wrap[1] =
  738. s->block_wrap[2] =
  739. s->block_wrap[3] = s->b8_stride;
  740. s->block_wrap[4] =
  741. s->block_wrap[5] = s->mb_stride;
  742. y_size = s->b8_stride * (2 * s->mb_height + 1);
  743. c_size = s->mb_stride * (s->mb_height + 1);
  744. yc_size = y_size + 2 * c_size;
  745. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  746. fail); // error ressilience code looks cleaner with this
  747. for (y = 0; y < s->mb_height; y++)
  748. for (x = 0; x < s->mb_width; x++)
  749. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  750. s->mb_index2xy[s->mb_height * s->mb_width] =
  751. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  752. if (s->encoding) {
  753. /* Allocate MV tables */
  754. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  755. mv_table_size * 2 * sizeof(int16_t), fail);
  756. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  757. mv_table_size * 2 * sizeof(int16_t), fail);
  758. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  759. mv_table_size * 2 * sizeof(int16_t), fail);
  760. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  761. mv_table_size * 2 * sizeof(int16_t), fail);
  762. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  763. mv_table_size * 2 * sizeof(int16_t), fail);
  764. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  765. mv_table_size * 2 * sizeof(int16_t), fail);
  766. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  767. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  768. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  769. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  770. s->mb_stride + 1;
  771. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  772. s->mb_stride + 1;
  773. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  774. /* Allocate MB type table */
  775. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  776. sizeof(uint16_t), fail); // needed for encoding
  777. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  778. sizeof(int), fail);
  779. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  780. mb_array_size * sizeof(float), fail);
  781. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  782. mb_array_size * sizeof(float), fail);
  783. }
  784. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  785. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  786. /* interlaced direct mode decoding tables */
  787. for (i = 0; i < 2; i++) {
  788. int j, k;
  789. for (j = 0; j < 2; j++) {
  790. for (k = 0; k < 2; k++) {
  791. FF_ALLOCZ_OR_GOTO(s->avctx,
  792. s->b_field_mv_table_base[i][j][k],
  793. mv_table_size * 2 * sizeof(int16_t),
  794. fail);
  795. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  796. s->mb_stride + 1;
  797. }
  798. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  799. mb_array_size * 2 * sizeof(uint8_t), fail);
  800. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  801. mv_table_size * 2 * sizeof(int16_t), fail);
  802. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  803. + s->mb_stride + 1;
  804. }
  805. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  806. mb_array_size * 2 * sizeof(uint8_t), fail);
  807. }
  808. }
  809. if (s->out_format == FMT_H263) {
  810. /* cbp values */
  811. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  812. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  813. /* cbp, ac_pred, pred_dir */
  814. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  815. mb_array_size * sizeof(uint8_t), fail);
  816. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  817. mb_array_size * sizeof(uint8_t), fail);
  818. }
  819. if (s->h263_pred || s->h263_plus || !s->encoding) {
  820. /* dc values */
  821. // MN: we need these for error resilience of intra-frames
  822. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  823. yc_size * sizeof(int16_t), fail);
  824. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  825. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  826. s->dc_val[2] = s->dc_val[1] + c_size;
  827. for (i = 0; i < yc_size; i++)
  828. s->dc_val_base[i] = 1024;
  829. }
  830. /* which mb is a intra block */
  831. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  832. memset(s->mbintra_table, 1, mb_array_size);
  833. /* init macroblock skip table */
  834. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  835. // Note the + 1 is for a quicker mpeg4 slice_end detection
  836. return init_er(s);
  837. fail:
  838. return AVERROR(ENOMEM);
  839. }
  840. /**
  841. * init common structure for both encoder and decoder.
  842. * this assumes that some variables like width/height are already set
  843. */
  844. av_cold int ff_MPV_common_init(MpegEncContext *s)
  845. {
  846. int i;
  847. int nb_slices = (HAVE_THREADS &&
  848. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  849. s->avctx->thread_count : 1;
  850. if (s->encoding && s->avctx->slices)
  851. nb_slices = s->avctx->slices;
  852. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  853. s->mb_height = (s->height + 31) / 32 * 2;
  854. else
  855. s->mb_height = (s->height + 15) / 16;
  856. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  857. av_log(s->avctx, AV_LOG_ERROR,
  858. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  859. return -1;
  860. }
  861. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  862. int max_slices;
  863. if (s->mb_height)
  864. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  865. else
  866. max_slices = MAX_THREADS;
  867. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  868. " reducing to %d\n", nb_slices, max_slices);
  869. nb_slices = max_slices;
  870. }
  871. if ((s->width || s->height) &&
  872. av_image_check_size(s->width, s->height, 0, s->avctx))
  873. return -1;
  874. ff_dct_common_init(s);
  875. s->flags = s->avctx->flags;
  876. s->flags2 = s->avctx->flags2;
  877. if (s->width && s->height) {
  878. /* set chroma shifts */
  879. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  880. &s->chroma_x_shift,
  881. &s->chroma_y_shift);
  882. /* convert fourcc to upper case */
  883. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  884. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  885. s->avctx->coded_frame = &s->current_picture.f;
  886. if (s->encoding) {
  887. if (s->msmpeg4_version) {
  888. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  889. 2 * 2 * (MAX_LEVEL + 1) *
  890. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  891. }
  892. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  893. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
  894. 64 * 32 * sizeof(int), fail);
  895. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
  896. 64 * 32 * sizeof(int), fail);
  897. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
  898. 64 * 32 * 2 * sizeof(uint16_t), fail);
  899. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
  900. 64 * 32 * 2 * sizeof(uint16_t), fail);
  901. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  902. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  903. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  904. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  905. if (s->avctx->noise_reduction) {
  906. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  907. 2 * 64 * sizeof(uint16_t), fail);
  908. }
  909. }
  910. }
  911. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  912. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  913. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  914. avcodec_get_frame_defaults(&s->picture[i].f);
  915. }
  916. memset(&s->next_picture, 0, sizeof(s->next_picture));
  917. memset(&s->last_picture, 0, sizeof(s->last_picture));
  918. memset(&s->current_picture, 0, sizeof(s->current_picture));
  919. avcodec_get_frame_defaults(&s->next_picture.f);
  920. avcodec_get_frame_defaults(&s->last_picture.f);
  921. avcodec_get_frame_defaults(&s->current_picture.f);
  922. if (s->width && s->height) {
  923. if (init_context_frame(s))
  924. goto fail;
  925. s->parse_context.state = -1;
  926. }
  927. s->context_initialized = 1;
  928. s->thread_context[0] = s;
  929. if (s->width && s->height) {
  930. if (nb_slices > 1) {
  931. for (i = 1; i < nb_slices; i++) {
  932. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  933. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  934. }
  935. for (i = 0; i < nb_slices; i++) {
  936. if (init_duplicate_context(s->thread_context[i]) < 0)
  937. goto fail;
  938. s->thread_context[i]->start_mb_y =
  939. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  940. s->thread_context[i]->end_mb_y =
  941. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  942. }
  943. } else {
  944. if (init_duplicate_context(s) < 0)
  945. goto fail;
  946. s->start_mb_y = 0;
  947. s->end_mb_y = s->mb_height;
  948. }
  949. s->slice_context_count = nb_slices;
  950. }
  951. return 0;
  952. fail:
  953. ff_MPV_common_end(s);
  954. return -1;
  955. }
  956. /**
  957. * Frees and resets MpegEncContext fields depending on the resolution.
  958. * Is used during resolution changes to avoid a full reinitialization of the
  959. * codec.
  960. */
  961. static int free_context_frame(MpegEncContext *s)
  962. {
  963. int i, j, k;
  964. av_freep(&s->mb_type);
  965. av_freep(&s->p_mv_table_base);
  966. av_freep(&s->b_forw_mv_table_base);
  967. av_freep(&s->b_back_mv_table_base);
  968. av_freep(&s->b_bidir_forw_mv_table_base);
  969. av_freep(&s->b_bidir_back_mv_table_base);
  970. av_freep(&s->b_direct_mv_table_base);
  971. s->p_mv_table = NULL;
  972. s->b_forw_mv_table = NULL;
  973. s->b_back_mv_table = NULL;
  974. s->b_bidir_forw_mv_table = NULL;
  975. s->b_bidir_back_mv_table = NULL;
  976. s->b_direct_mv_table = NULL;
  977. for (i = 0; i < 2; i++) {
  978. for (j = 0; j < 2; j++) {
  979. for (k = 0; k < 2; k++) {
  980. av_freep(&s->b_field_mv_table_base[i][j][k]);
  981. s->b_field_mv_table[i][j][k] = NULL;
  982. }
  983. av_freep(&s->b_field_select_table[i][j]);
  984. av_freep(&s->p_field_mv_table_base[i][j]);
  985. s->p_field_mv_table[i][j] = NULL;
  986. }
  987. av_freep(&s->p_field_select_table[i]);
  988. }
  989. av_freep(&s->dc_val_base);
  990. av_freep(&s->coded_block_base);
  991. av_freep(&s->mbintra_table);
  992. av_freep(&s->cbp_table);
  993. av_freep(&s->pred_dir_table);
  994. av_freep(&s->mbskip_table);
  995. av_freep(&s->er.error_status_table);
  996. av_freep(&s->er.er_temp_buffer);
  997. av_freep(&s->mb_index2xy);
  998. av_freep(&s->lambda_table);
  999. av_freep(&s->cplx_tab);
  1000. av_freep(&s->bits_tab);
  1001. s->linesize = s->uvlinesize = 0;
  1002. return 0;
  1003. }
  1004. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1005. {
  1006. int i, err = 0;
  1007. if (s->slice_context_count > 1) {
  1008. for (i = 0; i < s->slice_context_count; i++) {
  1009. free_duplicate_context(s->thread_context[i]);
  1010. }
  1011. for (i = 1; i < s->slice_context_count; i++) {
  1012. av_freep(&s->thread_context[i]);
  1013. }
  1014. } else
  1015. free_duplicate_context(s);
  1016. if ((err = free_context_frame(s)) < 0)
  1017. return err;
  1018. if (s->picture)
  1019. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1020. s->picture[i].needs_realloc = 1;
  1021. }
  1022. s->last_picture_ptr =
  1023. s->next_picture_ptr =
  1024. s->current_picture_ptr = NULL;
  1025. // init
  1026. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1027. s->mb_height = (s->height + 31) / 32 * 2;
  1028. else
  1029. s->mb_height = (s->height + 15) / 16;
  1030. if ((s->width || s->height) &&
  1031. av_image_check_size(s->width, s->height, 0, s->avctx))
  1032. return AVERROR_INVALIDDATA;
  1033. if ((err = init_context_frame(s)))
  1034. goto fail;
  1035. s->thread_context[0] = s;
  1036. if (s->width && s->height) {
  1037. int nb_slices = s->slice_context_count;
  1038. if (nb_slices > 1) {
  1039. for (i = 1; i < nb_slices; i++) {
  1040. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1041. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1042. }
  1043. for (i = 0; i < nb_slices; i++) {
  1044. if (init_duplicate_context(s->thread_context[i]) < 0)
  1045. goto fail;
  1046. s->thread_context[i]->start_mb_y =
  1047. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1048. s->thread_context[i]->end_mb_y =
  1049. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1050. }
  1051. } else {
  1052. if (init_duplicate_context(s) < 0)
  1053. goto fail;
  1054. s->start_mb_y = 0;
  1055. s->end_mb_y = s->mb_height;
  1056. }
  1057. s->slice_context_count = nb_slices;
  1058. }
  1059. return 0;
  1060. fail:
  1061. ff_MPV_common_end(s);
  1062. return err;
  1063. }
  1064. /* init common structure for both encoder and decoder */
  1065. void ff_MPV_common_end(MpegEncContext *s)
  1066. {
  1067. int i;
  1068. if (s->slice_context_count > 1) {
  1069. for (i = 0; i < s->slice_context_count; i++) {
  1070. free_duplicate_context(s->thread_context[i]);
  1071. }
  1072. for (i = 1; i < s->slice_context_count; i++) {
  1073. av_freep(&s->thread_context[i]);
  1074. }
  1075. s->slice_context_count = 1;
  1076. } else free_duplicate_context(s);
  1077. av_freep(&s->parse_context.buffer);
  1078. s->parse_context.buffer_size = 0;
  1079. av_freep(&s->bitstream_buffer);
  1080. s->allocated_bitstream_buffer_size = 0;
  1081. av_freep(&s->avctx->stats_out);
  1082. av_freep(&s->ac_stats);
  1083. av_freep(&s->q_intra_matrix);
  1084. av_freep(&s->q_inter_matrix);
  1085. av_freep(&s->q_intra_matrix16);
  1086. av_freep(&s->q_inter_matrix16);
  1087. av_freep(&s->input_picture);
  1088. av_freep(&s->reordered_input_picture);
  1089. av_freep(&s->dct_offset);
  1090. if (s->picture) {
  1091. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1092. free_picture_tables(&s->picture[i]);
  1093. ff_mpeg_unref_picture(s, &s->picture[i]);
  1094. }
  1095. }
  1096. av_freep(&s->picture);
  1097. free_picture_tables(&s->last_picture);
  1098. ff_mpeg_unref_picture(s, &s->last_picture);
  1099. free_picture_tables(&s->current_picture);
  1100. ff_mpeg_unref_picture(s, &s->current_picture);
  1101. free_picture_tables(&s->next_picture);
  1102. ff_mpeg_unref_picture(s, &s->next_picture);
  1103. free_picture_tables(&s->new_picture);
  1104. ff_mpeg_unref_picture(s, &s->new_picture);
  1105. free_context_frame(s);
  1106. s->context_initialized = 0;
  1107. s->last_picture_ptr =
  1108. s->next_picture_ptr =
  1109. s->current_picture_ptr = NULL;
  1110. s->linesize = s->uvlinesize = 0;
  1111. }
  1112. void ff_init_rl(RLTable *rl,
  1113. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1114. {
  1115. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1116. uint8_t index_run[MAX_RUN + 1];
  1117. int last, run, level, start, end, i;
  1118. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1119. if (static_store && rl->max_level[0])
  1120. return;
  1121. /* compute max_level[], max_run[] and index_run[] */
  1122. for (last = 0; last < 2; last++) {
  1123. if (last == 0) {
  1124. start = 0;
  1125. end = rl->last;
  1126. } else {
  1127. start = rl->last;
  1128. end = rl->n;
  1129. }
  1130. memset(max_level, 0, MAX_RUN + 1);
  1131. memset(max_run, 0, MAX_LEVEL + 1);
  1132. memset(index_run, rl->n, MAX_RUN + 1);
  1133. for (i = start; i < end; i++) {
  1134. run = rl->table_run[i];
  1135. level = rl->table_level[i];
  1136. if (index_run[run] == rl->n)
  1137. index_run[run] = i;
  1138. if (level > max_level[run])
  1139. max_level[run] = level;
  1140. if (run > max_run[level])
  1141. max_run[level] = run;
  1142. }
  1143. if (static_store)
  1144. rl->max_level[last] = static_store[last];
  1145. else
  1146. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1147. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1148. if (static_store)
  1149. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1150. else
  1151. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1152. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1153. if (static_store)
  1154. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1155. else
  1156. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1157. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1158. }
  1159. }
  1160. void ff_init_vlc_rl(RLTable *rl)
  1161. {
  1162. int i, q;
  1163. for (q = 0; q < 32; q++) {
  1164. int qmul = q * 2;
  1165. int qadd = (q - 1) | 1;
  1166. if (q == 0) {
  1167. qmul = 1;
  1168. qadd = 0;
  1169. }
  1170. for (i = 0; i < rl->vlc.table_size; i++) {
  1171. int code = rl->vlc.table[i][0];
  1172. int len = rl->vlc.table[i][1];
  1173. int level, run;
  1174. if (len == 0) { // illegal code
  1175. run = 66;
  1176. level = MAX_LEVEL;
  1177. } else if (len < 0) { // more bits needed
  1178. run = 0;
  1179. level = code;
  1180. } else {
  1181. if (code == rl->n) { // esc
  1182. run = 66;
  1183. level = 0;
  1184. } else {
  1185. run = rl->table_run[code] + 1;
  1186. level = rl->table_level[code] * qmul + qadd;
  1187. if (code >= rl->last) run += 192;
  1188. }
  1189. }
  1190. rl->rl_vlc[q][i].len = len;
  1191. rl->rl_vlc[q][i].level = level;
  1192. rl->rl_vlc[q][i].run = run;
  1193. }
  1194. }
  1195. }
  1196. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1197. {
  1198. int i;
  1199. /* release non reference frames */
  1200. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1201. if (!s->picture[i].reference &&
  1202. (remove_current || &s->picture[i] != s->current_picture_ptr)) {
  1203. ff_mpeg_unref_picture(s, &s->picture[i]);
  1204. }
  1205. }
  1206. }
  1207. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1208. {
  1209. if (pic->f.data[0] == NULL)
  1210. return 1;
  1211. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1212. return 1;
  1213. return 0;
  1214. }
  1215. static int find_unused_picture(MpegEncContext *s, int shared)
  1216. {
  1217. int i;
  1218. if (shared) {
  1219. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1220. if (s->picture[i].f.data[0] == NULL)
  1221. return i;
  1222. }
  1223. } else {
  1224. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1225. if (pic_is_unused(s, &s->picture[i]))
  1226. return i;
  1227. }
  1228. }
  1229. return AVERROR_INVALIDDATA;
  1230. }
  1231. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1232. {
  1233. int ret = find_unused_picture(s, shared);
  1234. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1235. if (s->picture[ret].needs_realloc) {
  1236. s->picture[ret].needs_realloc = 0;
  1237. free_picture_tables(&s->picture[ret]);
  1238. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1239. avcodec_get_frame_defaults(&s->picture[ret].f);
  1240. }
  1241. }
  1242. return ret;
  1243. }
  1244. static void update_noise_reduction(MpegEncContext *s)
  1245. {
  1246. int intra, i;
  1247. for (intra = 0; intra < 2; intra++) {
  1248. if (s->dct_count[intra] > (1 << 16)) {
  1249. for (i = 0; i < 64; i++) {
  1250. s->dct_error_sum[intra][i] >>= 1;
  1251. }
  1252. s->dct_count[intra] >>= 1;
  1253. }
  1254. for (i = 0; i < 64; i++) {
  1255. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1256. s->dct_count[intra] +
  1257. s->dct_error_sum[intra][i] / 2) /
  1258. (s->dct_error_sum[intra][i] + 1);
  1259. }
  1260. }
  1261. }
  1262. /**
  1263. * generic function for encode/decode called after coding/decoding
  1264. * the header and before a frame is coded/decoded.
  1265. */
  1266. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1267. {
  1268. int i, ret;
  1269. Picture *pic;
  1270. s->mb_skipped = 0;
  1271. /* mark & release old frames */
  1272. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1273. s->last_picture_ptr != s->next_picture_ptr &&
  1274. s->last_picture_ptr->f.data[0]) {
  1275. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1276. }
  1277. /* release forgotten pictures */
  1278. /* if (mpeg124/h263) */
  1279. if (!s->encoding) {
  1280. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1281. if (&s->picture[i] != s->last_picture_ptr &&
  1282. &s->picture[i] != s->next_picture_ptr &&
  1283. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1284. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1285. av_log(avctx, AV_LOG_ERROR,
  1286. "releasing zombie picture\n");
  1287. ff_mpeg_unref_picture(s, &s->picture[i]);
  1288. }
  1289. }
  1290. }
  1291. if (!s->encoding) {
  1292. ff_release_unused_pictures(s, 1);
  1293. if (s->current_picture_ptr &&
  1294. s->current_picture_ptr->f.data[0] == NULL) {
  1295. // we already have a unused image
  1296. // (maybe it was set before reading the header)
  1297. pic = s->current_picture_ptr;
  1298. } else {
  1299. i = ff_find_unused_picture(s, 0);
  1300. if (i < 0) {
  1301. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1302. return i;
  1303. }
  1304. pic = &s->picture[i];
  1305. }
  1306. pic->reference = 0;
  1307. if (!s->droppable) {
  1308. if (s->pict_type != AV_PICTURE_TYPE_B)
  1309. pic->reference = 3;
  1310. }
  1311. pic->f.coded_picture_number = s->coded_picture_number++;
  1312. if (ff_alloc_picture(s, pic, 0) < 0)
  1313. return -1;
  1314. s->current_picture_ptr = pic;
  1315. // FIXME use only the vars from current_pic
  1316. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1317. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1318. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1319. if (s->picture_structure != PICT_FRAME)
  1320. s->current_picture_ptr->f.top_field_first =
  1321. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1322. }
  1323. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1324. !s->progressive_sequence;
  1325. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1326. }
  1327. s->current_picture_ptr->f.pict_type = s->pict_type;
  1328. // if (s->flags && CODEC_FLAG_QSCALE)
  1329. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1330. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1331. ff_mpeg_unref_picture(s, &s->current_picture);
  1332. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1333. s->current_picture_ptr)) < 0)
  1334. return ret;
  1335. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1336. s->last_picture_ptr = s->next_picture_ptr;
  1337. if (!s->droppable)
  1338. s->next_picture_ptr = s->current_picture_ptr;
  1339. }
  1340. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1341. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1342. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1343. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1344. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1345. s->pict_type, s->droppable);
  1346. if ((s->last_picture_ptr == NULL ||
  1347. s->last_picture_ptr->f.data[0] == NULL) &&
  1348. (s->pict_type != AV_PICTURE_TYPE_I ||
  1349. s->picture_structure != PICT_FRAME)) {
  1350. int h_chroma_shift, v_chroma_shift;
  1351. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1352. &h_chroma_shift, &v_chroma_shift);
  1353. if (s->pict_type != AV_PICTURE_TYPE_I)
  1354. av_log(avctx, AV_LOG_ERROR,
  1355. "warning: first frame is no keyframe\n");
  1356. else if (s->picture_structure != PICT_FRAME)
  1357. av_log(avctx, AV_LOG_INFO,
  1358. "allocate dummy last picture for field based first keyframe\n");
  1359. /* Allocate a dummy frame */
  1360. i = ff_find_unused_picture(s, 0);
  1361. if (i < 0) {
  1362. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1363. return i;
  1364. }
  1365. s->last_picture_ptr = &s->picture[i];
  1366. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1367. s->last_picture_ptr = NULL;
  1368. return -1;
  1369. }
  1370. memset(s->last_picture_ptr->f.data[0], 0,
  1371. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1372. memset(s->last_picture_ptr->f.data[1], 0x80,
  1373. (avctx->height >> v_chroma_shift) *
  1374. s->last_picture_ptr->f.linesize[1]);
  1375. memset(s->last_picture_ptr->f.data[2], 0x80,
  1376. (avctx->height >> v_chroma_shift) *
  1377. s->last_picture_ptr->f.linesize[2]);
  1378. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1379. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1380. }
  1381. if ((s->next_picture_ptr == NULL ||
  1382. s->next_picture_ptr->f.data[0] == NULL) &&
  1383. s->pict_type == AV_PICTURE_TYPE_B) {
  1384. /* Allocate a dummy frame */
  1385. i = ff_find_unused_picture(s, 0);
  1386. if (i < 0) {
  1387. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1388. return i;
  1389. }
  1390. s->next_picture_ptr = &s->picture[i];
  1391. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1392. s->next_picture_ptr = NULL;
  1393. return -1;
  1394. }
  1395. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1396. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1397. }
  1398. if (s->last_picture_ptr) {
  1399. ff_mpeg_unref_picture(s, &s->last_picture);
  1400. if (s->last_picture_ptr->f.data[0] &&
  1401. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1402. s->last_picture_ptr)) < 0)
  1403. return ret;
  1404. }
  1405. if (s->next_picture_ptr) {
  1406. ff_mpeg_unref_picture(s, &s->next_picture);
  1407. if (s->next_picture_ptr->f.data[0] &&
  1408. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1409. s->next_picture_ptr)) < 0)
  1410. return ret;
  1411. }
  1412. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1413. s->last_picture_ptr->f.data[0]));
  1414. if (s->picture_structure!= PICT_FRAME) {
  1415. int i;
  1416. for (i = 0; i < 4; i++) {
  1417. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1418. s->current_picture.f.data[i] +=
  1419. s->current_picture.f.linesize[i];
  1420. }
  1421. s->current_picture.f.linesize[i] *= 2;
  1422. s->last_picture.f.linesize[i] *= 2;
  1423. s->next_picture.f.linesize[i] *= 2;
  1424. }
  1425. }
  1426. s->err_recognition = avctx->err_recognition;
  1427. /* set dequantizer, we can't do it during init as
  1428. * it might change for mpeg4 and we can't do it in the header
  1429. * decode as init is not called for mpeg4 there yet */
  1430. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1431. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1432. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1433. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1434. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1435. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1436. } else {
  1437. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1438. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1439. }
  1440. if (s->dct_error_sum) {
  1441. assert(s->avctx->noise_reduction && s->encoding);
  1442. update_noise_reduction(s);
  1443. }
  1444. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1445. return ff_xvmc_field_start(s, avctx);
  1446. return 0;
  1447. }
  1448. /* generic function for encode/decode called after a
  1449. * frame has been coded/decoded. */
  1450. void ff_MPV_frame_end(MpegEncContext *s)
  1451. {
  1452. int i;
  1453. /* redraw edges for the frame if decoding didn't complete */
  1454. // just to make sure that all data is rendered.
  1455. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1456. ff_xvmc_field_end(s);
  1457. } else if ((s->er.error_count || s->encoding) &&
  1458. !s->avctx->hwaccel &&
  1459. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1460. s->unrestricted_mv &&
  1461. s->current_picture.reference &&
  1462. !s->intra_only &&
  1463. !(s->flags & CODEC_FLAG_EMU_EDGE)) {
  1464. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
  1465. int hshift = desc->log2_chroma_w;
  1466. int vshift = desc->log2_chroma_h;
  1467. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1468. s->h_edge_pos, s->v_edge_pos,
  1469. EDGE_WIDTH, EDGE_WIDTH,
  1470. EDGE_TOP | EDGE_BOTTOM);
  1471. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1472. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1473. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1474. EDGE_TOP | EDGE_BOTTOM);
  1475. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1476. s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
  1477. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
  1478. EDGE_TOP | EDGE_BOTTOM);
  1479. }
  1480. emms_c();
  1481. s->last_pict_type = s->pict_type;
  1482. s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
  1483. if (s->pict_type!= AV_PICTURE_TYPE_B) {
  1484. s->last_non_b_pict_type = s->pict_type;
  1485. }
  1486. #if 0
  1487. /* copy back current_picture variables */
  1488. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1489. if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {
  1490. s->picture[i] = s->current_picture;
  1491. break;
  1492. }
  1493. }
  1494. assert(i < MAX_PICTURE_COUNT);
  1495. #endif
  1496. if (s->encoding) {
  1497. /* release non-reference frames */
  1498. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1499. if (!s->picture[i].reference)
  1500. ff_mpeg_unref_picture(s, &s->picture[i]);
  1501. }
  1502. }
  1503. // clear copies, to avoid confusion
  1504. #if 0
  1505. memset(&s->last_picture, 0, sizeof(Picture));
  1506. memset(&s->next_picture, 0, sizeof(Picture));
  1507. memset(&s->current_picture, 0, sizeof(Picture));
  1508. #endif
  1509. s->avctx->coded_frame = &s->current_picture_ptr->f;
  1510. if (s->current_picture.reference)
  1511. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1512. }
  1513. /**
  1514. * Print debugging info for the given picture.
  1515. */
  1516. void ff_print_debug_info(MpegEncContext *s, Picture *p)
  1517. {
  1518. AVFrame *pict;
  1519. if (s->avctx->hwaccel || !p || !p->mb_type)
  1520. return;
  1521. pict = &p->f;
  1522. if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1523. int x,y;
  1524. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1525. switch (pict->pict_type) {
  1526. case AV_PICTURE_TYPE_I:
  1527. av_log(s->avctx,AV_LOG_DEBUG,"I\n");
  1528. break;
  1529. case AV_PICTURE_TYPE_P:
  1530. av_log(s->avctx,AV_LOG_DEBUG,"P\n");
  1531. break;
  1532. case AV_PICTURE_TYPE_B:
  1533. av_log(s->avctx,AV_LOG_DEBUG,"B\n");
  1534. break;
  1535. case AV_PICTURE_TYPE_S:
  1536. av_log(s->avctx,AV_LOG_DEBUG,"S\n");
  1537. break;
  1538. case AV_PICTURE_TYPE_SI:
  1539. av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
  1540. break;
  1541. case AV_PICTURE_TYPE_SP:
  1542. av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
  1543. break;
  1544. }
  1545. for (y = 0; y < s->mb_height; y++) {
  1546. for (x = 0; x < s->mb_width; x++) {
  1547. if (s->avctx->debug & FF_DEBUG_SKIP) {
  1548. int count = s->mbskip_table[x + y * s->mb_stride];
  1549. if (count > 9)
  1550. count = 9;
  1551. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1552. }
  1553. if (s->avctx->debug & FF_DEBUG_QP) {
  1554. av_log(s->avctx, AV_LOG_DEBUG, "%2d",
  1555. p->qscale_table[x + y * s->mb_stride]);
  1556. }
  1557. if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
  1558. int mb_type = p->mb_type[x + y * s->mb_stride];
  1559. // Type & MV direction
  1560. if (IS_PCM(mb_type))
  1561. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1562. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1563. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1564. else if (IS_INTRA4x4(mb_type))
  1565. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1566. else if (IS_INTRA16x16(mb_type))
  1567. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1568. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1569. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1570. else if (IS_DIRECT(mb_type))
  1571. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1572. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1573. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1574. else if (IS_GMC(mb_type))
  1575. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1576. else if (IS_SKIP(mb_type))
  1577. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1578. else if (!USES_LIST(mb_type, 1))
  1579. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1580. else if (!USES_LIST(mb_type, 0))
  1581. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1582. else {
  1583. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1584. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1585. }
  1586. // segmentation
  1587. if (IS_8X8(mb_type))
  1588. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1589. else if (IS_16X8(mb_type))
  1590. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1591. else if (IS_8X16(mb_type))
  1592. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1593. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1594. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1595. else
  1596. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1597. if (IS_INTERLACED(mb_type))
  1598. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1599. else
  1600. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1601. }
  1602. }
  1603. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1604. }
  1605. }
  1606. }
  1607. /**
  1608. * find the lowest MB row referenced in the MVs
  1609. */
  1610. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1611. {
  1612. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1613. int my, off, i, mvs;
  1614. if (s->picture_structure != PICT_FRAME || s->mcsel)
  1615. goto unhandled;
  1616. switch (s->mv_type) {
  1617. case MV_TYPE_16X16:
  1618. mvs = 1;
  1619. break;
  1620. case MV_TYPE_16X8:
  1621. mvs = 2;
  1622. break;
  1623. case MV_TYPE_8X8:
  1624. mvs = 4;
  1625. break;
  1626. default:
  1627. goto unhandled;
  1628. }
  1629. for (i = 0; i < mvs; i++) {
  1630. my = s->mv[dir][i][1]<<qpel_shift;
  1631. my_max = FFMAX(my_max, my);
  1632. my_min = FFMIN(my_min, my);
  1633. }
  1634. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1635. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1636. unhandled:
  1637. return s->mb_height-1;
  1638. }
  1639. /* put block[] to dest[] */
  1640. static inline void put_dct(MpegEncContext *s,
  1641. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1642. {
  1643. s->dct_unquantize_intra(s, block, i, qscale);
  1644. s->dsp.idct_put (dest, line_size, block);
  1645. }
  1646. /* add block[] to dest[] */
  1647. static inline void add_dct(MpegEncContext *s,
  1648. int16_t *block, int i, uint8_t *dest, int line_size)
  1649. {
  1650. if (s->block_last_index[i] >= 0) {
  1651. s->dsp.idct_add (dest, line_size, block);
  1652. }
  1653. }
  1654. static inline void add_dequant_dct(MpegEncContext *s,
  1655. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  1656. {
  1657. if (s->block_last_index[i] >= 0) {
  1658. s->dct_unquantize_inter(s, block, i, qscale);
  1659. s->dsp.idct_add (dest, line_size, block);
  1660. }
  1661. }
  1662. /**
  1663. * Clean dc, ac, coded_block for the current non-intra MB.
  1664. */
  1665. void ff_clean_intra_table_entries(MpegEncContext *s)
  1666. {
  1667. int wrap = s->b8_stride;
  1668. int xy = s->block_index[0];
  1669. s->dc_val[0][xy ] =
  1670. s->dc_val[0][xy + 1 ] =
  1671. s->dc_val[0][xy + wrap] =
  1672. s->dc_val[0][xy + 1 + wrap] = 1024;
  1673. /* ac pred */
  1674. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1675. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1676. if (s->msmpeg4_version>=3) {
  1677. s->coded_block[xy ] =
  1678. s->coded_block[xy + 1 ] =
  1679. s->coded_block[xy + wrap] =
  1680. s->coded_block[xy + 1 + wrap] = 0;
  1681. }
  1682. /* chroma */
  1683. wrap = s->mb_stride;
  1684. xy = s->mb_x + s->mb_y * wrap;
  1685. s->dc_val[1][xy] =
  1686. s->dc_val[2][xy] = 1024;
  1687. /* ac pred */
  1688. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1689. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1690. s->mbintra_table[xy]= 0;
  1691. }
  1692. /* generic function called after a macroblock has been parsed by the
  1693. decoder or after it has been encoded by the encoder.
  1694. Important variables used:
  1695. s->mb_intra : true if intra macroblock
  1696. s->mv_dir : motion vector direction
  1697. s->mv_type : motion vector type
  1698. s->mv : motion vector
  1699. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1700. */
  1701. static av_always_inline
  1702. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  1703. int is_mpeg12)
  1704. {
  1705. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1706. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1707. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1708. return;
  1709. }
  1710. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1711. /* print DCT coefficients */
  1712. int i,j;
  1713. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1714. for(i=0; i<6; i++){
  1715. for(j=0; j<64; j++){
  1716. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  1717. }
  1718. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1719. }
  1720. }
  1721. s->current_picture.qscale_table[mb_xy] = s->qscale;
  1722. /* update DC predictors for P macroblocks */
  1723. if (!s->mb_intra) {
  1724. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1725. if(s->mbintra_table[mb_xy])
  1726. ff_clean_intra_table_entries(s);
  1727. } else {
  1728. s->last_dc[0] =
  1729. s->last_dc[1] =
  1730. s->last_dc[2] = 128 << s->intra_dc_precision;
  1731. }
  1732. }
  1733. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1734. s->mbintra_table[mb_xy]=1;
  1735. 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
  1736. uint8_t *dest_y, *dest_cb, *dest_cr;
  1737. int dct_linesize, dct_offset;
  1738. op_pixels_func (*op_pix)[4];
  1739. qpel_mc_func (*op_qpix)[16];
  1740. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1741. const int uvlinesize = s->current_picture.f.linesize[1];
  1742. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
  1743. const int block_size = 8;
  1744. /* avoid copy if macroblock skipped in last frame too */
  1745. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1746. if(!s->encoding){
  1747. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1748. if (s->mb_skipped) {
  1749. s->mb_skipped= 0;
  1750. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1751. *mbskip_ptr = 1;
  1752. } else if(!s->current_picture.reference) {
  1753. *mbskip_ptr = 1;
  1754. } else{
  1755. *mbskip_ptr = 0; /* not skipped */
  1756. }
  1757. }
  1758. dct_linesize = linesize << s->interlaced_dct;
  1759. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  1760. if(readable){
  1761. dest_y= s->dest[0];
  1762. dest_cb= s->dest[1];
  1763. dest_cr= s->dest[2];
  1764. }else{
  1765. dest_y = s->b_scratchpad;
  1766. dest_cb= s->b_scratchpad+16*linesize;
  1767. dest_cr= s->b_scratchpad+32*linesize;
  1768. }
  1769. if (!s->mb_intra) {
  1770. /* motion handling */
  1771. /* decoding or more than one mb_type (MC was already done otherwise) */
  1772. if(!s->encoding){
  1773. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1774. if (s->mv_dir & MV_DIR_FORWARD) {
  1775. ff_thread_await_progress(&s->last_picture_ptr->tf,
  1776. ff_MPV_lowest_referenced_row(s, 0),
  1777. 0);
  1778. }
  1779. if (s->mv_dir & MV_DIR_BACKWARD) {
  1780. ff_thread_await_progress(&s->next_picture_ptr->tf,
  1781. ff_MPV_lowest_referenced_row(s, 1),
  1782. 0);
  1783. }
  1784. }
  1785. op_qpix= s->me.qpel_put;
  1786. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1787. op_pix = s->hdsp.put_pixels_tab;
  1788. }else{
  1789. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  1790. }
  1791. if (s->mv_dir & MV_DIR_FORWARD) {
  1792. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1793. op_pix = s->hdsp.avg_pixels_tab;
  1794. op_qpix= s->me.qpel_avg;
  1795. }
  1796. if (s->mv_dir & MV_DIR_BACKWARD) {
  1797. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1798. }
  1799. }
  1800. /* skip dequant / idct if we are really late ;) */
  1801. if(s->avctx->skip_idct){
  1802. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1803. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1804. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1805. goto skip_idct;
  1806. }
  1807. /* add dct residue */
  1808. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  1809. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1810. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1811. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1812. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1813. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1814. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1815. if (s->chroma_y_shift){
  1816. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1817. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1818. }else{
  1819. dct_linesize >>= 1;
  1820. dct_offset >>=1;
  1821. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1822. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1823. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1824. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1825. }
  1826. }
  1827. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  1828. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1829. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1830. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1831. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1832. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1833. if(s->chroma_y_shift){//Chroma420
  1834. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1835. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1836. }else{
  1837. //chroma422
  1838. dct_linesize = uvlinesize << s->interlaced_dct;
  1839. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1840. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1841. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1842. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1843. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1844. if(!s->chroma_x_shift){//Chroma444
  1845. add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
  1846. add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
  1847. add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
  1848. add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
  1849. }
  1850. }
  1851. }//fi gray
  1852. }
  1853. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1854. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1855. }
  1856. } else {
  1857. /* dct only in intra block */
  1858. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  1859. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1860. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1861. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1862. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1863. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1864. if(s->chroma_y_shift){
  1865. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1866. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1867. }else{
  1868. dct_offset >>=1;
  1869. dct_linesize >>=1;
  1870. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1871. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1872. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1873. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1874. }
  1875. }
  1876. }else{
  1877. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1878. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1879. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1880. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1881. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1882. if(s->chroma_y_shift){
  1883. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1884. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1885. }else{
  1886. dct_linesize = uvlinesize << s->interlaced_dct;
  1887. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
  1888. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1889. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1890. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1891. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1892. if(!s->chroma_x_shift){//Chroma444
  1893. s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
  1894. s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
  1895. s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
  1896. s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
  1897. }
  1898. }
  1899. }//gray
  1900. }
  1901. }
  1902. skip_idct:
  1903. if(!readable){
  1904. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1905. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1906. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1907. }
  1908. }
  1909. }
  1910. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  1911. #if !CONFIG_SMALL
  1912. if(s->out_format == FMT_MPEG1) {
  1913. MPV_decode_mb_internal(s, block, 1);
  1914. } else
  1915. #endif
  1916. MPV_decode_mb_internal(s, block, 0);
  1917. }
  1918. /**
  1919. * @param h is the normal height, this will be reduced automatically if needed for the last row
  1920. */
  1921. void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur,
  1922. Picture *last, int y, int h, int picture_structure,
  1923. int first_field, int draw_edges, int low_delay,
  1924. int v_edge_pos, int h_edge_pos)
  1925. {
  1926. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1927. int hshift = desc->log2_chroma_w;
  1928. int vshift = desc->log2_chroma_h;
  1929. const int field_pic = picture_structure != PICT_FRAME;
  1930. if(field_pic){
  1931. h <<= 1;
  1932. y <<= 1;
  1933. }
  1934. if (!avctx->hwaccel &&
  1935. !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1936. draw_edges &&
  1937. cur->reference &&
  1938. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1939. int *linesize = cur->f.linesize;
  1940. int sides = 0, edge_h;
  1941. if (y==0) sides |= EDGE_TOP;
  1942. if (y + h >= v_edge_pos)
  1943. sides |= EDGE_BOTTOM;
  1944. edge_h= FFMIN(h, v_edge_pos - y);
  1945. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  1946. linesize[0], h_edge_pos, edge_h,
  1947. EDGE_WIDTH, EDGE_WIDTH, sides);
  1948. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  1949. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  1950. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1951. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  1952. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  1953. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1954. }
  1955. h = FFMIN(h, avctx->height - y);
  1956. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1957. if (avctx->draw_horiz_band) {
  1958. AVFrame *src;
  1959. int offset[AV_NUM_DATA_POINTERS];
  1960. int i;
  1961. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  1962. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  1963. src = &cur->f;
  1964. else if (last)
  1965. src = &last->f;
  1966. else
  1967. return;
  1968. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  1969. picture_structure == PICT_FRAME &&
  1970. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  1971. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  1972. offset[i] = 0;
  1973. }else{
  1974. offset[0]= y * src->linesize[0];
  1975. offset[1]=
  1976. offset[2]= (y >> vshift) * src->linesize[1];
  1977. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  1978. offset[i] = 0;
  1979. }
  1980. emms_c();
  1981. avctx->draw_horiz_band(avctx, src, offset,
  1982. y, picture_structure, h);
  1983. }
  1984. }
  1985. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1986. {
  1987. int draw_edges = s->unrestricted_mv && !s->intra_only;
  1988. ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
  1989. &s->last_picture, y, h, s->picture_structure,
  1990. s->first_field, draw_edges, s->low_delay,
  1991. s->v_edge_pos, s->h_edge_pos);
  1992. }
  1993. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1994. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1995. const int uvlinesize = s->current_picture.f.linesize[1];
  1996. const int mb_size= 4;
  1997. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1998. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1999. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2000. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2001. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2002. 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;
  2003. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2004. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2005. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2006. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2007. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2008. {
  2009. if(s->picture_structure==PICT_FRAME){
  2010. s->dest[0] += s->mb_y * linesize << mb_size;
  2011. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2012. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2013. }else{
  2014. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2015. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2016. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2017. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2018. }
  2019. }
  2020. }
  2021. /**
  2022. * Permute an 8x8 block.
  2023. * @param block the block which will be permuted according to the given permutation vector
  2024. * @param permutation the permutation vector
  2025. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2026. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2027. * (inverse) permutated to scantable order!
  2028. */
  2029. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2030. {
  2031. int i;
  2032. int16_t temp[64];
  2033. if(last<=0) return;
  2034. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2035. for(i=0; i<=last; i++){
  2036. const int j= scantable[i];
  2037. temp[j]= block[j];
  2038. block[j]=0;
  2039. }
  2040. for(i=0; i<=last; i++){
  2041. const int j= scantable[i];
  2042. const int perm_j= permutation[j];
  2043. block[perm_j]= temp[j];
  2044. }
  2045. }
  2046. void ff_mpeg_flush(AVCodecContext *avctx){
  2047. int i;
  2048. MpegEncContext *s = avctx->priv_data;
  2049. if(s==NULL || s->picture==NULL)
  2050. return;
  2051. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2052. ff_mpeg_unref_picture(s, &s->picture[i]);
  2053. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2054. ff_mpeg_unref_picture(s, &s->current_picture);
  2055. ff_mpeg_unref_picture(s, &s->last_picture);
  2056. ff_mpeg_unref_picture(s, &s->next_picture);
  2057. s->mb_x= s->mb_y= 0;
  2058. s->parse_context.state= -1;
  2059. s->parse_context.frame_start_found= 0;
  2060. s->parse_context.overread= 0;
  2061. s->parse_context.overread_index= 0;
  2062. s->parse_context.index= 0;
  2063. s->parse_context.last_index= 0;
  2064. s->bitstream_buffer_size=0;
  2065. s->pp_time=0;
  2066. }
  2067. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2068. int16_t *block, int n, int qscale)
  2069. {
  2070. int i, level, nCoeffs;
  2071. const uint16_t *quant_matrix;
  2072. nCoeffs= s->block_last_index[n];
  2073. if (n < 4)
  2074. block[0] = block[0] * s->y_dc_scale;
  2075. else
  2076. block[0] = block[0] * s->c_dc_scale;
  2077. /* XXX: only mpeg1 */
  2078. quant_matrix = s->intra_matrix;
  2079. for(i=1;i<=nCoeffs;i++) {
  2080. int j= s->intra_scantable.permutated[i];
  2081. level = block[j];
  2082. if (level) {
  2083. if (level < 0) {
  2084. level = -level;
  2085. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2086. level = (level - 1) | 1;
  2087. level = -level;
  2088. } else {
  2089. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2090. level = (level - 1) | 1;
  2091. }
  2092. block[j] = level;
  2093. }
  2094. }
  2095. }
  2096. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2097. int16_t *block, int n, int qscale)
  2098. {
  2099. int i, level, nCoeffs;
  2100. const uint16_t *quant_matrix;
  2101. nCoeffs= s->block_last_index[n];
  2102. quant_matrix = s->inter_matrix;
  2103. for(i=0; i<=nCoeffs; i++) {
  2104. int j= s->intra_scantable.permutated[i];
  2105. level = block[j];
  2106. if (level) {
  2107. if (level < 0) {
  2108. level = -level;
  2109. level = (((level << 1) + 1) * qscale *
  2110. ((int) (quant_matrix[j]))) >> 4;
  2111. level = (level - 1) | 1;
  2112. level = -level;
  2113. } else {
  2114. level = (((level << 1) + 1) * qscale *
  2115. ((int) (quant_matrix[j]))) >> 4;
  2116. level = (level - 1) | 1;
  2117. }
  2118. block[j] = level;
  2119. }
  2120. }
  2121. }
  2122. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2123. int16_t *block, int n, int qscale)
  2124. {
  2125. int i, level, nCoeffs;
  2126. const uint16_t *quant_matrix;
  2127. if(s->alternate_scan) nCoeffs= 63;
  2128. else nCoeffs= s->block_last_index[n];
  2129. if (n < 4)
  2130. block[0] = block[0] * s->y_dc_scale;
  2131. else
  2132. block[0] = block[0] * s->c_dc_scale;
  2133. quant_matrix = s->intra_matrix;
  2134. for(i=1;i<=nCoeffs;i++) {
  2135. int j= s->intra_scantable.permutated[i];
  2136. level = block[j];
  2137. if (level) {
  2138. if (level < 0) {
  2139. level = -level;
  2140. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2141. level = -level;
  2142. } else {
  2143. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2144. }
  2145. block[j] = level;
  2146. }
  2147. }
  2148. }
  2149. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2150. int16_t *block, int n, int qscale)
  2151. {
  2152. int i, level, nCoeffs;
  2153. const uint16_t *quant_matrix;
  2154. int sum=-1;
  2155. if(s->alternate_scan) nCoeffs= 63;
  2156. else nCoeffs= s->block_last_index[n];
  2157. if (n < 4)
  2158. block[0] = block[0] * s->y_dc_scale;
  2159. else
  2160. block[0] = block[0] * s->c_dc_scale;
  2161. quant_matrix = s->intra_matrix;
  2162. for(i=1;i<=nCoeffs;i++) {
  2163. int j= s->intra_scantable.permutated[i];
  2164. level = block[j];
  2165. if (level) {
  2166. if (level < 0) {
  2167. level = -level;
  2168. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2169. level = -level;
  2170. } else {
  2171. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2172. }
  2173. block[j] = level;
  2174. sum+=level;
  2175. }
  2176. }
  2177. block[63]^=sum&1;
  2178. }
  2179. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2180. int16_t *block, int n, int qscale)
  2181. {
  2182. int i, level, nCoeffs;
  2183. const uint16_t *quant_matrix;
  2184. int sum=-1;
  2185. if(s->alternate_scan) nCoeffs= 63;
  2186. else nCoeffs= s->block_last_index[n];
  2187. quant_matrix = s->inter_matrix;
  2188. for(i=0; i<=nCoeffs; i++) {
  2189. int j= s->intra_scantable.permutated[i];
  2190. level = block[j];
  2191. if (level) {
  2192. if (level < 0) {
  2193. level = -level;
  2194. level = (((level << 1) + 1) * qscale *
  2195. ((int) (quant_matrix[j]))) >> 4;
  2196. level = -level;
  2197. } else {
  2198. level = (((level << 1) + 1) * qscale *
  2199. ((int) (quant_matrix[j]))) >> 4;
  2200. }
  2201. block[j] = level;
  2202. sum+=level;
  2203. }
  2204. }
  2205. block[63]^=sum&1;
  2206. }
  2207. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2208. int16_t *block, int n, int qscale)
  2209. {
  2210. int i, level, qmul, qadd;
  2211. int nCoeffs;
  2212. assert(s->block_last_index[n]>=0);
  2213. qmul = qscale << 1;
  2214. if (!s->h263_aic) {
  2215. if (n < 4)
  2216. block[0] = block[0] * s->y_dc_scale;
  2217. else
  2218. block[0] = block[0] * s->c_dc_scale;
  2219. qadd = (qscale - 1) | 1;
  2220. }else{
  2221. qadd = 0;
  2222. }
  2223. if(s->ac_pred)
  2224. nCoeffs=63;
  2225. else
  2226. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2227. for(i=1; i<=nCoeffs; i++) {
  2228. level = block[i];
  2229. if (level) {
  2230. if (level < 0) {
  2231. level = level * qmul - qadd;
  2232. } else {
  2233. level = level * qmul + qadd;
  2234. }
  2235. block[i] = level;
  2236. }
  2237. }
  2238. }
  2239. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2240. int16_t *block, int n, int qscale)
  2241. {
  2242. int i, level, qmul, qadd;
  2243. int nCoeffs;
  2244. assert(s->block_last_index[n]>=0);
  2245. qadd = (qscale - 1) | 1;
  2246. qmul = qscale << 1;
  2247. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2248. for(i=0; i<=nCoeffs; i++) {
  2249. level = block[i];
  2250. if (level) {
  2251. if (level < 0) {
  2252. level = level * qmul - qadd;
  2253. } else {
  2254. level = level * qmul + qadd;
  2255. }
  2256. block[i] = level;
  2257. }
  2258. }
  2259. }
  2260. /**
  2261. * set qscale and update qscale dependent variables.
  2262. */
  2263. void ff_set_qscale(MpegEncContext * s, int qscale)
  2264. {
  2265. if (qscale < 1)
  2266. qscale = 1;
  2267. else if (qscale > 31)
  2268. qscale = 31;
  2269. s->qscale = qscale;
  2270. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2271. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2272. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2273. }
  2274. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2275. {
  2276. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2277. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2278. }
  2279. #if CONFIG_ERROR_RESILIENCE
  2280. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2281. {
  2282. ERContext *er = &s->er;
  2283. er->cur_pic = s->current_picture_ptr;
  2284. er->last_pic = s->last_picture_ptr;
  2285. er->next_pic = s->next_picture_ptr;
  2286. er->pp_time = s->pp_time;
  2287. er->pb_time = s->pb_time;
  2288. er->quarter_sample = s->quarter_sample;
  2289. er->partitioned_frame = s->partitioned_frame;
  2290. ff_er_frame_start(er);
  2291. }
  2292. #endif /* CONFIG_ERROR_RESILIENCE */