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.

2627 lines
90KB

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