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.

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