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.

2626 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 * 2,
  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. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  379. }
  380. static int update_picture_tables(Picture *dst, Picture *src)
  381. {
  382. int i;
  383. #define UPDATE_TABLE(table)\
  384. do {\
  385. if (src->table &&\
  386. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  387. av_buffer_unref(&dst->table);\
  388. dst->table = av_buffer_ref(src->table);\
  389. if (!dst->table) {\
  390. free_picture_tables(dst);\
  391. return AVERROR(ENOMEM);\
  392. }\
  393. }\
  394. } while (0)
  395. UPDATE_TABLE(mb_var_buf);
  396. UPDATE_TABLE(mc_mb_var_buf);
  397. UPDATE_TABLE(mb_mean_buf);
  398. UPDATE_TABLE(mbskip_table_buf);
  399. UPDATE_TABLE(qscale_table_buf);
  400. UPDATE_TABLE(mb_type_buf);
  401. for (i = 0; i < 2; i++) {
  402. UPDATE_TABLE(motion_val_buf[i]);
  403. UPDATE_TABLE(ref_index_buf[i]);
  404. }
  405. dst->mb_var = src->mb_var;
  406. dst->mc_mb_var = src->mc_mb_var;
  407. dst->mb_mean = src->mb_mean;
  408. dst->mbskip_table = src->mbskip_table;
  409. dst->qscale_table = src->qscale_table;
  410. dst->mb_type = src->mb_type;
  411. for (i = 0; i < 2; i++) {
  412. dst->motion_val[i] = src->motion_val[i];
  413. dst->ref_index[i] = src->ref_index[i];
  414. }
  415. return 0;
  416. }
  417. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  418. {
  419. int ret;
  420. av_assert0(!dst->f.buf[0]);
  421. av_assert0(src->f.buf[0]);
  422. src->tf.f = &src->f;
  423. dst->tf.f = &dst->f;
  424. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  425. if (ret < 0)
  426. goto fail;
  427. ret = update_picture_tables(dst, src);
  428. if (ret < 0)
  429. goto fail;
  430. if (src->hwaccel_picture_private) {
  431. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  432. if (!dst->hwaccel_priv_buf)
  433. goto fail;
  434. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  435. }
  436. dst->field_picture = src->field_picture;
  437. dst->mb_var_sum = src->mb_var_sum;
  438. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  439. dst->b_frame_score = src->b_frame_score;
  440. dst->needs_realloc = src->needs_realloc;
  441. dst->reference = src->reference;
  442. dst->shared = src->shared;
  443. return 0;
  444. fail:
  445. ff_mpeg_unref_picture(s, dst);
  446. return ret;
  447. }
  448. static int init_duplicate_context(MpegEncContext *s)
  449. {
  450. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  451. int c_size = s->mb_stride * (s->mb_height + 1);
  452. int yc_size = y_size + 2 * c_size;
  453. int i;
  454. s->edge_emu_buffer =
  455. s->me.scratchpad =
  456. s->me.temp =
  457. s->rd_scratchpad =
  458. s->b_scratchpad =
  459. s->obmc_scratchpad = NULL;
  460. if (s->encoding) {
  461. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  462. ME_MAP_SIZE * sizeof(uint32_t), fail)
  463. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  464. ME_MAP_SIZE * sizeof(uint32_t), fail)
  465. if (s->avctx->noise_reduction) {
  466. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  467. 2 * 64 * sizeof(int), fail)
  468. }
  469. }
  470. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  471. s->block = s->blocks[0];
  472. for (i = 0; i < 12; i++) {
  473. s->pblocks[i] = &s->block[i];
  474. }
  475. if (s->out_format == FMT_H263) {
  476. /* ac values */
  477. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  478. yc_size * sizeof(int16_t) * 16, fail);
  479. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  480. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  481. s->ac_val[2] = s->ac_val[1] + c_size;
  482. }
  483. return 0;
  484. fail:
  485. return -1; // free() through ff_MPV_common_end()
  486. }
  487. static void free_duplicate_context(MpegEncContext *s)
  488. {
  489. if (s == NULL)
  490. return;
  491. av_freep(&s->edge_emu_buffer);
  492. av_freep(&s->me.scratchpad);
  493. s->me.temp =
  494. s->rd_scratchpad =
  495. s->b_scratchpad =
  496. s->obmc_scratchpad = NULL;
  497. av_freep(&s->dct_error_sum);
  498. av_freep(&s->me.map);
  499. av_freep(&s->me.score_map);
  500. av_freep(&s->blocks);
  501. av_freep(&s->ac_val_base);
  502. s->block = NULL;
  503. }
  504. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  505. {
  506. #define COPY(a) bak->a = src->a
  507. COPY(edge_emu_buffer);
  508. COPY(me.scratchpad);
  509. COPY(me.temp);
  510. COPY(rd_scratchpad);
  511. COPY(b_scratchpad);
  512. COPY(obmc_scratchpad);
  513. COPY(me.map);
  514. COPY(me.score_map);
  515. COPY(blocks);
  516. COPY(block);
  517. COPY(start_mb_y);
  518. COPY(end_mb_y);
  519. COPY(me.map_generation);
  520. COPY(pb);
  521. COPY(dct_error_sum);
  522. COPY(dct_count[0]);
  523. COPY(dct_count[1]);
  524. COPY(ac_val_base);
  525. COPY(ac_val[0]);
  526. COPY(ac_val[1]);
  527. COPY(ac_val[2]);
  528. #undef COPY
  529. }
  530. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  531. {
  532. MpegEncContext bak;
  533. int i, ret;
  534. // FIXME copy only needed parts
  535. // START_TIMER
  536. backup_duplicate_context(&bak, dst);
  537. memcpy(dst, src, sizeof(MpegEncContext));
  538. backup_duplicate_context(dst, &bak);
  539. for (i = 0; i < 12; i++) {
  540. dst->pblocks[i] = &dst->block[i];
  541. }
  542. if (!dst->edge_emu_buffer &&
  543. (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
  544. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  545. "scratch buffers.\n");
  546. return ret;
  547. }
  548. // STOP_TIMER("update_duplicate_context")
  549. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  550. return 0;
  551. }
  552. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  553. const AVCodecContext *src)
  554. {
  555. int i, ret;
  556. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  557. if (dst == src || !s1->context_initialized)
  558. return 0;
  559. // FIXME can parameters change on I-frames?
  560. // in that case dst may need a reinit
  561. if (!s->context_initialized) {
  562. memcpy(s, s1, sizeof(MpegEncContext));
  563. s->avctx = dst;
  564. s->bitstream_buffer = NULL;
  565. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  566. ff_MPV_common_init(s);
  567. }
  568. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  569. int err;
  570. s->context_reinit = 0;
  571. s->height = s1->height;
  572. s->width = s1->width;
  573. if ((err = ff_MPV_common_frame_size_change(s)) < 0)
  574. return err;
  575. }
  576. s->avctx->coded_height = s1->avctx->coded_height;
  577. s->avctx->coded_width = s1->avctx->coded_width;
  578. s->avctx->width = s1->avctx->width;
  579. s->avctx->height = s1->avctx->height;
  580. s->coded_picture_number = s1->coded_picture_number;
  581. s->picture_number = s1->picture_number;
  582. s->input_picture_number = s1->input_picture_number;
  583. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  584. ff_mpeg_unref_picture(s, &s->picture[i]);
  585. if (s1->picture[i].f.data[0] &&
  586. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  587. return ret;
  588. }
  589. #define UPDATE_PICTURE(pic)\
  590. do {\
  591. ff_mpeg_unref_picture(s, &s->pic);\
  592. if (s1->pic.f.data[0])\
  593. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  594. else\
  595. ret = update_picture_tables(&s->pic, &s1->pic);\
  596. if (ret < 0)\
  597. return ret;\
  598. } while (0)
  599. UPDATE_PICTURE(current_picture);
  600. UPDATE_PICTURE(last_picture);
  601. UPDATE_PICTURE(next_picture);
  602. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  603. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  604. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  605. // Error/bug resilience
  606. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  607. s->workaround_bugs = s1->workaround_bugs;
  608. // MPEG4 timing info
  609. memcpy(&s->time_increment_bits, &s1->time_increment_bits,
  610. (char *) &s1->shape - (char *) &s1->time_increment_bits);
  611. // B-frame info
  612. s->max_b_frames = s1->max_b_frames;
  613. s->low_delay = s1->low_delay;
  614. s->droppable = s1->droppable;
  615. // DivX handling (doesn't work)
  616. s->divx_packed = s1->divx_packed;
  617. if (s1->bitstream_buffer) {
  618. if (s1->bitstream_buffer_size +
  619. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  620. av_fast_malloc(&s->bitstream_buffer,
  621. &s->allocated_bitstream_buffer_size,
  622. s1->allocated_bitstream_buffer_size);
  623. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  624. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  625. s1->bitstream_buffer_size);
  626. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  627. FF_INPUT_BUFFER_PADDING_SIZE);
  628. }
  629. // linesize dependend scratch buffer allocation
  630. if (!s->edge_emu_buffer)
  631. if (s1->linesize) {
  632. if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
  633. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  634. "scratch buffers.\n");
  635. return AVERROR(ENOMEM);
  636. }
  637. } else {
  638. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  639. "be allocated due to unknown size.\n");
  640. return AVERROR_BUG;
  641. }
  642. // MPEG2/interlacing info
  643. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  644. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  645. if (!s1->first_field) {
  646. s->last_pict_type = s1->pict_type;
  647. if (s1->current_picture_ptr)
  648. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  649. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  650. s->last_non_b_pict_type = s1->pict_type;
  651. }
  652. }
  653. return 0;
  654. }
  655. /**
  656. * Set the given MpegEncContext to common defaults
  657. * (same for encoding and decoding).
  658. * The changed fields will not depend upon the
  659. * prior state of the MpegEncContext.
  660. */
  661. void ff_MPV_common_defaults(MpegEncContext *s)
  662. {
  663. s->y_dc_scale_table =
  664. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  665. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  666. s->progressive_frame = 1;
  667. s->progressive_sequence = 1;
  668. s->picture_structure = PICT_FRAME;
  669. s->coded_picture_number = 0;
  670. s->picture_number = 0;
  671. s->input_picture_number = 0;
  672. s->picture_in_gop_number = 0;
  673. s->f_code = 1;
  674. s->b_code = 1;
  675. s->slice_context_count = 1;
  676. }
  677. /**
  678. * Set the given MpegEncContext to defaults for decoding.
  679. * the changed fields will not depend upon
  680. * the prior state of the MpegEncContext.
  681. */
  682. void ff_MPV_decode_defaults(MpegEncContext *s)
  683. {
  684. ff_MPV_common_defaults(s);
  685. }
  686. static int init_er(MpegEncContext *s)
  687. {
  688. ERContext *er = &s->er;
  689. int mb_array_size = s->mb_height * s->mb_stride;
  690. int i;
  691. er->avctx = s->avctx;
  692. er->dsp = &s->dsp;
  693. er->mb_index2xy = s->mb_index2xy;
  694. er->mb_num = s->mb_num;
  695. er->mb_width = s->mb_width;
  696. er->mb_height = s->mb_height;
  697. er->mb_stride = s->mb_stride;
  698. er->b8_stride = s->b8_stride;
  699. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  700. er->error_status_table = av_mallocz(mb_array_size);
  701. if (!er->er_temp_buffer || !er->error_status_table)
  702. goto fail;
  703. er->mbskip_table = s->mbskip_table;
  704. er->mbintra_table = s->mbintra_table;
  705. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  706. er->dc_val[i] = s->dc_val[i];
  707. er->decode_mb = mpeg_er_decode_mb;
  708. er->opaque = s;
  709. return 0;
  710. fail:
  711. av_freep(&er->er_temp_buffer);
  712. av_freep(&er->error_status_table);
  713. return AVERROR(ENOMEM);
  714. }
  715. /**
  716. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  717. */
  718. static int init_context_frame(MpegEncContext *s)
  719. {
  720. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  721. s->mb_width = (s->width + 15) / 16;
  722. s->mb_stride = s->mb_width + 1;
  723. s->b8_stride = s->mb_width * 2 + 1;
  724. s->b4_stride = s->mb_width * 4 + 1;
  725. mb_array_size = s->mb_height * s->mb_stride;
  726. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  727. /* set default edge pos, will be overriden
  728. * in decode_header if needed */
  729. s->h_edge_pos = s->mb_width * 16;
  730. s->v_edge_pos = s->mb_height * 16;
  731. s->mb_num = s->mb_width * s->mb_height;
  732. s->block_wrap[0] =
  733. s->block_wrap[1] =
  734. s->block_wrap[2] =
  735. s->block_wrap[3] = s->b8_stride;
  736. s->block_wrap[4] =
  737. s->block_wrap[5] = s->mb_stride;
  738. y_size = s->b8_stride * (2 * s->mb_height + 1);
  739. c_size = s->mb_stride * (s->mb_height + 1);
  740. yc_size = y_size + 2 * c_size;
  741. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
  742. fail); // error ressilience code looks cleaner with this
  743. for (y = 0; y < s->mb_height; y++)
  744. for (x = 0; x < s->mb_width; x++)
  745. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  746. s->mb_index2xy[s->mb_height * s->mb_width] =
  747. (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  748. if (s->encoding) {
  749. /* Allocate MV tables */
  750. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
  751. mv_table_size * 2 * sizeof(int16_t), fail);
  752. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
  753. mv_table_size * 2 * sizeof(int16_t), fail);
  754. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
  755. mv_table_size * 2 * sizeof(int16_t), fail);
  756. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
  757. mv_table_size * 2 * sizeof(int16_t), fail);
  758. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
  759. mv_table_size * 2 * sizeof(int16_t), fail);
  760. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
  761. mv_table_size * 2 * sizeof(int16_t), fail);
  762. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  763. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  764. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  765. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
  766. s->mb_stride + 1;
  767. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
  768. s->mb_stride + 1;
  769. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  770. /* Allocate MB type table */
  771. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
  772. sizeof(uint16_t), fail); // needed for encoding
  773. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
  774. sizeof(int), fail);
  775. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  776. mb_array_size * sizeof(float), fail);
  777. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  778. mb_array_size * sizeof(float), fail);
  779. }
  780. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  781. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  782. /* interlaced direct mode decoding tables */
  783. for (i = 0; i < 2; i++) {
  784. int j, k;
  785. for (j = 0; j < 2; j++) {
  786. for (k = 0; k < 2; k++) {
  787. FF_ALLOCZ_OR_GOTO(s->avctx,
  788. s->b_field_mv_table_base[i][j][k],
  789. mv_table_size * 2 * sizeof(int16_t),
  790. fail);
  791. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  792. s->mb_stride + 1;
  793. }
  794. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
  795. mb_array_size * 2 * sizeof(uint8_t), fail);
  796. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
  797. mv_table_size * 2 * sizeof(int16_t), fail);
  798. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
  799. + s->mb_stride + 1;
  800. }
  801. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
  802. mb_array_size * 2 * sizeof(uint8_t), fail);
  803. }
  804. }
  805. if (s->out_format == FMT_H263) {
  806. /* cbp values */
  807. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  808. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  809. /* cbp, ac_pred, pred_dir */
  810. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
  811. mb_array_size * sizeof(uint8_t), fail);
  812. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
  813. mb_array_size * sizeof(uint8_t), fail);
  814. }
  815. if (s->h263_pred || s->h263_plus || !s->encoding) {
  816. /* dc values */
  817. // MN: we need these for error resilience of intra-frames
  818. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
  819. yc_size * sizeof(int16_t), fail);
  820. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  821. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  822. s->dc_val[2] = s->dc_val[1] + c_size;
  823. for (i = 0; i < yc_size; i++)
  824. s->dc_val_base[i] = 1024;
  825. }
  826. /* which mb is a intra block */
  827. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  828. memset(s->mbintra_table, 1, mb_array_size);
  829. /* init macroblock skip table */
  830. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  831. // Note the + 1 is for a quicker mpeg4 slice_end detection
  832. return init_er(s);
  833. fail:
  834. return AVERROR(ENOMEM);
  835. }
  836. /**
  837. * init common structure for both encoder and decoder.
  838. * this assumes that some variables like width/height are already set
  839. */
  840. av_cold int ff_MPV_common_init(MpegEncContext *s)
  841. {
  842. int i;
  843. int nb_slices = (HAVE_THREADS &&
  844. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  845. s->avctx->thread_count : 1;
  846. if (s->encoding && s->avctx->slices)
  847. nb_slices = s->avctx->slices;
  848. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  849. s->mb_height = (s->height + 31) / 32 * 2;
  850. else
  851. s->mb_height = (s->height + 15) / 16;
  852. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  853. av_log(s->avctx, AV_LOG_ERROR,
  854. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  855. return -1;
  856. }
  857. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  858. int max_slices;
  859. if (s->mb_height)
  860. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  861. else
  862. max_slices = MAX_THREADS;
  863. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  864. " reducing to %d\n", nb_slices, max_slices);
  865. nb_slices = max_slices;
  866. }
  867. if ((s->width || s->height) &&
  868. av_image_check_size(s->width, s->height, 0, s->avctx))
  869. return -1;
  870. ff_dct_common_init(s);
  871. s->flags = s->avctx->flags;
  872. s->flags2 = s->avctx->flags2;
  873. if (s->width && s->height) {
  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. s->avctx->coded_frame = &s->current_picture.f;
  882. if (s->encoding) {
  883. if (s->msmpeg4_version) {
  884. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
  885. 2 * 2 * (MAX_LEVEL + 1) *
  886. (MAX_RUN + 1) * 2 * sizeof(int), fail);
  887. }
  888. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  889. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
  890. 64 * 32 * sizeof(int), fail);
  891. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
  892. 64 * 32 * sizeof(int), fail);
  893. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
  894. 64 * 32 * 2 * sizeof(uint16_t), fail);
  895. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
  896. 64 * 32 * 2 * sizeof(uint16_t), fail);
  897. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
  898. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  899. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
  900. MAX_PICTURE_COUNT * sizeof(Picture *), fail);
  901. if (s->avctx->noise_reduction) {
  902. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
  903. 2 * 64 * sizeof(uint16_t), fail);
  904. }
  905. }
  906. }
  907. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  908. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  909. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  910. avcodec_get_frame_defaults(&s->picture[i].f);
  911. }
  912. memset(&s->next_picture, 0, sizeof(s->next_picture));
  913. memset(&s->last_picture, 0, sizeof(s->last_picture));
  914. memset(&s->current_picture, 0, sizeof(s->current_picture));
  915. avcodec_get_frame_defaults(&s->next_picture.f);
  916. avcodec_get_frame_defaults(&s->last_picture.f);
  917. avcodec_get_frame_defaults(&s->current_picture.f);
  918. if (s->width && s->height) {
  919. if (init_context_frame(s))
  920. goto fail;
  921. s->parse_context.state = -1;
  922. }
  923. s->context_initialized = 1;
  924. s->thread_context[0] = s;
  925. if (s->width && s->height) {
  926. if (nb_slices > 1) {
  927. for (i = 1; i < nb_slices; i++) {
  928. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  929. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  930. }
  931. for (i = 0; i < nb_slices; i++) {
  932. if (init_duplicate_context(s->thread_context[i]) < 0)
  933. goto fail;
  934. s->thread_context[i]->start_mb_y =
  935. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  936. s->thread_context[i]->end_mb_y =
  937. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  938. }
  939. } else {
  940. if (init_duplicate_context(s) < 0)
  941. goto fail;
  942. s->start_mb_y = 0;
  943. s->end_mb_y = s->mb_height;
  944. }
  945. s->slice_context_count = nb_slices;
  946. }
  947. return 0;
  948. fail:
  949. ff_MPV_common_end(s);
  950. return -1;
  951. }
  952. /**
  953. * Frees and resets MpegEncContext fields depending on the resolution.
  954. * Is used during resolution changes to avoid a full reinitialization of the
  955. * codec.
  956. */
  957. static int free_context_frame(MpegEncContext *s)
  958. {
  959. int i, j, k;
  960. av_freep(&s->mb_type);
  961. av_freep(&s->p_mv_table_base);
  962. av_freep(&s->b_forw_mv_table_base);
  963. av_freep(&s->b_back_mv_table_base);
  964. av_freep(&s->b_bidir_forw_mv_table_base);
  965. av_freep(&s->b_bidir_back_mv_table_base);
  966. av_freep(&s->b_direct_mv_table_base);
  967. s->p_mv_table = NULL;
  968. s->b_forw_mv_table = NULL;
  969. s->b_back_mv_table = NULL;
  970. s->b_bidir_forw_mv_table = NULL;
  971. s->b_bidir_back_mv_table = NULL;
  972. s->b_direct_mv_table = NULL;
  973. for (i = 0; i < 2; i++) {
  974. for (j = 0; j < 2; j++) {
  975. for (k = 0; k < 2; k++) {
  976. av_freep(&s->b_field_mv_table_base[i][j][k]);
  977. s->b_field_mv_table[i][j][k] = NULL;
  978. }
  979. av_freep(&s->b_field_select_table[i][j]);
  980. av_freep(&s->p_field_mv_table_base[i][j]);
  981. s->p_field_mv_table[i][j] = NULL;
  982. }
  983. av_freep(&s->p_field_select_table[i]);
  984. }
  985. av_freep(&s->dc_val_base);
  986. av_freep(&s->coded_block_base);
  987. av_freep(&s->mbintra_table);
  988. av_freep(&s->cbp_table);
  989. av_freep(&s->pred_dir_table);
  990. av_freep(&s->mbskip_table);
  991. av_freep(&s->er.error_status_table);
  992. av_freep(&s->er.er_temp_buffer);
  993. av_freep(&s->mb_index2xy);
  994. av_freep(&s->lambda_table);
  995. av_freep(&s->cplx_tab);
  996. av_freep(&s->bits_tab);
  997. s->linesize = s->uvlinesize = 0;
  998. return 0;
  999. }
  1000. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1001. {
  1002. int i, err = 0;
  1003. if (s->slice_context_count > 1) {
  1004. for (i = 0; i < s->slice_context_count; i++) {
  1005. free_duplicate_context(s->thread_context[i]);
  1006. }
  1007. for (i = 1; i < s->slice_context_count; i++) {
  1008. av_freep(&s->thread_context[i]);
  1009. }
  1010. } else
  1011. free_duplicate_context(s);
  1012. if ((err = free_context_frame(s)) < 0)
  1013. return err;
  1014. if (s->picture)
  1015. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1016. s->picture[i].needs_realloc = 1;
  1017. }
  1018. s->last_picture_ptr =
  1019. s->next_picture_ptr =
  1020. s->current_picture_ptr = NULL;
  1021. // init
  1022. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1023. s->mb_height = (s->height + 31) / 32 * 2;
  1024. else
  1025. s->mb_height = (s->height + 15) / 16;
  1026. if ((s->width || s->height) &&
  1027. av_image_check_size(s->width, s->height, 0, s->avctx))
  1028. return AVERROR_INVALIDDATA;
  1029. if ((err = init_context_frame(s)))
  1030. goto fail;
  1031. s->thread_context[0] = s;
  1032. if (s->width && s->height) {
  1033. int nb_slices = s->slice_context_count;
  1034. if (nb_slices > 1) {
  1035. for (i = 1; i < nb_slices; i++) {
  1036. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1037. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1038. }
  1039. for (i = 0; i < nb_slices; i++) {
  1040. if (init_duplicate_context(s->thread_context[i]) < 0)
  1041. goto fail;
  1042. s->thread_context[i]->start_mb_y =
  1043. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1044. s->thread_context[i]->end_mb_y =
  1045. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1046. }
  1047. } else {
  1048. if (init_duplicate_context(s) < 0)
  1049. goto fail;
  1050. s->start_mb_y = 0;
  1051. s->end_mb_y = s->mb_height;
  1052. }
  1053. s->slice_context_count = nb_slices;
  1054. }
  1055. return 0;
  1056. fail:
  1057. ff_MPV_common_end(s);
  1058. return err;
  1059. }
  1060. /* init common structure for both encoder and decoder */
  1061. void ff_MPV_common_end(MpegEncContext *s)
  1062. {
  1063. int i;
  1064. if (s->slice_context_count > 1) {
  1065. for (i = 0; i < s->slice_context_count; i++) {
  1066. free_duplicate_context(s->thread_context[i]);
  1067. }
  1068. for (i = 1; i < s->slice_context_count; i++) {
  1069. av_freep(&s->thread_context[i]);
  1070. }
  1071. s->slice_context_count = 1;
  1072. } else free_duplicate_context(s);
  1073. av_freep(&s->parse_context.buffer);
  1074. s->parse_context.buffer_size = 0;
  1075. av_freep(&s->bitstream_buffer);
  1076. s->allocated_bitstream_buffer_size = 0;
  1077. av_freep(&s->avctx->stats_out);
  1078. av_freep(&s->ac_stats);
  1079. av_freep(&s->q_intra_matrix);
  1080. av_freep(&s->q_inter_matrix);
  1081. av_freep(&s->q_intra_matrix16);
  1082. av_freep(&s->q_inter_matrix16);
  1083. av_freep(&s->input_picture);
  1084. av_freep(&s->reordered_input_picture);
  1085. av_freep(&s->dct_offset);
  1086. if (s->picture) {
  1087. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1088. free_picture_tables(&s->picture[i]);
  1089. ff_mpeg_unref_picture(s, &s->picture[i]);
  1090. }
  1091. }
  1092. av_freep(&s->picture);
  1093. free_picture_tables(&s->last_picture);
  1094. ff_mpeg_unref_picture(s, &s->last_picture);
  1095. free_picture_tables(&s->current_picture);
  1096. ff_mpeg_unref_picture(s, &s->current_picture);
  1097. free_picture_tables(&s->next_picture);
  1098. ff_mpeg_unref_picture(s, &s->next_picture);
  1099. free_picture_tables(&s->new_picture);
  1100. ff_mpeg_unref_picture(s, &s->new_picture);
  1101. free_context_frame(s);
  1102. s->context_initialized = 0;
  1103. s->last_picture_ptr =
  1104. s->next_picture_ptr =
  1105. s->current_picture_ptr = NULL;
  1106. s->linesize = s->uvlinesize = 0;
  1107. }
  1108. av_cold void ff_init_rl(RLTable *rl,
  1109. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1110. {
  1111. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1112. uint8_t index_run[MAX_RUN + 1];
  1113. int last, run, level, start, end, i;
  1114. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1115. if (static_store && rl->max_level[0])
  1116. return;
  1117. /* compute max_level[], max_run[] and index_run[] */
  1118. for (last = 0; last < 2; last++) {
  1119. if (last == 0) {
  1120. start = 0;
  1121. end = rl->last;
  1122. } else {
  1123. start = rl->last;
  1124. end = rl->n;
  1125. }
  1126. memset(max_level, 0, MAX_RUN + 1);
  1127. memset(max_run, 0, MAX_LEVEL + 1);
  1128. memset(index_run, rl->n, MAX_RUN + 1);
  1129. for (i = start; i < end; i++) {
  1130. run = rl->table_run[i];
  1131. level = rl->table_level[i];
  1132. if (index_run[run] == rl->n)
  1133. index_run[run] = i;
  1134. if (level > max_level[run])
  1135. max_level[run] = level;
  1136. if (run > max_run[level])
  1137. max_run[level] = run;
  1138. }
  1139. if (static_store)
  1140. rl->max_level[last] = static_store[last];
  1141. else
  1142. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1143. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1144. if (static_store)
  1145. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1146. else
  1147. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1148. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1149. if (static_store)
  1150. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1151. else
  1152. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1153. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1154. }
  1155. }
  1156. av_cold void ff_init_vlc_rl(RLTable *rl)
  1157. {
  1158. int i, q;
  1159. for (q = 0; q < 32; q++) {
  1160. int qmul = q * 2;
  1161. int qadd = (q - 1) | 1;
  1162. if (q == 0) {
  1163. qmul = 1;
  1164. qadd = 0;
  1165. }
  1166. for (i = 0; i < rl->vlc.table_size; i++) {
  1167. int code = rl->vlc.table[i][0];
  1168. int len = rl->vlc.table[i][1];
  1169. int level, run;
  1170. if (len == 0) { // illegal code
  1171. run = 66;
  1172. level = MAX_LEVEL;
  1173. } else if (len < 0) { // more bits needed
  1174. run = 0;
  1175. level = code;
  1176. } else {
  1177. if (code == rl->n) { // esc
  1178. run = 66;
  1179. level = 0;
  1180. } else {
  1181. run = rl->table_run[code] + 1;
  1182. level = rl->table_level[code] * qmul + qadd;
  1183. if (code >= rl->last) run += 192;
  1184. }
  1185. }
  1186. rl->rl_vlc[q][i].len = len;
  1187. rl->rl_vlc[q][i].level = level;
  1188. rl->rl_vlc[q][i].run = run;
  1189. }
  1190. }
  1191. }
  1192. void ff_release_unused_pictures(MpegEncContext*s, int remove_current)
  1193. {
  1194. int i;
  1195. /* release non reference frames */
  1196. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1197. if (!s->picture[i].reference &&
  1198. (remove_current || &s->picture[i] != s->current_picture_ptr)) {
  1199. ff_mpeg_unref_picture(s, &s->picture[i]);
  1200. }
  1201. }
  1202. }
  1203. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1204. {
  1205. if (pic->f.data[0] == NULL)
  1206. return 1;
  1207. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1208. return 1;
  1209. return 0;
  1210. }
  1211. static int find_unused_picture(MpegEncContext *s, int shared)
  1212. {
  1213. int i;
  1214. if (shared) {
  1215. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1216. if (s->picture[i].f.data[0] == NULL)
  1217. return i;
  1218. }
  1219. } else {
  1220. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1221. if (pic_is_unused(s, &s->picture[i]))
  1222. return i;
  1223. }
  1224. }
  1225. return AVERROR_INVALIDDATA;
  1226. }
  1227. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1228. {
  1229. int ret = find_unused_picture(s, shared);
  1230. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1231. if (s->picture[ret].needs_realloc) {
  1232. s->picture[ret].needs_realloc = 0;
  1233. free_picture_tables(&s->picture[ret]);
  1234. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1235. avcodec_get_frame_defaults(&s->picture[ret].f);
  1236. }
  1237. }
  1238. return ret;
  1239. }
  1240. static void update_noise_reduction(MpegEncContext *s)
  1241. {
  1242. int intra, i;
  1243. for (intra = 0; intra < 2; intra++) {
  1244. if (s->dct_count[intra] > (1 << 16)) {
  1245. for (i = 0; i < 64; i++) {
  1246. s->dct_error_sum[intra][i] >>= 1;
  1247. }
  1248. s->dct_count[intra] >>= 1;
  1249. }
  1250. for (i = 0; i < 64; i++) {
  1251. s->dct_offset[intra][i] = (s->avctx->noise_reduction *
  1252. s->dct_count[intra] +
  1253. s->dct_error_sum[intra][i] / 2) /
  1254. (s->dct_error_sum[intra][i] + 1);
  1255. }
  1256. }
  1257. }
  1258. /**
  1259. * generic function for encode/decode called after coding/decoding
  1260. * the header and before a frame is coded/decoded.
  1261. */
  1262. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1263. {
  1264. int i, ret;
  1265. Picture *pic;
  1266. s->mb_skipped = 0;
  1267. /* mark & release old frames */
  1268. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1269. s->last_picture_ptr != s->next_picture_ptr &&
  1270. s->last_picture_ptr->f.data[0]) {
  1271. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1272. }
  1273. /* release forgotten pictures */
  1274. /* if (mpeg124/h263) */
  1275. if (!s->encoding) {
  1276. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1277. if (&s->picture[i] != s->last_picture_ptr &&
  1278. &s->picture[i] != s->next_picture_ptr &&
  1279. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1280. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1281. av_log(avctx, AV_LOG_ERROR,
  1282. "releasing zombie picture\n");
  1283. ff_mpeg_unref_picture(s, &s->picture[i]);
  1284. }
  1285. }
  1286. }
  1287. if (!s->encoding) {
  1288. ff_release_unused_pictures(s, 1);
  1289. if (s->current_picture_ptr &&
  1290. s->current_picture_ptr->f.data[0] == NULL) {
  1291. // we already have a unused image
  1292. // (maybe it was set before reading the header)
  1293. pic = s->current_picture_ptr;
  1294. } else {
  1295. i = ff_find_unused_picture(s, 0);
  1296. if (i < 0) {
  1297. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1298. return i;
  1299. }
  1300. pic = &s->picture[i];
  1301. }
  1302. pic->reference = 0;
  1303. if (!s->droppable) {
  1304. if (s->pict_type != AV_PICTURE_TYPE_B)
  1305. pic->reference = 3;
  1306. }
  1307. pic->f.coded_picture_number = s->coded_picture_number++;
  1308. if (ff_alloc_picture(s, pic, 0) < 0)
  1309. return -1;
  1310. s->current_picture_ptr = pic;
  1311. // FIXME use only the vars from current_pic
  1312. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1313. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1314. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1315. if (s->picture_structure != PICT_FRAME)
  1316. s->current_picture_ptr->f.top_field_first =
  1317. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1318. }
  1319. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1320. !s->progressive_sequence;
  1321. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1322. }
  1323. s->current_picture_ptr->f.pict_type = s->pict_type;
  1324. // if (s->flags && CODEC_FLAG_QSCALE)
  1325. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1326. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1327. ff_mpeg_unref_picture(s, &s->current_picture);
  1328. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1329. s->current_picture_ptr)) < 0)
  1330. return ret;
  1331. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1332. s->last_picture_ptr = s->next_picture_ptr;
  1333. if (!s->droppable)
  1334. s->next_picture_ptr = s->current_picture_ptr;
  1335. }
  1336. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1337. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1338. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1339. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1340. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1341. s->pict_type, s->droppable);
  1342. if ((s->last_picture_ptr == NULL ||
  1343. s->last_picture_ptr->f.data[0] == NULL) &&
  1344. (s->pict_type != AV_PICTURE_TYPE_I ||
  1345. s->picture_structure != PICT_FRAME)) {
  1346. int h_chroma_shift, v_chroma_shift;
  1347. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1348. &h_chroma_shift, &v_chroma_shift);
  1349. if (s->pict_type != AV_PICTURE_TYPE_I)
  1350. av_log(avctx, AV_LOG_ERROR,
  1351. "warning: first frame is no keyframe\n");
  1352. else if (s->picture_structure != PICT_FRAME)
  1353. av_log(avctx, AV_LOG_INFO,
  1354. "allocate dummy last picture for field based first keyframe\n");
  1355. /* Allocate a dummy frame */
  1356. i = ff_find_unused_picture(s, 0);
  1357. if (i < 0) {
  1358. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1359. return i;
  1360. }
  1361. s->last_picture_ptr = &s->picture[i];
  1362. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1363. s->last_picture_ptr = NULL;
  1364. return -1;
  1365. }
  1366. memset(s->last_picture_ptr->f.data[0], 0,
  1367. avctx->height * s->last_picture_ptr->f.linesize[0]);
  1368. memset(s->last_picture_ptr->f.data[1], 0x80,
  1369. (avctx->height >> v_chroma_shift) *
  1370. s->last_picture_ptr->f.linesize[1]);
  1371. memset(s->last_picture_ptr->f.data[2], 0x80,
  1372. (avctx->height >> v_chroma_shift) *
  1373. s->last_picture_ptr->f.linesize[2]);
  1374. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1375. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1376. }
  1377. if ((s->next_picture_ptr == NULL ||
  1378. s->next_picture_ptr->f.data[0] == NULL) &&
  1379. s->pict_type == AV_PICTURE_TYPE_B) {
  1380. /* Allocate a dummy frame */
  1381. i = ff_find_unused_picture(s, 0);
  1382. if (i < 0) {
  1383. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1384. return i;
  1385. }
  1386. s->next_picture_ptr = &s->picture[i];
  1387. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1388. s->next_picture_ptr = NULL;
  1389. return -1;
  1390. }
  1391. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1392. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1393. }
  1394. if (s->last_picture_ptr) {
  1395. ff_mpeg_unref_picture(s, &s->last_picture);
  1396. if (s->last_picture_ptr->f.data[0] &&
  1397. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1398. s->last_picture_ptr)) < 0)
  1399. return ret;
  1400. }
  1401. if (s->next_picture_ptr) {
  1402. ff_mpeg_unref_picture(s, &s->next_picture);
  1403. if (s->next_picture_ptr->f.data[0] &&
  1404. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1405. s->next_picture_ptr)) < 0)
  1406. return ret;
  1407. }
  1408. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1409. s->last_picture_ptr->f.data[0]));
  1410. if (s->picture_structure!= PICT_FRAME) {
  1411. int i;
  1412. for (i = 0; i < 4; i++) {
  1413. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1414. s->current_picture.f.data[i] +=
  1415. s->current_picture.f.linesize[i];
  1416. }
  1417. s->current_picture.f.linesize[i] *= 2;
  1418. s->last_picture.f.linesize[i] *= 2;
  1419. s->next_picture.f.linesize[i] *= 2;
  1420. }
  1421. }
  1422. s->err_recognition = avctx->err_recognition;
  1423. /* set dequantizer, we can't do it during init as
  1424. * it might change for mpeg4 and we can't do it in the header
  1425. * decode as init is not called for mpeg4 there yet */
  1426. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1427. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1428. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1429. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1430. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1431. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1432. } else {
  1433. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1434. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1435. }
  1436. if (s->dct_error_sum) {
  1437. assert(s->avctx->noise_reduction && s->encoding);
  1438. update_noise_reduction(s);
  1439. }
  1440. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1441. return ff_xvmc_field_start(s, avctx);
  1442. return 0;
  1443. }
  1444. /* generic function for encode/decode called after a
  1445. * frame has been coded/decoded. */
  1446. void ff_MPV_frame_end(MpegEncContext *s)
  1447. {
  1448. int i;
  1449. /* redraw edges for the frame if decoding didn't complete */
  1450. // just to make sure that all data is rendered.
  1451. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
  1452. ff_xvmc_field_end(s);
  1453. } else if ((s->er.error_count || s->encoding) &&
  1454. !s->avctx->hwaccel &&
  1455. !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  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. !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
  1932. draw_edges &&
  1933. cur->reference &&
  1934. !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  1935. int *linesize = cur->f.linesize;
  1936. int sides = 0, edge_h;
  1937. if (y==0) sides |= EDGE_TOP;
  1938. if (y + h >= v_edge_pos)
  1939. sides |= EDGE_BOTTOM;
  1940. edge_h= FFMIN(h, v_edge_pos - y);
  1941. dsp->draw_edges(cur->f.data[0] + y * linesize[0],
  1942. linesize[0], h_edge_pos, edge_h,
  1943. EDGE_WIDTH, EDGE_WIDTH, sides);
  1944. dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1],
  1945. linesize[1], h_edge_pos >> hshift, edge_h >> vshift,
  1946. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1947. dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2],
  1948. linesize[2], h_edge_pos >> hshift, edge_h >> vshift,
  1949. EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides);
  1950. }
  1951. h = FFMIN(h, avctx->height - y);
  1952. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  1953. if (avctx->draw_horiz_band) {
  1954. AVFrame *src;
  1955. int offset[AV_NUM_DATA_POINTERS];
  1956. int i;
  1957. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  1958. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  1959. src = &cur->f;
  1960. else if (last)
  1961. src = &last->f;
  1962. else
  1963. return;
  1964. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  1965. picture_structure == PICT_FRAME &&
  1966. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  1967. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  1968. offset[i] = 0;
  1969. }else{
  1970. offset[0]= y * src->linesize[0];
  1971. offset[1]=
  1972. offset[2]= (y >> vshift) * src->linesize[1];
  1973. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  1974. offset[i] = 0;
  1975. }
  1976. emms_c();
  1977. avctx->draw_horiz_band(avctx, src, offset,
  1978. y, picture_structure, h);
  1979. }
  1980. }
  1981. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  1982. {
  1983. int draw_edges = s->unrestricted_mv && !s->intra_only;
  1984. ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture,
  1985. &s->last_picture, y, h, s->picture_structure,
  1986. s->first_field, draw_edges, s->low_delay,
  1987. s->v_edge_pos, s->h_edge_pos);
  1988. }
  1989. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  1990. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1991. const int uvlinesize = s->current_picture.f.linesize[1];
  1992. const int mb_size= 4;
  1993. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  1994. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  1995. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  1996. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  1997. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  1998. 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;
  1999. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2000. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2001. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2002. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2003. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2004. {
  2005. if(s->picture_structure==PICT_FRAME){
  2006. s->dest[0] += s->mb_y * linesize << mb_size;
  2007. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2008. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2009. }else{
  2010. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2011. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2012. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2013. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2014. }
  2015. }
  2016. }
  2017. /**
  2018. * Permute an 8x8 block.
  2019. * @param block the block which will be permuted according to the given permutation vector
  2020. * @param permutation the permutation vector
  2021. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2022. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2023. * (inverse) permutated to scantable order!
  2024. */
  2025. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2026. {
  2027. int i;
  2028. int16_t temp[64];
  2029. if(last<=0) return;
  2030. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2031. for(i=0; i<=last; i++){
  2032. const int j= scantable[i];
  2033. temp[j]= block[j];
  2034. block[j]=0;
  2035. }
  2036. for(i=0; i<=last; i++){
  2037. const int j= scantable[i];
  2038. const int perm_j= permutation[j];
  2039. block[perm_j]= temp[j];
  2040. }
  2041. }
  2042. void ff_mpeg_flush(AVCodecContext *avctx){
  2043. int i;
  2044. MpegEncContext *s = avctx->priv_data;
  2045. if(s==NULL || s->picture==NULL)
  2046. return;
  2047. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2048. ff_mpeg_unref_picture(s, &s->picture[i]);
  2049. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2050. ff_mpeg_unref_picture(s, &s->current_picture);
  2051. ff_mpeg_unref_picture(s, &s->last_picture);
  2052. ff_mpeg_unref_picture(s, &s->next_picture);
  2053. s->mb_x= s->mb_y= 0;
  2054. s->parse_context.state= -1;
  2055. s->parse_context.frame_start_found= 0;
  2056. s->parse_context.overread= 0;
  2057. s->parse_context.overread_index= 0;
  2058. s->parse_context.index= 0;
  2059. s->parse_context.last_index= 0;
  2060. s->bitstream_buffer_size=0;
  2061. s->pp_time=0;
  2062. }
  2063. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2064. int16_t *block, int n, int qscale)
  2065. {
  2066. int i, level, nCoeffs;
  2067. const uint16_t *quant_matrix;
  2068. nCoeffs= s->block_last_index[n];
  2069. if (n < 4)
  2070. block[0] = block[0] * s->y_dc_scale;
  2071. else
  2072. block[0] = block[0] * s->c_dc_scale;
  2073. /* XXX: only mpeg1 */
  2074. quant_matrix = s->intra_matrix;
  2075. for(i=1;i<=nCoeffs;i++) {
  2076. int j= s->intra_scantable.permutated[i];
  2077. level = block[j];
  2078. if (level) {
  2079. if (level < 0) {
  2080. level = -level;
  2081. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2082. level = (level - 1) | 1;
  2083. level = -level;
  2084. } else {
  2085. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2086. level = (level - 1) | 1;
  2087. }
  2088. block[j] = level;
  2089. }
  2090. }
  2091. }
  2092. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2093. int16_t *block, int n, int qscale)
  2094. {
  2095. int i, level, nCoeffs;
  2096. const uint16_t *quant_matrix;
  2097. nCoeffs= s->block_last_index[n];
  2098. quant_matrix = s->inter_matrix;
  2099. for(i=0; i<=nCoeffs; i++) {
  2100. int j= s->intra_scantable.permutated[i];
  2101. level = block[j];
  2102. if (level) {
  2103. if (level < 0) {
  2104. level = -level;
  2105. level = (((level << 1) + 1) * qscale *
  2106. ((int) (quant_matrix[j]))) >> 4;
  2107. level = (level - 1) | 1;
  2108. level = -level;
  2109. } else {
  2110. level = (((level << 1) + 1) * qscale *
  2111. ((int) (quant_matrix[j]))) >> 4;
  2112. level = (level - 1) | 1;
  2113. }
  2114. block[j] = level;
  2115. }
  2116. }
  2117. }
  2118. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2119. int16_t *block, int n, int qscale)
  2120. {
  2121. int i, level, nCoeffs;
  2122. const uint16_t *quant_matrix;
  2123. if(s->alternate_scan) nCoeffs= 63;
  2124. else nCoeffs= s->block_last_index[n];
  2125. if (n < 4)
  2126. block[0] = block[0] * s->y_dc_scale;
  2127. else
  2128. block[0] = block[0] * s->c_dc_scale;
  2129. quant_matrix = s->intra_matrix;
  2130. for(i=1;i<=nCoeffs;i++) {
  2131. int j= s->intra_scantable.permutated[i];
  2132. level = block[j];
  2133. if (level) {
  2134. if (level < 0) {
  2135. level = -level;
  2136. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2137. level = -level;
  2138. } else {
  2139. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2140. }
  2141. block[j] = level;
  2142. }
  2143. }
  2144. }
  2145. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2146. int16_t *block, int n, int qscale)
  2147. {
  2148. int i, level, nCoeffs;
  2149. const uint16_t *quant_matrix;
  2150. int sum=-1;
  2151. if(s->alternate_scan) nCoeffs= 63;
  2152. else nCoeffs= s->block_last_index[n];
  2153. if (n < 4)
  2154. block[0] = block[0] * s->y_dc_scale;
  2155. else
  2156. block[0] = block[0] * s->c_dc_scale;
  2157. quant_matrix = s->intra_matrix;
  2158. for(i=1;i<=nCoeffs;i++) {
  2159. int j= s->intra_scantable.permutated[i];
  2160. level = block[j];
  2161. if (level) {
  2162. if (level < 0) {
  2163. level = -level;
  2164. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2165. level = -level;
  2166. } else {
  2167. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2168. }
  2169. block[j] = level;
  2170. sum+=level;
  2171. }
  2172. }
  2173. block[63]^=sum&1;
  2174. }
  2175. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2176. int16_t *block, int n, int qscale)
  2177. {
  2178. int i, level, nCoeffs;
  2179. const uint16_t *quant_matrix;
  2180. int sum=-1;
  2181. if(s->alternate_scan) nCoeffs= 63;
  2182. else nCoeffs= s->block_last_index[n];
  2183. quant_matrix = s->inter_matrix;
  2184. for(i=0; i<=nCoeffs; i++) {
  2185. int j= s->intra_scantable.permutated[i];
  2186. level = block[j];
  2187. if (level) {
  2188. if (level < 0) {
  2189. level = -level;
  2190. level = (((level << 1) + 1) * qscale *
  2191. ((int) (quant_matrix[j]))) >> 4;
  2192. level = -level;
  2193. } else {
  2194. level = (((level << 1) + 1) * qscale *
  2195. ((int) (quant_matrix[j]))) >> 4;
  2196. }
  2197. block[j] = level;
  2198. sum+=level;
  2199. }
  2200. }
  2201. block[63]^=sum&1;
  2202. }
  2203. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2204. int16_t *block, int n, int qscale)
  2205. {
  2206. int i, level, qmul, qadd;
  2207. int nCoeffs;
  2208. assert(s->block_last_index[n]>=0);
  2209. qmul = qscale << 1;
  2210. if (!s->h263_aic) {
  2211. if (n < 4)
  2212. block[0] = block[0] * s->y_dc_scale;
  2213. else
  2214. block[0] = block[0] * s->c_dc_scale;
  2215. qadd = (qscale - 1) | 1;
  2216. }else{
  2217. qadd = 0;
  2218. }
  2219. if(s->ac_pred)
  2220. nCoeffs=63;
  2221. else
  2222. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2223. for(i=1; i<=nCoeffs; i++) {
  2224. level = block[i];
  2225. if (level) {
  2226. if (level < 0) {
  2227. level = level * qmul - qadd;
  2228. } else {
  2229. level = level * qmul + qadd;
  2230. }
  2231. block[i] = level;
  2232. }
  2233. }
  2234. }
  2235. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2236. int16_t *block, int n, int qscale)
  2237. {
  2238. int i, level, qmul, qadd;
  2239. int nCoeffs;
  2240. assert(s->block_last_index[n]>=0);
  2241. qadd = (qscale - 1) | 1;
  2242. qmul = qscale << 1;
  2243. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2244. for(i=0; i<=nCoeffs; i++) {
  2245. level = block[i];
  2246. if (level) {
  2247. if (level < 0) {
  2248. level = level * qmul - qadd;
  2249. } else {
  2250. level = level * qmul + qadd;
  2251. }
  2252. block[i] = level;
  2253. }
  2254. }
  2255. }
  2256. /**
  2257. * set qscale and update qscale dependent variables.
  2258. */
  2259. void ff_set_qscale(MpegEncContext * s, int qscale)
  2260. {
  2261. if (qscale < 1)
  2262. qscale = 1;
  2263. else if (qscale > 31)
  2264. qscale = 31;
  2265. s->qscale = qscale;
  2266. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2267. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2268. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2269. }
  2270. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2271. {
  2272. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2273. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2274. }
  2275. #if CONFIG_ERROR_RESILIENCE
  2276. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2277. {
  2278. ERContext *er = &s->er;
  2279. er->cur_pic = s->current_picture_ptr;
  2280. er->last_pic = s->last_picture_ptr;
  2281. er->next_pic = s->next_picture_ptr;
  2282. er->pp_time = s->pp_time;
  2283. er->pb_time = s->pb_time;
  2284. er->quarter_sample = s->quarter_sample;
  2285. er->partitioned_frame = s->partitioned_frame;
  2286. ff_er_frame_start(er);
  2287. }
  2288. #endif /* CONFIG_ERROR_RESILIENCE */