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.

3501 lines
114KB

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