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.

3553 lines
116KB

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