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.

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