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.

3464 lines
112KB

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