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.

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