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.

3465 lines
113KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/atomic.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/bprint.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/crc.h"
  34. #include "libavutil/frame.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/samplefmt.h"
  40. #include "libavutil/dict.h"
  41. #include "avcodec.h"
  42. #include "dsputil.h"
  43. #include "libavutil/opt.h"
  44. #include "thread.h"
  45. #include "frame_thread_encoder.h"
  46. #include "internal.h"
  47. #include "bytestream.h"
  48. #include "version.h"
  49. #include <stdlib.h>
  50. #include <stdarg.h>
  51. #include <limits.h>
  52. #include <float.h>
  53. #if CONFIG_ICONV
  54. # include <iconv.h>
  55. #endif
  56. #if HAVE_PTHREADS
  57. #include <pthread.h>
  58. #elif HAVE_W32THREADS
  59. #include "compat/w32pthreads.h"
  60. #elif HAVE_OS2THREADS
  61. #include "compat/os2threads.h"
  62. #endif
  63. #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
  64. static int default_lockmgr_cb(void **arg, enum AVLockOp op)
  65. {
  66. void * volatile * mutex = arg;
  67. int err;
  68. switch (op) {
  69. case AV_LOCK_CREATE:
  70. return 0;
  71. case AV_LOCK_OBTAIN:
  72. if (!*mutex) {
  73. pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
  74. if (!tmp)
  75. return AVERROR(ENOMEM);
  76. if ((err = pthread_mutex_init(tmp, NULL))) {
  77. av_free(tmp);
  78. return AVERROR(err);
  79. }
  80. if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
  81. pthread_mutex_destroy(tmp);
  82. av_free(tmp);
  83. }
  84. }
  85. if ((err = pthread_mutex_lock(*mutex)))
  86. return AVERROR(err);
  87. return 0;
  88. case AV_LOCK_RELEASE:
  89. if ((err = pthread_mutex_unlock(*mutex)))
  90. return AVERROR(err);
  91. return 0;
  92. case AV_LOCK_DESTROY:
  93. if (*mutex)
  94. pthread_mutex_destroy(*mutex);
  95. av_free(*mutex);
  96. avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
  102. #else
  103. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
  104. #endif
  105. volatile int ff_avcodec_locked;
  106. static int volatile entangled_thread_counter = 0;
  107. static void *codec_mutex;
  108. static void *avformat_mutex;
  109. #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
  110. FF_SYMVER(void*, av_fast_realloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
  111. {
  112. return av_fast_realloc(ptr, size, min_size);
  113. }
  114. FF_SYMVER(void, av_fast_malloc, (void *ptr, unsigned int *size, size_t min_size), "LIBAVCODEC_55")
  115. {
  116. av_fast_malloc(ptr, size, min_size);
  117. }
  118. #endif
  119. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  120. {
  121. void **p = ptr;
  122. if (min_size < *size)
  123. return 0;
  124. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  125. av_free(*p);
  126. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  127. if (!*p)
  128. min_size = 0;
  129. *size = min_size;
  130. return 1;
  131. }
  132. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  133. {
  134. uint8_t **p = ptr;
  135. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  136. av_freep(p);
  137. *size = 0;
  138. return;
  139. }
  140. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  141. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  142. }
  143. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  144. {
  145. uint8_t **p = ptr;
  146. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  147. av_freep(p);
  148. *size = 0;
  149. return;
  150. }
  151. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  152. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  153. }
  154. /* encoder management */
  155. static AVCodec *first_avcodec = NULL;
  156. AVCodec *av_codec_next(const AVCodec *c)
  157. {
  158. if (c)
  159. return c->next;
  160. else
  161. return first_avcodec;
  162. }
  163. static av_cold void avcodec_init(void)
  164. {
  165. static int initialized = 0;
  166. if (initialized != 0)
  167. return;
  168. initialized = 1;
  169. if (CONFIG_DSPUTIL)
  170. ff_dsputil_static_init();
  171. }
  172. int av_codec_is_encoder(const AVCodec *codec)
  173. {
  174. return codec && (codec->encode_sub || codec->encode2);
  175. }
  176. int av_codec_is_decoder(const AVCodec *codec)
  177. {
  178. return codec && codec->decode;
  179. }
  180. av_cold void avcodec_register(AVCodec *codec)
  181. {
  182. AVCodec **p;
  183. avcodec_init();
  184. p = &first_avcodec;
  185. codec->next = NULL;
  186. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
  187. p = &(*p)->next;
  188. if (codec->init_static_data)
  189. codec->init_static_data(codec);
  190. }
  191. unsigned avcodec_get_edge_width(void)
  192. {
  193. return EDGE_WIDTH;
  194. }
  195. #if FF_API_SET_DIMENSIONS
  196. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  197. {
  198. int ret = ff_set_dimensions(s, width, height);
  199. if (ret < 0) {
  200. av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
  201. }
  202. }
  203. #endif
  204. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  205. {
  206. int ret = av_image_check_size(width, height, 0, s);
  207. if (ret < 0)
  208. width = height = 0;
  209. s->coded_width = width;
  210. s->coded_height = height;
  211. s->width = FF_CEIL_RSHIFT(width, s->lowres);
  212. s->height = FF_CEIL_RSHIFT(height, s->lowres);
  213. return ret;
  214. }
  215. #if HAVE_NEON || ARCH_PPC || HAVE_MMX
  216. # define STRIDE_ALIGN 16
  217. #else
  218. # define STRIDE_ALIGN 8
  219. #endif
  220. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  221. int linesize_align[AV_NUM_DATA_POINTERS])
  222. {
  223. int i;
  224. int w_align = 1;
  225. int h_align = 1;
  226. switch (s->pix_fmt) {
  227. case AV_PIX_FMT_YUV420P:
  228. case AV_PIX_FMT_YUYV422:
  229. case AV_PIX_FMT_UYVY422:
  230. case AV_PIX_FMT_YUV422P:
  231. case AV_PIX_FMT_YUV440P:
  232. case AV_PIX_FMT_YUV444P:
  233. case AV_PIX_FMT_GBRAP:
  234. case AV_PIX_FMT_GBRP:
  235. case AV_PIX_FMT_GRAY8:
  236. case AV_PIX_FMT_GRAY16BE:
  237. case AV_PIX_FMT_GRAY16LE:
  238. case AV_PIX_FMT_YUVJ420P:
  239. case AV_PIX_FMT_YUVJ422P:
  240. case AV_PIX_FMT_YUVJ440P:
  241. case AV_PIX_FMT_YUVJ444P:
  242. case AV_PIX_FMT_YUVA420P:
  243. case AV_PIX_FMT_YUVA422P:
  244. case AV_PIX_FMT_YUVA444P:
  245. case AV_PIX_FMT_YUV420P9LE:
  246. case AV_PIX_FMT_YUV420P9BE:
  247. case AV_PIX_FMT_YUV420P10LE:
  248. case AV_PIX_FMT_YUV420P10BE:
  249. case AV_PIX_FMT_YUV420P12LE:
  250. case AV_PIX_FMT_YUV420P12BE:
  251. case AV_PIX_FMT_YUV420P14LE:
  252. case AV_PIX_FMT_YUV420P14BE:
  253. case AV_PIX_FMT_YUV420P16LE:
  254. case AV_PIX_FMT_YUV420P16BE:
  255. case AV_PIX_FMT_YUV422P9LE:
  256. case AV_PIX_FMT_YUV422P9BE:
  257. case AV_PIX_FMT_YUV422P10LE:
  258. case AV_PIX_FMT_YUV422P10BE:
  259. case AV_PIX_FMT_YUV422P12LE:
  260. case AV_PIX_FMT_YUV422P12BE:
  261. case AV_PIX_FMT_YUV422P14LE:
  262. case AV_PIX_FMT_YUV422P14BE:
  263. case AV_PIX_FMT_YUV422P16LE:
  264. case AV_PIX_FMT_YUV422P16BE:
  265. case AV_PIX_FMT_YUV444P9LE:
  266. case AV_PIX_FMT_YUV444P9BE:
  267. case AV_PIX_FMT_YUV444P10LE:
  268. case AV_PIX_FMT_YUV444P10BE:
  269. case AV_PIX_FMT_YUV444P12LE:
  270. case AV_PIX_FMT_YUV444P12BE:
  271. case AV_PIX_FMT_YUV444P14LE:
  272. case AV_PIX_FMT_YUV444P14BE:
  273. case AV_PIX_FMT_YUV444P16LE:
  274. case AV_PIX_FMT_YUV444P16BE:
  275. case AV_PIX_FMT_YUVA420P9LE:
  276. case AV_PIX_FMT_YUVA420P9BE:
  277. case AV_PIX_FMT_YUVA420P10LE:
  278. case AV_PIX_FMT_YUVA420P10BE:
  279. case AV_PIX_FMT_YUVA420P16LE:
  280. case AV_PIX_FMT_YUVA420P16BE:
  281. case AV_PIX_FMT_YUVA422P9LE:
  282. case AV_PIX_FMT_YUVA422P9BE:
  283. case AV_PIX_FMT_YUVA422P10LE:
  284. case AV_PIX_FMT_YUVA422P10BE:
  285. case AV_PIX_FMT_YUVA422P16LE:
  286. case AV_PIX_FMT_YUVA422P16BE:
  287. case AV_PIX_FMT_YUVA444P9LE:
  288. case AV_PIX_FMT_YUVA444P9BE:
  289. case AV_PIX_FMT_YUVA444P10LE:
  290. case AV_PIX_FMT_YUVA444P10BE:
  291. case AV_PIX_FMT_YUVA444P16LE:
  292. case AV_PIX_FMT_YUVA444P16BE:
  293. case AV_PIX_FMT_GBRP9LE:
  294. case AV_PIX_FMT_GBRP9BE:
  295. case AV_PIX_FMT_GBRP10LE:
  296. case AV_PIX_FMT_GBRP10BE:
  297. case AV_PIX_FMT_GBRP12LE:
  298. case AV_PIX_FMT_GBRP12BE:
  299. case AV_PIX_FMT_GBRP14LE:
  300. case AV_PIX_FMT_GBRP14BE:
  301. w_align = 16; //FIXME assume 16 pixel per macroblock
  302. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  303. break;
  304. case AV_PIX_FMT_YUV411P:
  305. case AV_PIX_FMT_YUVJ411P:
  306. case AV_PIX_FMT_UYYVYY411:
  307. w_align = 32;
  308. h_align = 8;
  309. break;
  310. case AV_PIX_FMT_YUV410P:
  311. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  312. w_align = 64;
  313. h_align = 64;
  314. }
  315. break;
  316. case AV_PIX_FMT_RGB555:
  317. if (s->codec_id == AV_CODEC_ID_RPZA) {
  318. w_align = 4;
  319. h_align = 4;
  320. }
  321. break;
  322. case AV_PIX_FMT_PAL8:
  323. case AV_PIX_FMT_BGR8:
  324. case AV_PIX_FMT_RGB8:
  325. if (s->codec_id == AV_CODEC_ID_SMC ||
  326. s->codec_id == AV_CODEC_ID_CINEPAK) {
  327. w_align = 4;
  328. h_align = 4;
  329. }
  330. break;
  331. case AV_PIX_FMT_BGR24:
  332. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  333. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  334. w_align = 4;
  335. h_align = 4;
  336. }
  337. break;
  338. case AV_PIX_FMT_RGB24:
  339. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  340. w_align = 4;
  341. h_align = 4;
  342. }
  343. break;
  344. default:
  345. w_align = 1;
  346. h_align = 1;
  347. break;
  348. }
  349. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  350. w_align = FFMAX(w_align, 8);
  351. }
  352. *width = FFALIGN(*width, w_align);
  353. *height = FFALIGN(*height, h_align);
  354. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  355. // some of the optimized chroma MC reads one line too much
  356. // which is also done in mpeg decoders with lowres > 0
  357. *height += 2;
  358. for (i = 0; i < 4; i++)
  359. linesize_align[i] = STRIDE_ALIGN;
  360. }
  361. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  362. {
  363. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  364. int chroma_shift = desc->log2_chroma_w;
  365. int linesize_align[AV_NUM_DATA_POINTERS];
  366. int align;
  367. avcodec_align_dimensions2(s, width, height, linesize_align);
  368. align = FFMAX(linesize_align[0], linesize_align[3]);
  369. linesize_align[1] <<= chroma_shift;
  370. linesize_align[2] <<= chroma_shift;
  371. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  372. *width = FFALIGN(*width, align);
  373. }
  374. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  375. {
  376. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  377. return AVERROR(EINVAL);
  378. pos--;
  379. *xpos = (pos&1) * 128;
  380. *ypos = ((pos>>1)^(pos<4)) * 128;
  381. return 0;
  382. }
  383. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  384. {
  385. int pos, xout, yout;
  386. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  387. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  388. return pos;
  389. }
  390. return AVCHROMA_LOC_UNSPECIFIED;
  391. }
  392. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  393. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  394. int buf_size, int align)
  395. {
  396. int ch, planar, needed_size, ret = 0;
  397. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  398. frame->nb_samples, sample_fmt,
  399. align);
  400. if (buf_size < needed_size)
  401. return AVERROR(EINVAL);
  402. planar = av_sample_fmt_is_planar(sample_fmt);
  403. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  404. if (!(frame->extended_data = av_mallocz(nb_channels *
  405. sizeof(*frame->extended_data))))
  406. return AVERROR(ENOMEM);
  407. } else {
  408. frame->extended_data = frame->data;
  409. }
  410. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  411. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  412. sample_fmt, align)) < 0) {
  413. if (frame->extended_data != frame->data)
  414. av_freep(&frame->extended_data);
  415. return ret;
  416. }
  417. if (frame->extended_data != frame->data) {
  418. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  419. frame->data[ch] = frame->extended_data[ch];
  420. }
  421. return ret;
  422. }
  423. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  424. {
  425. FramePool *pool = avctx->internal->pool;
  426. int i, ret;
  427. switch (avctx->codec_type) {
  428. case AVMEDIA_TYPE_VIDEO: {
  429. AVPicture picture;
  430. int size[4] = { 0 };
  431. int w = frame->width;
  432. int h = frame->height;
  433. int tmpsize, unaligned;
  434. if (pool->format == frame->format &&
  435. pool->width == frame->width && pool->height == frame->height)
  436. return 0;
  437. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  438. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  439. w += EDGE_WIDTH * 2;
  440. h += EDGE_WIDTH * 2;
  441. }
  442. do {
  443. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  444. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  445. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  446. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  447. w += w & ~(w - 1);
  448. unaligned = 0;
  449. for (i = 0; i < 4; i++)
  450. unaligned |= picture.linesize[i] % pool->stride_align[i];
  451. } while (unaligned);
  452. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  453. NULL, picture.linesize);
  454. if (tmpsize < 0)
  455. return -1;
  456. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  457. size[i] = picture.data[i + 1] - picture.data[i];
  458. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  459. for (i = 0; i < 4; i++) {
  460. av_buffer_pool_uninit(&pool->pools[i]);
  461. pool->linesize[i] = picture.linesize[i];
  462. if (size[i]) {
  463. pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
  464. CONFIG_MEMORY_POISONING ?
  465. NULL :
  466. av_buffer_allocz);
  467. if (!pool->pools[i]) {
  468. ret = AVERROR(ENOMEM);
  469. goto fail;
  470. }
  471. }
  472. }
  473. pool->format = frame->format;
  474. pool->width = frame->width;
  475. pool->height = frame->height;
  476. break;
  477. }
  478. case AVMEDIA_TYPE_AUDIO: {
  479. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  480. int planar = av_sample_fmt_is_planar(frame->format);
  481. int planes = planar ? ch : 1;
  482. if (pool->format == frame->format && pool->planes == planes &&
  483. pool->channels == ch && frame->nb_samples == pool->samples)
  484. return 0;
  485. av_buffer_pool_uninit(&pool->pools[0]);
  486. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  487. frame->nb_samples, frame->format, 0);
  488. if (ret < 0)
  489. goto fail;
  490. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  491. if (!pool->pools[0]) {
  492. ret = AVERROR(ENOMEM);
  493. goto fail;
  494. }
  495. pool->format = frame->format;
  496. pool->planes = planes;
  497. pool->channels = ch;
  498. pool->samples = frame->nb_samples;
  499. break;
  500. }
  501. default: av_assert0(0);
  502. }
  503. return 0;
  504. fail:
  505. for (i = 0; i < 4; i++)
  506. av_buffer_pool_uninit(&pool->pools[i]);
  507. pool->format = -1;
  508. pool->planes = pool->channels = pool->samples = 0;
  509. pool->width = pool->height = 0;
  510. return ret;
  511. }
  512. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  513. {
  514. FramePool *pool = avctx->internal->pool;
  515. int planes = pool->planes;
  516. int i;
  517. frame->linesize[0] = pool->linesize[0];
  518. if (planes > AV_NUM_DATA_POINTERS) {
  519. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  520. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  521. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  522. sizeof(*frame->extended_buf));
  523. if (!frame->extended_data || !frame->extended_buf) {
  524. av_freep(&frame->extended_data);
  525. av_freep(&frame->extended_buf);
  526. return AVERROR(ENOMEM);
  527. }
  528. } else {
  529. frame->extended_data = frame->data;
  530. av_assert0(frame->nb_extended_buf == 0);
  531. }
  532. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  533. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  534. if (!frame->buf[i])
  535. goto fail;
  536. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  537. }
  538. for (i = 0; i < frame->nb_extended_buf; i++) {
  539. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  540. if (!frame->extended_buf[i])
  541. goto fail;
  542. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  543. }
  544. if (avctx->debug & FF_DEBUG_BUFFERS)
  545. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  546. return 0;
  547. fail:
  548. av_frame_unref(frame);
  549. return AVERROR(ENOMEM);
  550. }
  551. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  552. {
  553. FramePool *pool = s->internal->pool;
  554. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  555. int pixel_size = desc->comp[0].step_minus1 + 1;
  556. int h_chroma_shift, v_chroma_shift;
  557. int i;
  558. if (pic->data[0] != NULL) {
  559. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  560. return -1;
  561. }
  562. memset(pic->data, 0, sizeof(pic->data));
  563. pic->extended_data = pic->data;
  564. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  565. for (i = 0; i < 4 && pool->pools[i]; i++) {
  566. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  567. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  568. int is_planar = pool->pools[2] || (i==0 && s->pix_fmt == AV_PIX_FMT_GRAY8);
  569. pic->linesize[i] = pool->linesize[i];
  570. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  571. if (!pic->buf[i])
  572. goto fail;
  573. // no edge if EDGE EMU or not planar YUV
  574. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !is_planar)
  575. pic->data[i] = pic->buf[i]->data;
  576. else {
  577. pic->data[i] = pic->buf[i]->data +
  578. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  579. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  580. }
  581. }
  582. for (; i < AV_NUM_DATA_POINTERS; i++) {
  583. pic->data[i] = NULL;
  584. pic->linesize[i] = 0;
  585. }
  586. if (pic->data[1] && !pic->data[2])
  587. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  588. if (s->debug & FF_DEBUG_BUFFERS)
  589. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  590. return 0;
  591. fail:
  592. av_frame_unref(pic);
  593. return AVERROR(ENOMEM);
  594. }
  595. void avpriv_color_frame(AVFrame *frame, const int c[4])
  596. {
  597. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  598. int p, y, x;
  599. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  600. for (p = 0; p<desc->nb_components; p++) {
  601. uint8_t *dst = frame->data[p];
  602. int is_chroma = p == 1 || p == 2;
  603. int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  604. int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  605. for (y = 0; y < height; y++) {
  606. if (desc->comp[0].depth_minus1 >= 8) {
  607. for (x = 0; x<bytes; x++)
  608. ((uint16_t*)dst)[x] = c[p];
  609. }else
  610. memset(dst, c[p], bytes);
  611. dst += frame->linesize[p];
  612. }
  613. }
  614. }
  615. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  616. {
  617. int ret;
  618. if ((ret = update_frame_pool(avctx, frame)) < 0)
  619. return ret;
  620. #if FF_API_GET_BUFFER
  621. FF_DISABLE_DEPRECATION_WARNINGS
  622. frame->type = FF_BUFFER_TYPE_INTERNAL;
  623. FF_ENABLE_DEPRECATION_WARNINGS
  624. #endif
  625. switch (avctx->codec_type) {
  626. case AVMEDIA_TYPE_VIDEO:
  627. return video_get_buffer(avctx, frame);
  628. case AVMEDIA_TYPE_AUDIO:
  629. return audio_get_buffer(avctx, frame);
  630. default:
  631. return -1;
  632. }
  633. }
  634. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  635. {
  636. if (avctx->internal->pkt) {
  637. frame->pkt_pts = avctx->internal->pkt->pts;
  638. av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
  639. av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
  640. av_frame_set_pkt_size (frame, avctx->internal->pkt->size);
  641. } else {
  642. frame->pkt_pts = AV_NOPTS_VALUE;
  643. av_frame_set_pkt_pos (frame, -1);
  644. av_frame_set_pkt_duration(frame, 0);
  645. av_frame_set_pkt_size (frame, -1);
  646. }
  647. frame->reordered_opaque = avctx->reordered_opaque;
  648. switch (avctx->codec->type) {
  649. case AVMEDIA_TYPE_VIDEO:
  650. frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
  651. frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
  652. if (frame->format < 0)
  653. frame->format = avctx->pix_fmt;
  654. if (!frame->sample_aspect_ratio.num)
  655. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  656. if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
  657. av_frame_set_colorspace(frame, avctx->colorspace);
  658. if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
  659. av_frame_set_color_range(frame, avctx->color_range);
  660. break;
  661. case AVMEDIA_TYPE_AUDIO:
  662. if (!frame->sample_rate)
  663. frame->sample_rate = avctx->sample_rate;
  664. if (frame->format < 0)
  665. frame->format = avctx->sample_fmt;
  666. if (!frame->channel_layout) {
  667. if (avctx->channel_layout) {
  668. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  669. avctx->channels) {
  670. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  671. "configuration.\n");
  672. return AVERROR(EINVAL);
  673. }
  674. frame->channel_layout = avctx->channel_layout;
  675. } else {
  676. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  677. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  678. avctx->channels);
  679. return AVERROR(ENOSYS);
  680. }
  681. }
  682. }
  683. av_frame_set_channels(frame, avctx->channels);
  684. break;
  685. }
  686. return 0;
  687. }
  688. #if FF_API_GET_BUFFER
  689. FF_DISABLE_DEPRECATION_WARNINGS
  690. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  691. {
  692. return avcodec_default_get_buffer2(avctx, frame, 0);
  693. }
  694. typedef struct CompatReleaseBufPriv {
  695. AVCodecContext avctx;
  696. AVFrame frame;
  697. } CompatReleaseBufPriv;
  698. static void compat_free_buffer(void *opaque, uint8_t *data)
  699. {
  700. CompatReleaseBufPriv *priv = opaque;
  701. if (priv->avctx.release_buffer)
  702. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  703. av_freep(&priv);
  704. }
  705. static void compat_release_buffer(void *opaque, uint8_t *data)
  706. {
  707. AVBufferRef *buf = opaque;
  708. av_buffer_unref(&buf);
  709. }
  710. FF_ENABLE_DEPRECATION_WARNINGS
  711. #endif
  712. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  713. {
  714. int ret;
  715. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  716. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  717. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  718. return AVERROR(EINVAL);
  719. }
  720. }
  721. if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  722. return ret;
  723. #if FF_API_GET_BUFFER
  724. FF_DISABLE_DEPRECATION_WARNINGS
  725. /*
  726. * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
  727. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  728. * a dummy AVBuffer as its private data, unreffing it on free.
  729. * When all the planes are freed, the dummy buffer's free callback calls
  730. * release_buffer().
  731. */
  732. if (avctx->get_buffer) {
  733. CompatReleaseBufPriv *priv = NULL;
  734. AVBufferRef *dummy_buf = NULL;
  735. int planes, i, ret;
  736. if (flags & AV_GET_BUFFER_FLAG_REF)
  737. frame->reference = 1;
  738. ret = avctx->get_buffer(avctx, frame);
  739. if (ret < 0)
  740. return ret;
  741. /* return if the buffers are already set up
  742. * this would happen e.g. when a custom get_buffer() calls
  743. * avcodec_default_get_buffer
  744. */
  745. if (frame->buf[0])
  746. goto end;
  747. priv = av_mallocz(sizeof(*priv));
  748. if (!priv) {
  749. ret = AVERROR(ENOMEM);
  750. goto fail;
  751. }
  752. priv->avctx = *avctx;
  753. priv->frame = *frame;
  754. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  755. if (!dummy_buf) {
  756. ret = AVERROR(ENOMEM);
  757. goto fail;
  758. }
  759. #define WRAP_PLANE(ref_out, data, data_size) \
  760. do { \
  761. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  762. if (!dummy_ref) { \
  763. ret = AVERROR(ENOMEM); \
  764. goto fail; \
  765. } \
  766. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  767. dummy_ref, 0); \
  768. if (!ref_out) { \
  769. av_frame_unref(frame); \
  770. ret = AVERROR(ENOMEM); \
  771. goto fail; \
  772. } \
  773. } while (0)
  774. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  775. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  776. planes = av_pix_fmt_count_planes(frame->format);
  777. /* workaround for AVHWAccel plane count of 0, buf[0] is used as
  778. check for allocated buffers: make libavcodec happy */
  779. if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  780. planes = 1;
  781. if (!desc || planes <= 0) {
  782. ret = AVERROR(EINVAL);
  783. goto fail;
  784. }
  785. for (i = 0; i < planes; i++) {
  786. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  787. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  788. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  789. }
  790. } else {
  791. int planar = av_sample_fmt_is_planar(frame->format);
  792. planes = planar ? avctx->channels : 1;
  793. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  794. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  795. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  796. frame->nb_extended_buf);
  797. if (!frame->extended_buf) {
  798. ret = AVERROR(ENOMEM);
  799. goto fail;
  800. }
  801. }
  802. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  803. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  804. for (i = 0; i < frame->nb_extended_buf; i++)
  805. WRAP_PLANE(frame->extended_buf[i],
  806. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  807. frame->linesize[0]);
  808. }
  809. av_buffer_unref(&dummy_buf);
  810. end:
  811. frame->width = avctx->width;
  812. frame->height = avctx->height;
  813. return 0;
  814. fail:
  815. avctx->release_buffer(avctx, frame);
  816. av_freep(&priv);
  817. av_buffer_unref(&dummy_buf);
  818. return ret;
  819. }
  820. FF_ENABLE_DEPRECATION_WARNINGS
  821. #endif
  822. ret = avctx->get_buffer2(avctx, frame, flags);
  823. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  824. frame->width = avctx->width;
  825. frame->height = avctx->height;
  826. }
  827. return ret;
  828. }
  829. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  830. {
  831. int ret = get_buffer_internal(avctx, frame, flags);
  832. if (ret < 0)
  833. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  834. return ret;
  835. }
  836. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  837. {
  838. AVFrame tmp;
  839. int ret;
  840. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  841. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  842. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  843. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  844. av_frame_unref(frame);
  845. }
  846. ff_init_buffer_info(avctx, frame);
  847. if (!frame->data[0])
  848. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  849. if (av_frame_is_writable(frame))
  850. return 0;
  851. av_frame_move_ref(&tmp, frame);
  852. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  853. if (ret < 0) {
  854. av_frame_unref(&tmp);
  855. return ret;
  856. }
  857. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  858. frame->format, frame->width, frame->height);
  859. av_frame_unref(&tmp);
  860. return 0;
  861. }
  862. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  863. {
  864. int ret = reget_buffer_internal(avctx, frame);
  865. if (ret < 0)
  866. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  867. return ret;
  868. }
  869. #if FF_API_GET_BUFFER
  870. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  871. {
  872. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  873. av_frame_unref(pic);
  874. }
  875. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  876. {
  877. av_assert0(0);
  878. return AVERROR_BUG;
  879. }
  880. #endif
  881. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  882. {
  883. int i;
  884. for (i = 0; i < count; i++) {
  885. int r = func(c, (char *)arg + i * size);
  886. if (ret)
  887. ret[i] = r;
  888. }
  889. return 0;
  890. }
  891. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  892. {
  893. int i;
  894. for (i = 0; i < count; i++) {
  895. int r = func(c, arg, i, 0);
  896. if (ret)
  897. ret[i] = r;
  898. }
  899. return 0;
  900. }
  901. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  902. {
  903. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  904. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  905. }
  906. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  907. {
  908. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  909. ++fmt;
  910. return fmt[0];
  911. }
  912. #if FF_API_AVFRAME_LAVC
  913. void avcodec_get_frame_defaults(AVFrame *frame)
  914. {
  915. #if LIBAVCODEC_VERSION_MAJOR >= 55
  916. // extended_data should explicitly be freed when needed, this code is unsafe currently
  917. // also this is not compatible to the <55 ABI/API
  918. if (frame->extended_data != frame->data && 0)
  919. av_freep(&frame->extended_data);
  920. #endif
  921. memset(frame, 0, sizeof(AVFrame));
  922. frame->pts =
  923. frame->pkt_dts =
  924. frame->pkt_pts = AV_NOPTS_VALUE;
  925. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  926. av_frame_set_pkt_duration (frame, 0);
  927. av_frame_set_pkt_pos (frame, -1);
  928. av_frame_set_pkt_size (frame, -1);
  929. frame->key_frame = 1;
  930. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  931. frame->format = -1; /* unknown */
  932. frame->extended_data = frame->data;
  933. av_frame_set_colorspace(frame, AVCOL_SPC_UNSPECIFIED);
  934. }
  935. AVFrame *avcodec_alloc_frame(void)
  936. {
  937. AVFrame *frame = av_malloc(sizeof(AVFrame));
  938. if (frame == NULL)
  939. return NULL;
  940. frame->extended_data = NULL;
  941. avcodec_get_frame_defaults(frame);
  942. return frame;
  943. }
  944. void avcodec_free_frame(AVFrame **frame)
  945. {
  946. av_frame_free(frame);
  947. }
  948. #endif
  949. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  950. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  951. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  952. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  953. int av_codec_get_max_lowres(const AVCodec *codec)
  954. {
  955. return codec->max_lowres;
  956. }
  957. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  958. {
  959. memset(sub, 0, sizeof(*sub));
  960. sub->pts = AV_NOPTS_VALUE;
  961. }
  962. static int get_bit_rate(AVCodecContext *ctx)
  963. {
  964. int bit_rate;
  965. int bits_per_sample;
  966. switch (ctx->codec_type) {
  967. case AVMEDIA_TYPE_VIDEO:
  968. case AVMEDIA_TYPE_DATA:
  969. case AVMEDIA_TYPE_SUBTITLE:
  970. case AVMEDIA_TYPE_ATTACHMENT:
  971. bit_rate = ctx->bit_rate;
  972. break;
  973. case AVMEDIA_TYPE_AUDIO:
  974. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  975. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  976. break;
  977. default:
  978. bit_rate = 0;
  979. break;
  980. }
  981. return bit_rate;
  982. }
  983. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  984. {
  985. int ret = 0;
  986. ff_unlock_avcodec();
  987. ret = avcodec_open2(avctx, codec, options);
  988. ff_lock_avcodec(avctx);
  989. return ret;
  990. }
  991. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  992. {
  993. int ret = 0;
  994. AVDictionary *tmp = NULL;
  995. if (avcodec_is_open(avctx))
  996. return 0;
  997. if ((!codec && !avctx->codec)) {
  998. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  999. return AVERROR(EINVAL);
  1000. }
  1001. if ((codec && avctx->codec && codec != avctx->codec)) {
  1002. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  1003. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  1004. return AVERROR(EINVAL);
  1005. }
  1006. if (!codec)
  1007. codec = avctx->codec;
  1008. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  1009. return AVERROR(EINVAL);
  1010. if (options)
  1011. av_dict_copy(&tmp, *options, 0);
  1012. ret = ff_lock_avcodec(avctx);
  1013. if (ret < 0)
  1014. return ret;
  1015. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  1016. if (!avctx->internal) {
  1017. ret = AVERROR(ENOMEM);
  1018. goto end;
  1019. }
  1020. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  1021. if (!avctx->internal->pool) {
  1022. ret = AVERROR(ENOMEM);
  1023. goto free_and_end;
  1024. }
  1025. avctx->internal->to_free = av_frame_alloc();
  1026. if (!avctx->internal->to_free) {
  1027. ret = AVERROR(ENOMEM);
  1028. goto free_and_end;
  1029. }
  1030. if (codec->priv_data_size > 0) {
  1031. if (!avctx->priv_data) {
  1032. avctx->priv_data = av_mallocz(codec->priv_data_size);
  1033. if (!avctx->priv_data) {
  1034. ret = AVERROR(ENOMEM);
  1035. goto end;
  1036. }
  1037. if (codec->priv_class) {
  1038. *(const AVClass **)avctx->priv_data = codec->priv_class;
  1039. av_opt_set_defaults(avctx->priv_data);
  1040. }
  1041. }
  1042. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  1043. goto free_and_end;
  1044. } else {
  1045. avctx->priv_data = NULL;
  1046. }
  1047. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  1048. goto free_and_end;
  1049. // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1050. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1051. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1052. if (avctx->coded_width && avctx->coded_height)
  1053. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1054. else if (avctx->width && avctx->height)
  1055. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1056. if (ret < 0)
  1057. goto free_and_end;
  1058. }
  1059. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1060. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1061. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1062. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1063. ff_set_dimensions(avctx, 0, 0);
  1064. }
  1065. /* if the decoder init function was already called previously,
  1066. * free the already allocated subtitle_header before overwriting it */
  1067. if (av_codec_is_decoder(codec))
  1068. av_freep(&avctx->subtitle_header);
  1069. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1070. ret = AVERROR(EINVAL);
  1071. goto free_and_end;
  1072. }
  1073. avctx->codec = codec;
  1074. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1075. avctx->codec_id == AV_CODEC_ID_NONE) {
  1076. avctx->codec_type = codec->type;
  1077. avctx->codec_id = codec->id;
  1078. }
  1079. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1080. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1081. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1082. ret = AVERROR(EINVAL);
  1083. goto free_and_end;
  1084. }
  1085. avctx->frame_number = 0;
  1086. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1087. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  1088. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1089. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1090. AVCodec *codec2;
  1091. av_log(avctx, AV_LOG_ERROR,
  1092. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1093. "add '-strict %d' if you want to use it.\n",
  1094. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1095. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1096. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  1097. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1098. codec_string, codec2->name);
  1099. ret = AVERROR_EXPERIMENTAL;
  1100. goto free_and_end;
  1101. }
  1102. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1103. (!avctx->time_base.num || !avctx->time_base.den)) {
  1104. avctx->time_base.num = 1;
  1105. avctx->time_base.den = avctx->sample_rate;
  1106. }
  1107. if (!HAVE_THREADS)
  1108. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1109. if (CONFIG_FRAME_THREAD_ENCODER) {
  1110. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1111. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1112. ff_lock_avcodec(avctx);
  1113. if (ret < 0)
  1114. goto free_and_end;
  1115. }
  1116. if (HAVE_THREADS
  1117. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1118. ret = ff_thread_init(avctx);
  1119. if (ret < 0) {
  1120. goto free_and_end;
  1121. }
  1122. }
  1123. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1124. avctx->thread_count = 1;
  1125. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1126. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1127. avctx->codec->max_lowres);
  1128. ret = AVERROR(EINVAL);
  1129. goto free_and_end;
  1130. }
  1131. if (av_codec_is_encoder(avctx->codec)) {
  1132. int i;
  1133. if (avctx->codec->sample_fmts) {
  1134. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1135. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1136. break;
  1137. if (avctx->channels == 1 &&
  1138. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1139. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1140. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1141. break;
  1142. }
  1143. }
  1144. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1145. char buf[128];
  1146. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1147. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1148. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1149. ret = AVERROR(EINVAL);
  1150. goto free_and_end;
  1151. }
  1152. }
  1153. if (avctx->codec->pix_fmts) {
  1154. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1155. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1156. break;
  1157. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1158. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1159. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1160. char buf[128];
  1161. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1162. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1163. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1164. ret = AVERROR(EINVAL);
  1165. goto free_and_end;
  1166. }
  1167. }
  1168. if (avctx->codec->supported_samplerates) {
  1169. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1170. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1171. break;
  1172. if (avctx->codec->supported_samplerates[i] == 0) {
  1173. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1174. avctx->sample_rate);
  1175. ret = AVERROR(EINVAL);
  1176. goto free_and_end;
  1177. }
  1178. }
  1179. if (avctx->codec->channel_layouts) {
  1180. if (!avctx->channel_layout) {
  1181. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1182. } else {
  1183. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1184. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1185. break;
  1186. if (avctx->codec->channel_layouts[i] == 0) {
  1187. char buf[512];
  1188. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1189. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1190. ret = AVERROR(EINVAL);
  1191. goto free_and_end;
  1192. }
  1193. }
  1194. }
  1195. if (avctx->channel_layout && avctx->channels) {
  1196. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1197. if (channels != avctx->channels) {
  1198. char buf[512];
  1199. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1200. av_log(avctx, AV_LOG_ERROR,
  1201. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1202. buf, channels, avctx->channels);
  1203. ret = AVERROR(EINVAL);
  1204. goto free_and_end;
  1205. }
  1206. } else if (avctx->channel_layout) {
  1207. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1208. }
  1209. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  1210. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  1211. ) {
  1212. if (avctx->width <= 0 || avctx->height <= 0) {
  1213. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1214. ret = AVERROR(EINVAL);
  1215. goto free_and_end;
  1216. }
  1217. }
  1218. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1219. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1220. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1221. }
  1222. if (!avctx->rc_initial_buffer_occupancy)
  1223. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1224. }
  1225. avctx->pts_correction_num_faulty_pts =
  1226. avctx->pts_correction_num_faulty_dts = 0;
  1227. avctx->pts_correction_last_pts =
  1228. avctx->pts_correction_last_dts = INT64_MIN;
  1229. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1230. || avctx->internal->frame_thread_encoder)) {
  1231. ret = avctx->codec->init(avctx);
  1232. if (ret < 0) {
  1233. goto free_and_end;
  1234. }
  1235. }
  1236. ret=0;
  1237. if (av_codec_is_decoder(avctx->codec)) {
  1238. if (!avctx->bit_rate)
  1239. avctx->bit_rate = get_bit_rate(avctx);
  1240. /* validate channel layout from the decoder */
  1241. if (avctx->channel_layout) {
  1242. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1243. if (!avctx->channels)
  1244. avctx->channels = channels;
  1245. else if (channels != avctx->channels) {
  1246. char buf[512];
  1247. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1248. av_log(avctx, AV_LOG_WARNING,
  1249. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1250. "ignoring specified channel layout\n",
  1251. buf, channels, avctx->channels);
  1252. avctx->channel_layout = 0;
  1253. }
  1254. }
  1255. if (avctx->channels && avctx->channels < 0 ||
  1256. avctx->channels > FF_SANE_NB_CHANNELS) {
  1257. ret = AVERROR(EINVAL);
  1258. goto free_and_end;
  1259. }
  1260. if (avctx->sub_charenc) {
  1261. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1262. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1263. "supported with subtitles codecs\n");
  1264. ret = AVERROR(EINVAL);
  1265. goto free_and_end;
  1266. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1267. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1268. "subtitles character encoding will be ignored\n",
  1269. avctx->codec_descriptor->name);
  1270. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1271. } else {
  1272. /* input character encoding is set for a text based subtitle
  1273. * codec at this point */
  1274. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1275. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1276. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1277. #if CONFIG_ICONV
  1278. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1279. if (cd == (iconv_t)-1) {
  1280. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1281. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1282. ret = AVERROR(errno);
  1283. goto free_and_end;
  1284. }
  1285. iconv_close(cd);
  1286. #else
  1287. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1288. "conversion needs a libavcodec built with iconv support "
  1289. "for this codec\n");
  1290. ret = AVERROR(ENOSYS);
  1291. goto free_and_end;
  1292. #endif
  1293. }
  1294. }
  1295. }
  1296. }
  1297. end:
  1298. ff_unlock_avcodec();
  1299. if (options) {
  1300. av_dict_free(options);
  1301. *options = tmp;
  1302. }
  1303. return ret;
  1304. free_and_end:
  1305. av_dict_free(&tmp);
  1306. av_freep(&avctx->priv_data);
  1307. if (avctx->internal)
  1308. av_freep(&avctx->internal->pool);
  1309. av_freep(&avctx->internal);
  1310. avctx->codec = NULL;
  1311. goto end;
  1312. }
  1313. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1314. {
  1315. if (avpkt->size < 0) {
  1316. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1317. return AVERROR(EINVAL);
  1318. }
  1319. if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1320. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1321. size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1322. return AVERROR(EINVAL);
  1323. }
  1324. if (avctx) {
  1325. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1326. if (!avpkt->data || avpkt->size < size) {
  1327. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1328. avpkt->data = avctx->internal->byte_buffer;
  1329. avpkt->size = avctx->internal->byte_buffer_size;
  1330. avpkt->destruct = NULL;
  1331. }
  1332. }
  1333. if (avpkt->data) {
  1334. AVBufferRef *buf = avpkt->buf;
  1335. #if FF_API_DESTRUCT_PACKET
  1336. FF_DISABLE_DEPRECATION_WARNINGS
  1337. void *destruct = avpkt->destruct;
  1338. FF_ENABLE_DEPRECATION_WARNINGS
  1339. #endif
  1340. if (avpkt->size < size) {
  1341. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1342. return AVERROR(EINVAL);
  1343. }
  1344. av_init_packet(avpkt);
  1345. #if FF_API_DESTRUCT_PACKET
  1346. FF_DISABLE_DEPRECATION_WARNINGS
  1347. avpkt->destruct = destruct;
  1348. FF_ENABLE_DEPRECATION_WARNINGS
  1349. #endif
  1350. avpkt->buf = buf;
  1351. avpkt->size = size;
  1352. return 0;
  1353. } else {
  1354. int ret = av_new_packet(avpkt, size);
  1355. if (ret < 0)
  1356. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1357. return ret;
  1358. }
  1359. }
  1360. int ff_alloc_packet(AVPacket *avpkt, int size)
  1361. {
  1362. return ff_alloc_packet2(NULL, avpkt, size);
  1363. }
  1364. /**
  1365. * Pad last frame with silence.
  1366. */
  1367. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1368. {
  1369. AVFrame *frame = NULL;
  1370. int ret;
  1371. if (!(frame = av_frame_alloc()))
  1372. return AVERROR(ENOMEM);
  1373. frame->format = src->format;
  1374. frame->channel_layout = src->channel_layout;
  1375. av_frame_set_channels(frame, av_frame_get_channels(src));
  1376. frame->nb_samples = s->frame_size;
  1377. ret = av_frame_get_buffer(frame, 32);
  1378. if (ret < 0)
  1379. goto fail;
  1380. ret = av_frame_copy_props(frame, src);
  1381. if (ret < 0)
  1382. goto fail;
  1383. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1384. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1385. goto fail;
  1386. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1387. frame->nb_samples - src->nb_samples,
  1388. s->channels, s->sample_fmt)) < 0)
  1389. goto fail;
  1390. *dst = frame;
  1391. return 0;
  1392. fail:
  1393. av_frame_free(&frame);
  1394. return ret;
  1395. }
  1396. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1397. AVPacket *avpkt,
  1398. const AVFrame *frame,
  1399. int *got_packet_ptr)
  1400. {
  1401. AVFrame tmp;
  1402. AVFrame *padded_frame = NULL;
  1403. int ret;
  1404. AVPacket user_pkt = *avpkt;
  1405. int needs_realloc = !user_pkt.data;
  1406. *got_packet_ptr = 0;
  1407. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1408. av_free_packet(avpkt);
  1409. av_init_packet(avpkt);
  1410. return 0;
  1411. }
  1412. /* ensure that extended_data is properly set */
  1413. if (frame && !frame->extended_data) {
  1414. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1415. avctx->channels > AV_NUM_DATA_POINTERS) {
  1416. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1417. "with more than %d channels, but extended_data is not set.\n",
  1418. AV_NUM_DATA_POINTERS);
  1419. return AVERROR(EINVAL);
  1420. }
  1421. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1422. tmp = *frame;
  1423. tmp.extended_data = tmp.data;
  1424. frame = &tmp;
  1425. }
  1426. /* check for valid frame size */
  1427. if (frame) {
  1428. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1429. if (frame->nb_samples > avctx->frame_size) {
  1430. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1431. return AVERROR(EINVAL);
  1432. }
  1433. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1434. if (frame->nb_samples < avctx->frame_size &&
  1435. !avctx->internal->last_audio_frame) {
  1436. ret = pad_last_frame(avctx, &padded_frame, frame);
  1437. if (ret < 0)
  1438. return ret;
  1439. frame = padded_frame;
  1440. avctx->internal->last_audio_frame = 1;
  1441. }
  1442. if (frame->nb_samples != avctx->frame_size) {
  1443. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1444. ret = AVERROR(EINVAL);
  1445. goto end;
  1446. }
  1447. }
  1448. }
  1449. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1450. if (!ret) {
  1451. if (*got_packet_ptr) {
  1452. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1453. if (avpkt->pts == AV_NOPTS_VALUE)
  1454. avpkt->pts = frame->pts;
  1455. if (!avpkt->duration)
  1456. avpkt->duration = ff_samples_to_time_base(avctx,
  1457. frame->nb_samples);
  1458. }
  1459. avpkt->dts = avpkt->pts;
  1460. } else {
  1461. avpkt->size = 0;
  1462. }
  1463. }
  1464. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1465. needs_realloc = 0;
  1466. if (user_pkt.data) {
  1467. if (user_pkt.size >= avpkt->size) {
  1468. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1469. } else {
  1470. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1471. avpkt->size = user_pkt.size;
  1472. ret = -1;
  1473. }
  1474. avpkt->buf = user_pkt.buf;
  1475. avpkt->data = user_pkt.data;
  1476. avpkt->destruct = user_pkt.destruct;
  1477. } else {
  1478. if (av_dup_packet(avpkt) < 0) {
  1479. ret = AVERROR(ENOMEM);
  1480. }
  1481. }
  1482. }
  1483. if (!ret) {
  1484. if (needs_realloc && avpkt->data) {
  1485. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1486. if (ret >= 0)
  1487. avpkt->data = avpkt->buf->data;
  1488. }
  1489. avctx->frame_number++;
  1490. }
  1491. if (ret < 0 || !*got_packet_ptr) {
  1492. av_free_packet(avpkt);
  1493. av_init_packet(avpkt);
  1494. goto end;
  1495. }
  1496. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1497. * this needs to be moved to the encoders, but for now we can do it
  1498. * here to simplify things */
  1499. avpkt->flags |= AV_PKT_FLAG_KEY;
  1500. end:
  1501. av_frame_free(&padded_frame);
  1502. return ret;
  1503. }
  1504. #if FF_API_OLD_ENCODE_AUDIO
  1505. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1506. uint8_t *buf, int buf_size,
  1507. const short *samples)
  1508. {
  1509. AVPacket pkt;
  1510. AVFrame frame0 = { { 0 } };
  1511. AVFrame *frame;
  1512. int ret, samples_size, got_packet;
  1513. av_init_packet(&pkt);
  1514. pkt.data = buf;
  1515. pkt.size = buf_size;
  1516. if (samples) {
  1517. frame = &frame0;
  1518. avcodec_get_frame_defaults(frame);
  1519. if (avctx->frame_size) {
  1520. frame->nb_samples = avctx->frame_size;
  1521. } else {
  1522. /* if frame_size is not set, the number of samples must be
  1523. * calculated from the buffer size */
  1524. int64_t nb_samples;
  1525. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1526. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1527. "support this codec\n");
  1528. return AVERROR(EINVAL);
  1529. }
  1530. nb_samples = (int64_t)buf_size * 8 /
  1531. (av_get_bits_per_sample(avctx->codec_id) *
  1532. avctx->channels);
  1533. if (nb_samples >= INT_MAX)
  1534. return AVERROR(EINVAL);
  1535. frame->nb_samples = nb_samples;
  1536. }
  1537. /* it is assumed that the samples buffer is large enough based on the
  1538. * relevant parameters */
  1539. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1540. frame->nb_samples,
  1541. avctx->sample_fmt, 1);
  1542. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1543. avctx->sample_fmt,
  1544. (const uint8_t *)samples,
  1545. samples_size, 1)) < 0)
  1546. return ret;
  1547. /* fabricate frame pts from sample count.
  1548. * this is needed because the avcodec_encode_audio() API does not have
  1549. * a way for the user to provide pts */
  1550. if (avctx->sample_rate && avctx->time_base.num)
  1551. frame->pts = ff_samples_to_time_base(avctx,
  1552. avctx->internal->sample_count);
  1553. else
  1554. frame->pts = AV_NOPTS_VALUE;
  1555. avctx->internal->sample_count += frame->nb_samples;
  1556. } else {
  1557. frame = NULL;
  1558. }
  1559. got_packet = 0;
  1560. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1561. if (!ret && got_packet && avctx->coded_frame) {
  1562. avctx->coded_frame->pts = pkt.pts;
  1563. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1564. }
  1565. /* free any side data since we cannot return it */
  1566. av_packet_free_side_data(&pkt);
  1567. if (frame && frame->extended_data != frame->data)
  1568. av_freep(&frame->extended_data);
  1569. return ret ? ret : pkt.size;
  1570. }
  1571. #endif
  1572. #if FF_API_OLD_ENCODE_VIDEO
  1573. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1574. const AVFrame *pict)
  1575. {
  1576. AVPacket pkt;
  1577. int ret, got_packet = 0;
  1578. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1579. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1580. return -1;
  1581. }
  1582. av_init_packet(&pkt);
  1583. pkt.data = buf;
  1584. pkt.size = buf_size;
  1585. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1586. if (!ret && got_packet && avctx->coded_frame) {
  1587. avctx->coded_frame->pts = pkt.pts;
  1588. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1589. }
  1590. /* free any side data since we cannot return it */
  1591. if (pkt.side_data_elems > 0) {
  1592. int i;
  1593. for (i = 0; i < pkt.side_data_elems; i++)
  1594. av_free(pkt.side_data[i].data);
  1595. av_freep(&pkt.side_data);
  1596. pkt.side_data_elems = 0;
  1597. }
  1598. return ret ? ret : pkt.size;
  1599. }
  1600. #endif
  1601. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1602. AVPacket *avpkt,
  1603. const AVFrame *frame,
  1604. int *got_packet_ptr)
  1605. {
  1606. int ret;
  1607. AVPacket user_pkt = *avpkt;
  1608. int needs_realloc = !user_pkt.data;
  1609. *got_packet_ptr = 0;
  1610. if(CONFIG_FRAME_THREAD_ENCODER &&
  1611. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1612. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1613. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1614. avctx->stats_out[0] = '\0';
  1615. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1616. av_free_packet(avpkt);
  1617. av_init_packet(avpkt);
  1618. avpkt->size = 0;
  1619. return 0;
  1620. }
  1621. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1622. return AVERROR(EINVAL);
  1623. av_assert0(avctx->codec->encode2);
  1624. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1625. av_assert0(ret <= 0);
  1626. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1627. needs_realloc = 0;
  1628. if (user_pkt.data) {
  1629. if (user_pkt.size >= avpkt->size) {
  1630. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1631. } else {
  1632. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1633. avpkt->size = user_pkt.size;
  1634. ret = -1;
  1635. }
  1636. avpkt->buf = user_pkt.buf;
  1637. avpkt->data = user_pkt.data;
  1638. avpkt->destruct = user_pkt.destruct;
  1639. } else {
  1640. if (av_dup_packet(avpkt) < 0) {
  1641. ret = AVERROR(ENOMEM);
  1642. }
  1643. }
  1644. }
  1645. if (!ret) {
  1646. if (!*got_packet_ptr)
  1647. avpkt->size = 0;
  1648. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1649. avpkt->pts = avpkt->dts = frame->pts;
  1650. if (needs_realloc && avpkt->data) {
  1651. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1652. if (ret >= 0)
  1653. avpkt->data = avpkt->buf->data;
  1654. }
  1655. avctx->frame_number++;
  1656. }
  1657. if (ret < 0 || !*got_packet_ptr)
  1658. av_free_packet(avpkt);
  1659. else
  1660. av_packet_merge_side_data(avpkt);
  1661. emms_c();
  1662. return ret;
  1663. }
  1664. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1665. const AVSubtitle *sub)
  1666. {
  1667. int ret;
  1668. if (sub->start_display_time) {
  1669. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1670. return -1;
  1671. }
  1672. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1673. avctx->frame_number++;
  1674. return ret;
  1675. }
  1676. /**
  1677. * Attempt to guess proper monotonic timestamps for decoded video frames
  1678. * which might have incorrect times. Input timestamps may wrap around, in
  1679. * which case the output will as well.
  1680. *
  1681. * @param pts the pts field of the decoded AVPacket, as passed through
  1682. * AVFrame.pkt_pts
  1683. * @param dts the dts field of the decoded AVPacket
  1684. * @return one of the input values, may be AV_NOPTS_VALUE
  1685. */
  1686. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1687. int64_t reordered_pts, int64_t dts)
  1688. {
  1689. int64_t pts = AV_NOPTS_VALUE;
  1690. if (dts != AV_NOPTS_VALUE) {
  1691. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1692. ctx->pts_correction_last_dts = dts;
  1693. }
  1694. if (reordered_pts != AV_NOPTS_VALUE) {
  1695. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1696. ctx->pts_correction_last_pts = reordered_pts;
  1697. }
  1698. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1699. && reordered_pts != AV_NOPTS_VALUE)
  1700. pts = reordered_pts;
  1701. else
  1702. pts = dts;
  1703. return pts;
  1704. }
  1705. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1706. {
  1707. int size = 0, ret;
  1708. const uint8_t *data;
  1709. uint32_t flags;
  1710. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1711. if (!data)
  1712. return 0;
  1713. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1714. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1715. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1716. return AVERROR(EINVAL);
  1717. }
  1718. if (size < 4)
  1719. goto fail;
  1720. flags = bytestream_get_le32(&data);
  1721. size -= 4;
  1722. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1723. if (size < 4)
  1724. goto fail;
  1725. avctx->channels = bytestream_get_le32(&data);
  1726. size -= 4;
  1727. }
  1728. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1729. if (size < 8)
  1730. goto fail;
  1731. avctx->channel_layout = bytestream_get_le64(&data);
  1732. size -= 8;
  1733. }
  1734. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1735. if (size < 4)
  1736. goto fail;
  1737. avctx->sample_rate = bytestream_get_le32(&data);
  1738. size -= 4;
  1739. }
  1740. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1741. if (size < 8)
  1742. goto fail;
  1743. avctx->width = bytestream_get_le32(&data);
  1744. avctx->height = bytestream_get_le32(&data);
  1745. size -= 8;
  1746. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1747. if (ret < 0)
  1748. return ret;
  1749. }
  1750. return 0;
  1751. fail:
  1752. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1753. return AVERROR_INVALIDDATA;
  1754. }
  1755. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1756. {
  1757. int size;
  1758. const uint8_t *side_metadata;
  1759. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  1760. side_metadata = av_packet_get_side_data(avctx->internal->pkt,
  1761. AV_PKT_DATA_STRINGS_METADATA, &size);
  1762. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1763. }
  1764. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1765. {
  1766. int ret;
  1767. /* move the original frame to our backup */
  1768. av_frame_unref(avci->to_free);
  1769. av_frame_move_ref(avci->to_free, frame);
  1770. /* now copy everything except the AVBufferRefs back
  1771. * note that we make a COPY of the side data, so calling av_frame_free() on
  1772. * the caller's frame will work properly */
  1773. ret = av_frame_copy_props(frame, avci->to_free);
  1774. if (ret < 0)
  1775. return ret;
  1776. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1777. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1778. if (avci->to_free->extended_data != avci->to_free->data) {
  1779. int planes = av_frame_get_channels(avci->to_free);
  1780. int size = planes * sizeof(*frame->extended_data);
  1781. if (!size) {
  1782. av_frame_unref(frame);
  1783. return AVERROR_BUG;
  1784. }
  1785. frame->extended_data = av_malloc(size);
  1786. if (!frame->extended_data) {
  1787. av_frame_unref(frame);
  1788. return AVERROR(ENOMEM);
  1789. }
  1790. memcpy(frame->extended_data, avci->to_free->extended_data,
  1791. size);
  1792. } else
  1793. frame->extended_data = frame->data;
  1794. frame->format = avci->to_free->format;
  1795. frame->width = avci->to_free->width;
  1796. frame->height = avci->to_free->height;
  1797. frame->channel_layout = avci->to_free->channel_layout;
  1798. frame->nb_samples = avci->to_free->nb_samples;
  1799. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1800. return 0;
  1801. }
  1802. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1803. int *got_picture_ptr,
  1804. const AVPacket *avpkt)
  1805. {
  1806. AVCodecInternal *avci = avctx->internal;
  1807. int ret;
  1808. // copy to ensure we do not change avpkt
  1809. AVPacket tmp = *avpkt;
  1810. if (!avctx->codec)
  1811. return AVERROR(EINVAL);
  1812. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1813. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1814. return AVERROR(EINVAL);
  1815. }
  1816. *got_picture_ptr = 0;
  1817. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1818. return AVERROR(EINVAL);
  1819. av_frame_unref(picture);
  1820. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1821. int did_split = av_packet_split_side_data(&tmp);
  1822. ret = apply_param_change(avctx, &tmp);
  1823. if (ret < 0) {
  1824. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1825. if (avctx->err_recognition & AV_EF_EXPLODE)
  1826. goto fail;
  1827. }
  1828. avctx->internal->pkt = &tmp;
  1829. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1830. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1831. &tmp);
  1832. else {
  1833. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1834. &tmp);
  1835. picture->pkt_dts = avpkt->dts;
  1836. if(!avctx->has_b_frames){
  1837. av_frame_set_pkt_pos(picture, avpkt->pos);
  1838. }
  1839. //FIXME these should be under if(!avctx->has_b_frames)
  1840. /* get_buffer is supposed to set frame parameters */
  1841. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1842. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1843. if (!picture->width) picture->width = avctx->width;
  1844. if (!picture->height) picture->height = avctx->height;
  1845. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1846. }
  1847. }
  1848. add_metadata_from_side_data(avctx, picture);
  1849. fail:
  1850. emms_c(); //needed to avoid an emms_c() call before every return;
  1851. avctx->internal->pkt = NULL;
  1852. if (did_split) {
  1853. av_packet_free_side_data(&tmp);
  1854. if(ret == tmp.size)
  1855. ret = avpkt->size;
  1856. }
  1857. if (*got_picture_ptr) {
  1858. if (!avctx->refcounted_frames) {
  1859. int err = unrefcount_frame(avci, picture);
  1860. if (err < 0)
  1861. return err;
  1862. }
  1863. avctx->frame_number++;
  1864. av_frame_set_best_effort_timestamp(picture,
  1865. guess_correct_pts(avctx,
  1866. picture->pkt_pts,
  1867. picture->pkt_dts));
  1868. } else
  1869. av_frame_unref(picture);
  1870. } else
  1871. ret = 0;
  1872. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1873. * make sure it's set correctly */
  1874. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  1875. return ret;
  1876. }
  1877. #if FF_API_OLD_DECODE_AUDIO
  1878. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1879. int *frame_size_ptr,
  1880. AVPacket *avpkt)
  1881. {
  1882. AVFrame frame = { { 0 } };
  1883. int ret, got_frame = 0;
  1884. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1885. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1886. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1887. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1888. "avcodec_decode_audio4()\n");
  1889. avctx->get_buffer = avcodec_default_get_buffer;
  1890. avctx->release_buffer = avcodec_default_release_buffer;
  1891. }
  1892. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1893. if (ret >= 0 && got_frame) {
  1894. int ch, plane_size;
  1895. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1896. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1897. frame.nb_samples,
  1898. avctx->sample_fmt, 1);
  1899. if (*frame_size_ptr < data_size) {
  1900. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1901. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1902. return AVERROR(EINVAL);
  1903. }
  1904. memcpy(samples, frame.extended_data[0], plane_size);
  1905. if (planar && avctx->channels > 1) {
  1906. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1907. for (ch = 1; ch < avctx->channels; ch++) {
  1908. memcpy(out, frame.extended_data[ch], plane_size);
  1909. out += plane_size;
  1910. }
  1911. }
  1912. *frame_size_ptr = data_size;
  1913. } else {
  1914. *frame_size_ptr = 0;
  1915. }
  1916. return ret;
  1917. }
  1918. #endif
  1919. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1920. AVFrame *frame,
  1921. int *got_frame_ptr,
  1922. const AVPacket *avpkt)
  1923. {
  1924. AVCodecInternal *avci = avctx->internal;
  1925. int ret = 0;
  1926. *got_frame_ptr = 0;
  1927. if (!avpkt->data && avpkt->size) {
  1928. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1929. return AVERROR(EINVAL);
  1930. }
  1931. if (!avctx->codec)
  1932. return AVERROR(EINVAL);
  1933. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1934. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1935. return AVERROR(EINVAL);
  1936. }
  1937. av_frame_unref(frame);
  1938. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1939. uint8_t *side;
  1940. int side_size;
  1941. uint32_t discard_padding = 0;
  1942. // copy to ensure we do not change avpkt
  1943. AVPacket tmp = *avpkt;
  1944. int did_split = av_packet_split_side_data(&tmp);
  1945. ret = apply_param_change(avctx, &tmp);
  1946. if (ret < 0) {
  1947. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1948. if (avctx->err_recognition & AV_EF_EXPLODE)
  1949. goto fail;
  1950. }
  1951. avctx->internal->pkt = &tmp;
  1952. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1953. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  1954. else {
  1955. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1956. frame->pkt_dts = avpkt->dts;
  1957. }
  1958. if (ret >= 0 && *got_frame_ptr) {
  1959. add_metadata_from_side_data(avctx, frame);
  1960. avctx->frame_number++;
  1961. av_frame_set_best_effort_timestamp(frame,
  1962. guess_correct_pts(avctx,
  1963. frame->pkt_pts,
  1964. frame->pkt_dts));
  1965. if (frame->format == AV_SAMPLE_FMT_NONE)
  1966. frame->format = avctx->sample_fmt;
  1967. if (!frame->channel_layout)
  1968. frame->channel_layout = avctx->channel_layout;
  1969. if (!av_frame_get_channels(frame))
  1970. av_frame_set_channels(frame, avctx->channels);
  1971. if (!frame->sample_rate)
  1972. frame->sample_rate = avctx->sample_rate;
  1973. }
  1974. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1975. if(side && side_size>=10) {
  1976. avctx->internal->skip_samples = AV_RL32(side);
  1977. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1978. avctx->internal->skip_samples);
  1979. discard_padding = AV_RL32(side + 4);
  1980. }
  1981. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1982. if(frame->nb_samples <= avctx->internal->skip_samples){
  1983. *got_frame_ptr = 0;
  1984. avctx->internal->skip_samples -= frame->nb_samples;
  1985. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1986. avctx->internal->skip_samples);
  1987. } else {
  1988. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1989. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1990. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1991. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1992. (AVRational){1, avctx->sample_rate},
  1993. avctx->pkt_timebase);
  1994. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1995. frame->pkt_pts += diff_ts;
  1996. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1997. frame->pkt_dts += diff_ts;
  1998. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1999. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2000. } else {
  2001. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2002. }
  2003. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2004. avctx->internal->skip_samples, frame->nb_samples);
  2005. frame->nb_samples -= avctx->internal->skip_samples;
  2006. avctx->internal->skip_samples = 0;
  2007. }
  2008. }
  2009. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
  2010. if (discard_padding == frame->nb_samples) {
  2011. *got_frame_ptr = 0;
  2012. } else {
  2013. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2014. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2015. (AVRational){1, avctx->sample_rate},
  2016. avctx->pkt_timebase);
  2017. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2018. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2019. } else {
  2020. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2021. }
  2022. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2023. discard_padding, frame->nb_samples);
  2024. frame->nb_samples -= discard_padding;
  2025. }
  2026. }
  2027. fail:
  2028. avctx->internal->pkt = NULL;
  2029. if (did_split) {
  2030. av_packet_free_side_data(&tmp);
  2031. if(ret == tmp.size)
  2032. ret = avpkt->size;
  2033. }
  2034. if (ret >= 0 && *got_frame_ptr) {
  2035. if (!avctx->refcounted_frames) {
  2036. int err = unrefcount_frame(avci, frame);
  2037. if (err < 0)
  2038. return err;
  2039. }
  2040. } else
  2041. av_frame_unref(frame);
  2042. }
  2043. return ret;
  2044. }
  2045. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2046. static int recode_subtitle(AVCodecContext *avctx,
  2047. AVPacket *outpkt, const AVPacket *inpkt)
  2048. {
  2049. #if CONFIG_ICONV
  2050. iconv_t cd = (iconv_t)-1;
  2051. int ret = 0;
  2052. char *inb, *outb;
  2053. size_t inl, outl;
  2054. AVPacket tmp;
  2055. #endif
  2056. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2057. return 0;
  2058. #if CONFIG_ICONV
  2059. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2060. av_assert0(cd != (iconv_t)-1);
  2061. inb = inpkt->data;
  2062. inl = inpkt->size;
  2063. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  2064. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2065. ret = AVERROR(ENOMEM);
  2066. goto end;
  2067. }
  2068. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2069. if (ret < 0)
  2070. goto end;
  2071. outpkt->buf = tmp.buf;
  2072. outpkt->data = tmp.data;
  2073. outpkt->size = tmp.size;
  2074. outb = outpkt->data;
  2075. outl = outpkt->size;
  2076. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2077. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2078. outl >= outpkt->size || inl != 0) {
  2079. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2080. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2081. av_free_packet(&tmp);
  2082. ret = AVERROR(errno);
  2083. goto end;
  2084. }
  2085. outpkt->size -= outl;
  2086. memset(outpkt->data + outpkt->size, 0, outl);
  2087. end:
  2088. if (cd != (iconv_t)-1)
  2089. iconv_close(cd);
  2090. return ret;
  2091. #else
  2092. av_assert0(!"requesting subtitles recoding without iconv");
  2093. #endif
  2094. }
  2095. static int utf8_check(const uint8_t *str)
  2096. {
  2097. const uint8_t *byte;
  2098. uint32_t codepoint, min;
  2099. while (*str) {
  2100. byte = str;
  2101. GET_UTF8(codepoint, *(byte++), return 0;);
  2102. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2103. 1 << (5 * (byte - str) - 4);
  2104. if (codepoint < min || codepoint >= 0x110000 ||
  2105. codepoint == 0xFFFE /* BOM */ ||
  2106. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2107. return 0;
  2108. str = byte;
  2109. }
  2110. return 1;
  2111. }
  2112. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2113. int *got_sub_ptr,
  2114. AVPacket *avpkt)
  2115. {
  2116. int i, ret = 0;
  2117. if (!avpkt->data && avpkt->size) {
  2118. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2119. return AVERROR(EINVAL);
  2120. }
  2121. if (!avctx->codec)
  2122. return AVERROR(EINVAL);
  2123. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2124. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2125. return AVERROR(EINVAL);
  2126. }
  2127. *got_sub_ptr = 0;
  2128. avcodec_get_subtitle_defaults(sub);
  2129. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  2130. AVPacket pkt_recoded;
  2131. AVPacket tmp = *avpkt;
  2132. int did_split = av_packet_split_side_data(&tmp);
  2133. //apply_param_change(avctx, &tmp);
  2134. if (did_split) {
  2135. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2136. * proper padding.
  2137. * If the side data is smaller than the buffer padding size, the
  2138. * remaining bytes should have already been filled with zeros by the
  2139. * original packet allocation anyway. */
  2140. memset(tmp.data + tmp.size, 0,
  2141. FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
  2142. }
  2143. pkt_recoded = tmp;
  2144. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2145. if (ret < 0) {
  2146. *got_sub_ptr = 0;
  2147. } else {
  2148. avctx->internal->pkt = &pkt_recoded;
  2149. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2150. sub->pts = av_rescale_q(avpkt->pts,
  2151. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2152. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2153. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2154. !!*got_sub_ptr >= !!sub->num_rects);
  2155. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2156. avctx->pkt_timebase.num) {
  2157. AVRational ms = { 1, 1000 };
  2158. sub->end_display_time = av_rescale_q(avpkt->duration,
  2159. avctx->pkt_timebase, ms);
  2160. }
  2161. for (i = 0; i < sub->num_rects; i++) {
  2162. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2163. av_log(avctx, AV_LOG_ERROR,
  2164. "Invalid UTF-8 in decoded subtitles text; "
  2165. "maybe missing -sub_charenc option\n");
  2166. avsubtitle_free(sub);
  2167. return AVERROR_INVALIDDATA;
  2168. }
  2169. }
  2170. if (tmp.data != pkt_recoded.data) { // did we recode?
  2171. /* prevent from destroying side data from original packet */
  2172. pkt_recoded.side_data = NULL;
  2173. pkt_recoded.side_data_elems = 0;
  2174. av_free_packet(&pkt_recoded);
  2175. }
  2176. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2177. sub->format = 0;
  2178. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2179. sub->format = 1;
  2180. avctx->internal->pkt = NULL;
  2181. }
  2182. if (did_split) {
  2183. av_packet_free_side_data(&tmp);
  2184. if(ret == tmp.size)
  2185. ret = avpkt->size;
  2186. }
  2187. if (*got_sub_ptr)
  2188. avctx->frame_number++;
  2189. }
  2190. return ret;
  2191. }
  2192. void avsubtitle_free(AVSubtitle *sub)
  2193. {
  2194. int i;
  2195. for (i = 0; i < sub->num_rects; i++) {
  2196. av_freep(&sub->rects[i]->pict.data[0]);
  2197. av_freep(&sub->rects[i]->pict.data[1]);
  2198. av_freep(&sub->rects[i]->pict.data[2]);
  2199. av_freep(&sub->rects[i]->pict.data[3]);
  2200. av_freep(&sub->rects[i]->text);
  2201. av_freep(&sub->rects[i]->ass);
  2202. av_freep(&sub->rects[i]);
  2203. }
  2204. av_freep(&sub->rects);
  2205. memset(sub, 0, sizeof(AVSubtitle));
  2206. }
  2207. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  2208. {
  2209. int ret = 0;
  2210. ff_unlock_avcodec();
  2211. ret = avcodec_close(avctx);
  2212. ff_lock_avcodec(NULL);
  2213. return ret;
  2214. }
  2215. av_cold int avcodec_close(AVCodecContext *avctx)
  2216. {
  2217. int ret;
  2218. if (!avctx)
  2219. return 0;
  2220. ret = ff_lock_avcodec(avctx);
  2221. if (ret < 0)
  2222. return ret;
  2223. if (avcodec_is_open(avctx)) {
  2224. FramePool *pool = avctx->internal->pool;
  2225. int i;
  2226. if (CONFIG_FRAME_THREAD_ENCODER &&
  2227. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2228. ff_unlock_avcodec();
  2229. ff_frame_thread_encoder_free(avctx);
  2230. ff_lock_avcodec(avctx);
  2231. }
  2232. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2233. ff_thread_free(avctx);
  2234. if (avctx->codec && avctx->codec->close)
  2235. avctx->codec->close(avctx);
  2236. avctx->coded_frame = NULL;
  2237. avctx->internal->byte_buffer_size = 0;
  2238. av_freep(&avctx->internal->byte_buffer);
  2239. av_frame_free(&avctx->internal->to_free);
  2240. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2241. av_buffer_pool_uninit(&pool->pools[i]);
  2242. av_freep(&avctx->internal->pool);
  2243. av_freep(&avctx->internal);
  2244. }
  2245. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2246. av_opt_free(avctx->priv_data);
  2247. av_opt_free(avctx);
  2248. av_freep(&avctx->priv_data);
  2249. if (av_codec_is_encoder(avctx->codec))
  2250. av_freep(&avctx->extradata);
  2251. avctx->codec = NULL;
  2252. avctx->active_thread_type = 0;
  2253. ff_unlock_avcodec();
  2254. return 0;
  2255. }
  2256. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2257. {
  2258. switch(id){
  2259. //This is for future deprecatec codec ids, its empty since
  2260. //last major bump but will fill up again over time, please don't remove it
  2261. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2262. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2263. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2264. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2265. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2266. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2267. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2268. case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
  2269. case AV_CODEC_ID_HEVC_DEPRECATED: return AV_CODEC_ID_HEVC;
  2270. default : return id;
  2271. }
  2272. }
  2273. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2274. {
  2275. AVCodec *p, *experimental = NULL;
  2276. p = first_avcodec;
  2277. id= remap_deprecated_codec_id(id);
  2278. while (p) {
  2279. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2280. p->id == id) {
  2281. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2282. experimental = p;
  2283. } else
  2284. return p;
  2285. }
  2286. p = p->next;
  2287. }
  2288. return experimental;
  2289. }
  2290. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2291. {
  2292. return find_encdec(id, 1);
  2293. }
  2294. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2295. {
  2296. AVCodec *p;
  2297. if (!name)
  2298. return NULL;
  2299. p = first_avcodec;
  2300. while (p) {
  2301. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2302. return p;
  2303. p = p->next;
  2304. }
  2305. return NULL;
  2306. }
  2307. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2308. {
  2309. return find_encdec(id, 0);
  2310. }
  2311. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2312. {
  2313. AVCodec *p;
  2314. if (!name)
  2315. return NULL;
  2316. p = first_avcodec;
  2317. while (p) {
  2318. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2319. return p;
  2320. p = p->next;
  2321. }
  2322. return NULL;
  2323. }
  2324. const char *avcodec_get_name(enum AVCodecID id)
  2325. {
  2326. const AVCodecDescriptor *cd;
  2327. AVCodec *codec;
  2328. if (id == AV_CODEC_ID_NONE)
  2329. return "none";
  2330. cd = avcodec_descriptor_get(id);
  2331. if (cd)
  2332. return cd->name;
  2333. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2334. codec = avcodec_find_decoder(id);
  2335. if (codec)
  2336. return codec->name;
  2337. codec = avcodec_find_encoder(id);
  2338. if (codec)
  2339. return codec->name;
  2340. return "unknown_codec";
  2341. }
  2342. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2343. {
  2344. int i, len, ret = 0;
  2345. #define TAG_PRINT(x) \
  2346. (((x) >= '0' && (x) <= '9') || \
  2347. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2348. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2349. for (i = 0; i < 4; i++) {
  2350. len = snprintf(buf, buf_size,
  2351. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2352. buf += len;
  2353. buf_size = buf_size > len ? buf_size - len : 0;
  2354. ret += len;
  2355. codec_tag >>= 8;
  2356. }
  2357. return ret;
  2358. }
  2359. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2360. {
  2361. const char *codec_type;
  2362. const char *codec_name;
  2363. const char *profile = NULL;
  2364. const AVCodec *p;
  2365. int bitrate;
  2366. AVRational display_aspect_ratio;
  2367. if (!buf || buf_size <= 0)
  2368. return;
  2369. codec_type = av_get_media_type_string(enc->codec_type);
  2370. codec_name = avcodec_get_name(enc->codec_id);
  2371. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2372. if (enc->codec)
  2373. p = enc->codec;
  2374. else
  2375. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2376. avcodec_find_decoder(enc->codec_id);
  2377. if (p)
  2378. profile = av_get_profile_name(p, enc->profile);
  2379. }
  2380. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2381. codec_name);
  2382. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2383. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2384. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2385. if (profile)
  2386. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2387. if (enc->codec_tag) {
  2388. char tag_buf[32];
  2389. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2390. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2391. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2392. }
  2393. switch (enc->codec_type) {
  2394. case AVMEDIA_TYPE_VIDEO:
  2395. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2396. char detail[256] = "(";
  2397. const char *colorspace_name;
  2398. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2399. ", %s",
  2400. av_get_pix_fmt_name(enc->pix_fmt));
  2401. if (enc->bits_per_raw_sample &&
  2402. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2403. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2404. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2405. av_strlcatf(detail, sizeof(detail),
  2406. enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
  2407. colorspace_name = av_get_colorspace_name(enc->colorspace);
  2408. if (colorspace_name)
  2409. av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
  2410. if (strlen(detail) > 1) {
  2411. detail[strlen(detail) - 2] = 0;
  2412. av_strlcatf(buf, buf_size, "%s)", detail);
  2413. }
  2414. }
  2415. if (enc->width) {
  2416. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2417. ", %dx%d",
  2418. enc->width, enc->height);
  2419. if (enc->sample_aspect_ratio.num) {
  2420. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2421. enc->width * enc->sample_aspect_ratio.num,
  2422. enc->height * enc->sample_aspect_ratio.den,
  2423. 1024 * 1024);
  2424. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2425. " [SAR %d:%d DAR %d:%d]",
  2426. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2427. display_aspect_ratio.num, display_aspect_ratio.den);
  2428. }
  2429. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2430. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2431. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2432. ", %d/%d",
  2433. enc->time_base.num / g, enc->time_base.den / g);
  2434. }
  2435. }
  2436. if (encode) {
  2437. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2438. ", q=%d-%d", enc->qmin, enc->qmax);
  2439. }
  2440. break;
  2441. case AVMEDIA_TYPE_AUDIO:
  2442. if (enc->sample_rate) {
  2443. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2444. ", %d Hz", enc->sample_rate);
  2445. }
  2446. av_strlcat(buf, ", ", buf_size);
  2447. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2448. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2449. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2450. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2451. }
  2452. break;
  2453. case AVMEDIA_TYPE_DATA:
  2454. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2455. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2456. if (g)
  2457. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2458. ", %d/%d",
  2459. enc->time_base.num / g, enc->time_base.den / g);
  2460. }
  2461. break;
  2462. case AVMEDIA_TYPE_SUBTITLE:
  2463. if (enc->width)
  2464. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2465. ", %dx%d", enc->width, enc->height);
  2466. break;
  2467. default:
  2468. return;
  2469. }
  2470. if (encode) {
  2471. if (enc->flags & CODEC_FLAG_PASS1)
  2472. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2473. ", pass 1");
  2474. if (enc->flags & CODEC_FLAG_PASS2)
  2475. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2476. ", pass 2");
  2477. }
  2478. bitrate = get_bit_rate(enc);
  2479. if (bitrate != 0) {
  2480. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2481. ", %d kb/s", bitrate / 1000);
  2482. } else if (enc->rc_max_rate > 0) {
  2483. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2484. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2485. }
  2486. }
  2487. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2488. {
  2489. const AVProfile *p;
  2490. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2491. return NULL;
  2492. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2493. if (p->profile == profile)
  2494. return p->name;
  2495. return NULL;
  2496. }
  2497. unsigned avcodec_version(void)
  2498. {
  2499. // av_assert0(AV_CODEC_ID_V410==164);
  2500. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2501. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2502. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2503. av_assert0(AV_CODEC_ID_SRT==94216);
  2504. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2505. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2506. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2507. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2508. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2509. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2510. return LIBAVCODEC_VERSION_INT;
  2511. }
  2512. const char *avcodec_configuration(void)
  2513. {
  2514. return FFMPEG_CONFIGURATION;
  2515. }
  2516. const char *avcodec_license(void)
  2517. {
  2518. #define LICENSE_PREFIX "libavcodec license: "
  2519. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2520. }
  2521. void avcodec_flush_buffers(AVCodecContext *avctx)
  2522. {
  2523. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2524. ff_thread_flush(avctx);
  2525. else if (avctx->codec->flush)
  2526. avctx->codec->flush(avctx);
  2527. avctx->pts_correction_last_pts =
  2528. avctx->pts_correction_last_dts = INT64_MIN;
  2529. if (!avctx->refcounted_frames)
  2530. av_frame_unref(avctx->internal->to_free);
  2531. }
  2532. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2533. {
  2534. switch (codec_id) {
  2535. case AV_CODEC_ID_8SVX_EXP:
  2536. case AV_CODEC_ID_8SVX_FIB:
  2537. case AV_CODEC_ID_ADPCM_CT:
  2538. case AV_CODEC_ID_ADPCM_IMA_APC:
  2539. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2540. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2541. case AV_CODEC_ID_ADPCM_IMA_WS:
  2542. case AV_CODEC_ID_ADPCM_G722:
  2543. case AV_CODEC_ID_ADPCM_YAMAHA:
  2544. return 4;
  2545. case AV_CODEC_ID_PCM_ALAW:
  2546. case AV_CODEC_ID_PCM_MULAW:
  2547. case AV_CODEC_ID_PCM_S8:
  2548. case AV_CODEC_ID_PCM_S8_PLANAR:
  2549. case AV_CODEC_ID_PCM_U8:
  2550. case AV_CODEC_ID_PCM_ZORK:
  2551. return 8;
  2552. case AV_CODEC_ID_PCM_S16BE:
  2553. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2554. case AV_CODEC_ID_PCM_S16LE:
  2555. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2556. case AV_CODEC_ID_PCM_U16BE:
  2557. case AV_CODEC_ID_PCM_U16LE:
  2558. return 16;
  2559. case AV_CODEC_ID_PCM_S24DAUD:
  2560. case AV_CODEC_ID_PCM_S24BE:
  2561. case AV_CODEC_ID_PCM_S24LE:
  2562. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2563. case AV_CODEC_ID_PCM_U24BE:
  2564. case AV_CODEC_ID_PCM_U24LE:
  2565. return 24;
  2566. case AV_CODEC_ID_PCM_S32BE:
  2567. case AV_CODEC_ID_PCM_S32LE:
  2568. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2569. case AV_CODEC_ID_PCM_U32BE:
  2570. case AV_CODEC_ID_PCM_U32LE:
  2571. case AV_CODEC_ID_PCM_F32BE:
  2572. case AV_CODEC_ID_PCM_F32LE:
  2573. return 32;
  2574. case AV_CODEC_ID_PCM_F64BE:
  2575. case AV_CODEC_ID_PCM_F64LE:
  2576. return 64;
  2577. default:
  2578. return 0;
  2579. }
  2580. }
  2581. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2582. {
  2583. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2584. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2585. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2586. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2587. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2588. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2589. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2590. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2591. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2592. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2593. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2594. };
  2595. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2596. return AV_CODEC_ID_NONE;
  2597. if (be < 0 || be > 1)
  2598. be = AV_NE(1, 0);
  2599. return map[fmt][be];
  2600. }
  2601. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2602. {
  2603. switch (codec_id) {
  2604. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2605. return 2;
  2606. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2607. return 3;
  2608. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2609. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2610. case AV_CODEC_ID_ADPCM_IMA_QT:
  2611. case AV_CODEC_ID_ADPCM_SWF:
  2612. case AV_CODEC_ID_ADPCM_MS:
  2613. return 4;
  2614. default:
  2615. return av_get_exact_bits_per_sample(codec_id);
  2616. }
  2617. }
  2618. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2619. {
  2620. int id, sr, ch, ba, tag, bps;
  2621. id = avctx->codec_id;
  2622. sr = avctx->sample_rate;
  2623. ch = avctx->channels;
  2624. ba = avctx->block_align;
  2625. tag = avctx->codec_tag;
  2626. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2627. /* codecs with an exact constant bits per sample */
  2628. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2629. return (frame_bytes * 8LL) / (bps * ch);
  2630. bps = avctx->bits_per_coded_sample;
  2631. /* codecs with a fixed packet duration */
  2632. switch (id) {
  2633. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2634. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2635. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2636. case AV_CODEC_ID_AMR_NB:
  2637. case AV_CODEC_ID_EVRC:
  2638. case AV_CODEC_ID_GSM:
  2639. case AV_CODEC_ID_QCELP:
  2640. case AV_CODEC_ID_RA_288: return 160;
  2641. case AV_CODEC_ID_AMR_WB:
  2642. case AV_CODEC_ID_GSM_MS: return 320;
  2643. case AV_CODEC_ID_MP1: return 384;
  2644. case AV_CODEC_ID_ATRAC1: return 512;
  2645. case AV_CODEC_ID_ATRAC3: return 1024;
  2646. case AV_CODEC_ID_MP2:
  2647. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2648. case AV_CODEC_ID_AC3: return 1536;
  2649. }
  2650. if (sr > 0) {
  2651. /* calc from sample rate */
  2652. if (id == AV_CODEC_ID_TTA)
  2653. return 256 * sr / 245;
  2654. if (ch > 0) {
  2655. /* calc from sample rate and channels */
  2656. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2657. return (480 << (sr / 22050)) / ch;
  2658. }
  2659. }
  2660. if (ba > 0) {
  2661. /* calc from block_align */
  2662. if (id == AV_CODEC_ID_SIPR) {
  2663. switch (ba) {
  2664. case 20: return 160;
  2665. case 19: return 144;
  2666. case 29: return 288;
  2667. case 37: return 480;
  2668. }
  2669. } else if (id == AV_CODEC_ID_ILBC) {
  2670. switch (ba) {
  2671. case 38: return 160;
  2672. case 50: return 240;
  2673. }
  2674. }
  2675. }
  2676. if (frame_bytes > 0) {
  2677. /* calc from frame_bytes only */
  2678. if (id == AV_CODEC_ID_TRUESPEECH)
  2679. return 240 * (frame_bytes / 32);
  2680. if (id == AV_CODEC_ID_NELLYMOSER)
  2681. return 256 * (frame_bytes / 64);
  2682. if (id == AV_CODEC_ID_RA_144)
  2683. return 160 * (frame_bytes / 20);
  2684. if (id == AV_CODEC_ID_G723_1)
  2685. return 240 * (frame_bytes / 24);
  2686. if (bps > 0) {
  2687. /* calc from frame_bytes and bits_per_coded_sample */
  2688. if (id == AV_CODEC_ID_ADPCM_G726)
  2689. return frame_bytes * 8 / bps;
  2690. }
  2691. if (ch > 0) {
  2692. /* calc from frame_bytes and channels */
  2693. switch (id) {
  2694. case AV_CODEC_ID_ADPCM_AFC:
  2695. return frame_bytes / (9 * ch) * 16;
  2696. case AV_CODEC_ID_ADPCM_DTK:
  2697. return frame_bytes / (16 * ch) * 28;
  2698. case AV_CODEC_ID_ADPCM_4XM:
  2699. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2700. return (frame_bytes - 4 * ch) * 2 / ch;
  2701. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2702. return (frame_bytes - 4) * 2 / ch;
  2703. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2704. return (frame_bytes - 8) * 2 / ch;
  2705. case AV_CODEC_ID_ADPCM_XA:
  2706. return (frame_bytes / 128) * 224 / ch;
  2707. case AV_CODEC_ID_INTERPLAY_DPCM:
  2708. return (frame_bytes - 6 - ch) / ch;
  2709. case AV_CODEC_ID_ROQ_DPCM:
  2710. return (frame_bytes - 8) / ch;
  2711. case AV_CODEC_ID_XAN_DPCM:
  2712. return (frame_bytes - 2 * ch) / ch;
  2713. case AV_CODEC_ID_MACE3:
  2714. return 3 * frame_bytes / ch;
  2715. case AV_CODEC_ID_MACE6:
  2716. return 6 * frame_bytes / ch;
  2717. case AV_CODEC_ID_PCM_LXF:
  2718. return 2 * (frame_bytes / (5 * ch));
  2719. case AV_CODEC_ID_IAC:
  2720. case AV_CODEC_ID_IMC:
  2721. return 4 * frame_bytes / ch;
  2722. }
  2723. if (tag) {
  2724. /* calc from frame_bytes, channels, and codec_tag */
  2725. if (id == AV_CODEC_ID_SOL_DPCM) {
  2726. if (tag == 3)
  2727. return frame_bytes / ch;
  2728. else
  2729. return frame_bytes * 2 / ch;
  2730. }
  2731. }
  2732. if (ba > 0) {
  2733. /* calc from frame_bytes, channels, and block_align */
  2734. int blocks = frame_bytes / ba;
  2735. switch (avctx->codec_id) {
  2736. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2737. if (bps < 2 || bps > 5)
  2738. return 0;
  2739. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2740. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2741. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2742. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2743. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2744. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2745. return blocks * ((ba - 4 * ch) * 2 / ch);
  2746. case AV_CODEC_ID_ADPCM_MS:
  2747. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2748. }
  2749. }
  2750. if (bps > 0) {
  2751. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2752. switch (avctx->codec_id) {
  2753. case AV_CODEC_ID_PCM_DVD:
  2754. if(bps<4)
  2755. return 0;
  2756. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2757. case AV_CODEC_ID_PCM_BLURAY:
  2758. if(bps<4)
  2759. return 0;
  2760. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2761. case AV_CODEC_ID_S302M:
  2762. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2763. }
  2764. }
  2765. }
  2766. }
  2767. return 0;
  2768. }
  2769. #if !HAVE_THREADS
  2770. int ff_thread_init(AVCodecContext *s)
  2771. {
  2772. return -1;
  2773. }
  2774. #endif
  2775. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2776. {
  2777. unsigned int n = 0;
  2778. while (v >= 0xff) {
  2779. *s++ = 0xff;
  2780. v -= 0xff;
  2781. n++;
  2782. }
  2783. *s = v;
  2784. n++;
  2785. return n;
  2786. }
  2787. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2788. {
  2789. int i;
  2790. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2791. return i;
  2792. }
  2793. #if FF_API_MISSING_SAMPLE
  2794. FF_DISABLE_DEPRECATION_WARNINGS
  2795. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2796. {
  2797. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2798. "version to the newest one from Git. If the problem still "
  2799. "occurs, it means that your file has a feature which has not "
  2800. "been implemented.\n", feature);
  2801. if(want_sample)
  2802. av_log_ask_for_sample(avc, NULL);
  2803. }
  2804. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2805. {
  2806. va_list argument_list;
  2807. va_start(argument_list, msg);
  2808. if (msg)
  2809. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2810. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2811. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2812. "and contact the ffmpeg-devel mailing list.\n");
  2813. va_end(argument_list);
  2814. }
  2815. FF_ENABLE_DEPRECATION_WARNINGS
  2816. #endif /* FF_API_MISSING_SAMPLE */
  2817. static AVHWAccel *first_hwaccel = NULL;
  2818. void av_register_hwaccel(AVHWAccel *hwaccel)
  2819. {
  2820. AVHWAccel **p = &first_hwaccel;
  2821. hwaccel->next = NULL;
  2822. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  2823. p = &(*p)->next;
  2824. }
  2825. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2826. {
  2827. return hwaccel ? hwaccel->next : first_hwaccel;
  2828. }
  2829. AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
  2830. {
  2831. enum AVCodecID codec_id = avctx->codec->id;
  2832. enum AVPixelFormat pix_fmt = avctx->pix_fmt;
  2833. AVHWAccel *hwaccel = NULL;
  2834. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2835. if (hwaccel->id == codec_id
  2836. && hwaccel->pix_fmt == pix_fmt)
  2837. return hwaccel;
  2838. return NULL;
  2839. }
  2840. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2841. {
  2842. if (lockmgr_cb) {
  2843. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2844. return -1;
  2845. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2846. return -1;
  2847. }
  2848. lockmgr_cb = cb;
  2849. if (lockmgr_cb) {
  2850. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2851. return -1;
  2852. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2853. return -1;
  2854. }
  2855. return 0;
  2856. }
  2857. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2858. {
  2859. if (lockmgr_cb) {
  2860. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2861. return -1;
  2862. }
  2863. entangled_thread_counter++;
  2864. if (entangled_thread_counter != 1) {
  2865. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2866. if (!lockmgr_cb)
  2867. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  2868. ff_avcodec_locked = 1;
  2869. ff_unlock_avcodec();
  2870. return AVERROR(EINVAL);
  2871. }
  2872. av_assert0(!ff_avcodec_locked);
  2873. ff_avcodec_locked = 1;
  2874. return 0;
  2875. }
  2876. int ff_unlock_avcodec(void)
  2877. {
  2878. av_assert0(ff_avcodec_locked);
  2879. ff_avcodec_locked = 0;
  2880. entangled_thread_counter--;
  2881. if (lockmgr_cb) {
  2882. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2883. return -1;
  2884. }
  2885. return 0;
  2886. }
  2887. int avpriv_lock_avformat(void)
  2888. {
  2889. if (lockmgr_cb) {
  2890. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2891. return -1;
  2892. }
  2893. return 0;
  2894. }
  2895. int avpriv_unlock_avformat(void)
  2896. {
  2897. if (lockmgr_cb) {
  2898. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2899. return -1;
  2900. }
  2901. return 0;
  2902. }
  2903. unsigned int avpriv_toupper4(unsigned int x)
  2904. {
  2905. return av_toupper(x & 0xFF) +
  2906. (av_toupper((x >> 8) & 0xFF) << 8) +
  2907. (av_toupper((x >> 16) & 0xFF) << 16) +
  2908. (av_toupper((x >> 24) & 0xFF) << 24);
  2909. }
  2910. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2911. {
  2912. int ret;
  2913. dst->owner = src->owner;
  2914. ret = av_frame_ref(dst->f, src->f);
  2915. if (ret < 0)
  2916. return ret;
  2917. if (src->progress &&
  2918. !(dst->progress = av_buffer_ref(src->progress))) {
  2919. ff_thread_release_buffer(dst->owner, dst);
  2920. return AVERROR(ENOMEM);
  2921. }
  2922. return 0;
  2923. }
  2924. #if !HAVE_THREADS
  2925. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2926. {
  2927. return avctx->get_format(avctx, fmt);
  2928. }
  2929. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2930. {
  2931. f->owner = avctx;
  2932. return ff_get_buffer(avctx, f->f, flags);
  2933. }
  2934. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2935. {
  2936. av_frame_unref(f->f);
  2937. }
  2938. void ff_thread_finish_setup(AVCodecContext *avctx)
  2939. {
  2940. }
  2941. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2942. {
  2943. }
  2944. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2945. {
  2946. }
  2947. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2948. {
  2949. return 1;
  2950. }
  2951. int ff_alloc_entries(AVCodecContext *avctx, int count)
  2952. {
  2953. return 0;
  2954. }
  2955. void ff_reset_entries(AVCodecContext *avctx)
  2956. {
  2957. }
  2958. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  2959. {
  2960. }
  2961. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  2962. {
  2963. }
  2964. #endif
  2965. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2966. {
  2967. AVCodec *c= avcodec_find_decoder(codec_id);
  2968. if(!c)
  2969. c= avcodec_find_encoder(codec_id);
  2970. if(c)
  2971. return c->type;
  2972. if (codec_id <= AV_CODEC_ID_NONE)
  2973. return AVMEDIA_TYPE_UNKNOWN;
  2974. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2975. return AVMEDIA_TYPE_VIDEO;
  2976. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2977. return AVMEDIA_TYPE_AUDIO;
  2978. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2979. return AVMEDIA_TYPE_SUBTITLE;
  2980. return AVMEDIA_TYPE_UNKNOWN;
  2981. }
  2982. int avcodec_is_open(AVCodecContext *s)
  2983. {
  2984. return !!s->internal;
  2985. }
  2986. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2987. {
  2988. int ret;
  2989. char *str;
  2990. ret = av_bprint_finalize(buf, &str);
  2991. if (ret < 0)
  2992. return ret;
  2993. avctx->extradata = str;
  2994. /* Note: the string is NUL terminated (so extradata can be read as a
  2995. * string), but the ending character is not accounted in the size (in
  2996. * binary formats you are likely not supposed to mux that character). When
  2997. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2998. * zeros. */
  2999. avctx->extradata_size = buf->len;
  3000. return 0;
  3001. }
  3002. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3003. const uint8_t *end,
  3004. uint32_t *av_restrict state)
  3005. {
  3006. int i;
  3007. av_assert0(p <= end);
  3008. if (p >= end)
  3009. return end;
  3010. for (i = 0; i < 3; i++) {
  3011. uint32_t tmp = *state << 8;
  3012. *state = tmp + *(p++);
  3013. if (tmp == 0x100 || p == end)
  3014. return p;
  3015. }
  3016. while (p < end) {
  3017. if (p[-1] > 1 ) p += 3;
  3018. else if (p[-2] ) p += 2;
  3019. else if (p[-3]|(p[-1]-1)) p++;
  3020. else {
  3021. p++;
  3022. break;
  3023. }
  3024. }
  3025. p = FFMIN(p, end) - 4;
  3026. *state = AV_RB32(p);
  3027. return p + 4;
  3028. }