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.

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