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.

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