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.

3475 lines
113KB

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