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.

3434 lines
111KB

  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;
  1756. const uint8_t *side_metadata;
  1757. const uint8_t *end;
  1758. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  1759. side_metadata = av_packet_get_side_data(avctx->internal->pkt,
  1760. AV_PKT_DATA_STRINGS_METADATA, &size);
  1761. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1762. }
  1763. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1764. int *got_picture_ptr,
  1765. const AVPacket *avpkt)
  1766. {
  1767. AVCodecInternal *avci = avctx->internal;
  1768. int ret;
  1769. // copy to ensure we do not change avpkt
  1770. AVPacket tmp = *avpkt;
  1771. if (!avctx->codec)
  1772. return AVERROR(EINVAL);
  1773. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1774. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1775. return AVERROR(EINVAL);
  1776. }
  1777. *got_picture_ptr = 0;
  1778. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1779. return AVERROR(EINVAL);
  1780. avcodec_get_frame_defaults(picture);
  1781. if (!avctx->refcounted_frames)
  1782. av_frame_unref(&avci->to_free);
  1783. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1784. int did_split = av_packet_split_side_data(&tmp);
  1785. ret = apply_param_change(avctx, &tmp);
  1786. if (ret < 0) {
  1787. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1788. if (avctx->err_recognition & AV_EF_EXPLODE)
  1789. goto fail;
  1790. }
  1791. avctx->internal->pkt = &tmp;
  1792. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1793. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1794. &tmp);
  1795. else {
  1796. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1797. &tmp);
  1798. picture->pkt_dts = avpkt->dts;
  1799. if(!avctx->has_b_frames){
  1800. av_frame_set_pkt_pos(picture, avpkt->pos);
  1801. }
  1802. //FIXME these should be under if(!avctx->has_b_frames)
  1803. /* get_buffer is supposed to set frame parameters */
  1804. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1805. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1806. if (!picture->width) picture->width = avctx->width;
  1807. if (!picture->height) picture->height = avctx->height;
  1808. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1809. }
  1810. }
  1811. add_metadata_from_side_data(avctx, picture);
  1812. fail:
  1813. emms_c(); //needed to avoid an emms_c() call before every return;
  1814. avctx->internal->pkt = NULL;
  1815. if (did_split) {
  1816. av_packet_free_side_data(&tmp);
  1817. if(ret == tmp.size)
  1818. ret = avpkt->size;
  1819. }
  1820. if (ret < 0 && picture->data[0])
  1821. av_frame_unref(picture);
  1822. if (*got_picture_ptr) {
  1823. if (!avctx->refcounted_frames) {
  1824. avci->to_free = *picture;
  1825. avci->to_free.extended_data = avci->to_free.data;
  1826. memset(picture->buf, 0, sizeof(picture->buf));
  1827. }
  1828. avctx->frame_number++;
  1829. av_frame_set_best_effort_timestamp(picture,
  1830. guess_correct_pts(avctx,
  1831. picture->pkt_pts,
  1832. picture->pkt_dts));
  1833. }
  1834. } else
  1835. ret = 0;
  1836. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1837. * make sure it's set correctly */
  1838. picture->extended_data = picture->data;
  1839. return ret;
  1840. }
  1841. #if FF_API_OLD_DECODE_AUDIO
  1842. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1843. int *frame_size_ptr,
  1844. AVPacket *avpkt)
  1845. {
  1846. AVFrame frame = { { 0 } };
  1847. int ret, got_frame = 0;
  1848. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1849. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1850. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1851. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1852. "avcodec_decode_audio4()\n");
  1853. avctx->get_buffer = avcodec_default_get_buffer;
  1854. avctx->release_buffer = avcodec_default_release_buffer;
  1855. }
  1856. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1857. if (ret >= 0 && got_frame) {
  1858. int ch, plane_size;
  1859. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1860. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1861. frame.nb_samples,
  1862. avctx->sample_fmt, 1);
  1863. if (*frame_size_ptr < data_size) {
  1864. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1865. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1866. return AVERROR(EINVAL);
  1867. }
  1868. memcpy(samples, frame.extended_data[0], plane_size);
  1869. if (planar && avctx->channels > 1) {
  1870. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1871. for (ch = 1; ch < avctx->channels; ch++) {
  1872. memcpy(out, frame.extended_data[ch], plane_size);
  1873. out += plane_size;
  1874. }
  1875. }
  1876. *frame_size_ptr = data_size;
  1877. } else {
  1878. *frame_size_ptr = 0;
  1879. }
  1880. return ret;
  1881. }
  1882. #endif
  1883. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1884. AVFrame *frame,
  1885. int *got_frame_ptr,
  1886. const AVPacket *avpkt)
  1887. {
  1888. AVCodecInternal *avci = avctx->internal;
  1889. int planar, channels;
  1890. int ret = 0;
  1891. *got_frame_ptr = 0;
  1892. if (!avpkt->data && avpkt->size) {
  1893. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1894. return AVERROR(EINVAL);
  1895. }
  1896. if (!avctx->codec)
  1897. return AVERROR(EINVAL);
  1898. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1899. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1900. return AVERROR(EINVAL);
  1901. }
  1902. avcodec_get_frame_defaults(frame);
  1903. if (!avctx->refcounted_frames)
  1904. av_frame_unref(&avci->to_free);
  1905. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1906. uint8_t *side;
  1907. int side_size;
  1908. uint32_t discard_padding = 0;
  1909. // copy to ensure we do not change avpkt
  1910. AVPacket tmp = *avpkt;
  1911. int did_split = av_packet_split_side_data(&tmp);
  1912. ret = apply_param_change(avctx, &tmp);
  1913. if (ret < 0) {
  1914. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1915. if (avctx->err_recognition & AV_EF_EXPLODE)
  1916. goto fail;
  1917. }
  1918. avctx->internal->pkt = &tmp;
  1919. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1920. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  1921. else {
  1922. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1923. frame->pkt_dts = avpkt->dts;
  1924. }
  1925. if (ret >= 0 && *got_frame_ptr) {
  1926. add_metadata_from_side_data(avctx, frame);
  1927. avctx->frame_number++;
  1928. av_frame_set_best_effort_timestamp(frame,
  1929. guess_correct_pts(avctx,
  1930. frame->pkt_pts,
  1931. frame->pkt_dts));
  1932. if (frame->format == AV_SAMPLE_FMT_NONE)
  1933. frame->format = avctx->sample_fmt;
  1934. if (!frame->channel_layout)
  1935. frame->channel_layout = avctx->channel_layout;
  1936. if (!av_frame_get_channels(frame))
  1937. av_frame_set_channels(frame, avctx->channels);
  1938. if (!frame->sample_rate)
  1939. frame->sample_rate = avctx->sample_rate;
  1940. }
  1941. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1942. if(side && side_size>=10) {
  1943. avctx->internal->skip_samples = AV_RL32(side);
  1944. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1945. avctx->internal->skip_samples);
  1946. discard_padding = AV_RL32(side + 4);
  1947. }
  1948. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1949. if(frame->nb_samples <= avctx->internal->skip_samples){
  1950. *got_frame_ptr = 0;
  1951. avctx->internal->skip_samples -= frame->nb_samples;
  1952. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1953. avctx->internal->skip_samples);
  1954. } else {
  1955. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1956. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1957. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1958. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1959. (AVRational){1, avctx->sample_rate},
  1960. avctx->pkt_timebase);
  1961. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1962. frame->pkt_pts += diff_ts;
  1963. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1964. frame->pkt_dts += diff_ts;
  1965. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1966. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1967. } else {
  1968. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1969. }
  1970. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1971. avctx->internal->skip_samples, frame->nb_samples);
  1972. frame->nb_samples -= avctx->internal->skip_samples;
  1973. avctx->internal->skip_samples = 0;
  1974. }
  1975. }
  1976. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
  1977. if (discard_padding == frame->nb_samples) {
  1978. *got_frame_ptr = 0;
  1979. } else {
  1980. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1981. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  1982. (AVRational){1, avctx->sample_rate},
  1983. avctx->pkt_timebase);
  1984. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1985. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1986. } else {
  1987. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  1988. }
  1989. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  1990. discard_padding, frame->nb_samples);
  1991. frame->nb_samples -= discard_padding;
  1992. }
  1993. }
  1994. fail:
  1995. avctx->internal->pkt = NULL;
  1996. if (did_split) {
  1997. av_packet_free_side_data(&tmp);
  1998. if(ret == tmp.size)
  1999. ret = avpkt->size;
  2000. }
  2001. if (ret >= 0 && *got_frame_ptr) {
  2002. if (!avctx->refcounted_frames) {
  2003. avci->to_free = *frame;
  2004. avci->to_free.extended_data = avci->to_free.data;
  2005. memset(frame->buf, 0, sizeof(frame->buf));
  2006. frame->extended_buf = NULL;
  2007. frame->nb_extended_buf = 0;
  2008. }
  2009. } else if (frame->data[0])
  2010. av_frame_unref(frame);
  2011. }
  2012. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2013. * make sure it's set correctly; assume decoders that actually use
  2014. * extended_data are doing it correctly */
  2015. if (*got_frame_ptr) {
  2016. planar = av_sample_fmt_is_planar(frame->format);
  2017. channels = av_frame_get_channels(frame);
  2018. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  2019. frame->extended_data = frame->data;
  2020. } else {
  2021. frame->extended_data = NULL;
  2022. }
  2023. return ret;
  2024. }
  2025. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2026. static int recode_subtitle(AVCodecContext *avctx,
  2027. AVPacket *outpkt, const AVPacket *inpkt)
  2028. {
  2029. #if CONFIG_ICONV
  2030. iconv_t cd = (iconv_t)-1;
  2031. int ret = 0;
  2032. char *inb, *outb;
  2033. size_t inl, outl;
  2034. AVPacket tmp;
  2035. #endif
  2036. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2037. return 0;
  2038. #if CONFIG_ICONV
  2039. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2040. av_assert0(cd != (iconv_t)-1);
  2041. inb = inpkt->data;
  2042. inl = inpkt->size;
  2043. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  2044. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2045. ret = AVERROR(ENOMEM);
  2046. goto end;
  2047. }
  2048. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2049. if (ret < 0)
  2050. goto end;
  2051. outpkt->buf = tmp.buf;
  2052. outpkt->data = tmp.data;
  2053. outpkt->size = tmp.size;
  2054. outb = outpkt->data;
  2055. outl = outpkt->size;
  2056. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2057. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2058. outl >= outpkt->size || inl != 0) {
  2059. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2060. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2061. av_free_packet(&tmp);
  2062. ret = AVERROR(errno);
  2063. goto end;
  2064. }
  2065. outpkt->size -= outl;
  2066. memset(outpkt->data + outpkt->size, 0, outl);
  2067. end:
  2068. if (cd != (iconv_t)-1)
  2069. iconv_close(cd);
  2070. return ret;
  2071. #else
  2072. av_assert0(!"requesting subtitles recoding without iconv");
  2073. #endif
  2074. }
  2075. static int utf8_check(const uint8_t *str)
  2076. {
  2077. const uint8_t *byte;
  2078. uint32_t codepoint, min;
  2079. while (*str) {
  2080. byte = str;
  2081. GET_UTF8(codepoint, *(byte++), return 0;);
  2082. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2083. 1 << (5 * (byte - str) - 4);
  2084. if (codepoint < min || codepoint >= 0x110000 ||
  2085. codepoint == 0xFFFE /* BOM */ ||
  2086. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2087. return 0;
  2088. str = byte;
  2089. }
  2090. return 1;
  2091. }
  2092. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2093. int *got_sub_ptr,
  2094. AVPacket *avpkt)
  2095. {
  2096. int i, ret = 0;
  2097. if (!avpkt->data && avpkt->size) {
  2098. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2099. return AVERROR(EINVAL);
  2100. }
  2101. if (!avctx->codec)
  2102. return AVERROR(EINVAL);
  2103. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2104. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2105. return AVERROR(EINVAL);
  2106. }
  2107. *got_sub_ptr = 0;
  2108. avcodec_get_subtitle_defaults(sub);
  2109. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  2110. AVPacket pkt_recoded;
  2111. AVPacket tmp = *avpkt;
  2112. int did_split = av_packet_split_side_data(&tmp);
  2113. //apply_param_change(avctx, &tmp);
  2114. pkt_recoded = tmp;
  2115. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2116. if (ret < 0) {
  2117. *got_sub_ptr = 0;
  2118. } else {
  2119. avctx->internal->pkt = &pkt_recoded;
  2120. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2121. sub->pts = av_rescale_q(avpkt->pts,
  2122. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2123. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2124. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2125. !!*got_sub_ptr >= !!sub->num_rects);
  2126. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2127. avctx->pkt_timebase.num) {
  2128. AVRational ms = { 1, 1000 };
  2129. sub->end_display_time = av_rescale_q(avpkt->duration,
  2130. avctx->pkt_timebase, ms);
  2131. }
  2132. for (i = 0; i < sub->num_rects; i++) {
  2133. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2134. av_log(avctx, AV_LOG_ERROR,
  2135. "Invalid UTF-8 in decoded subtitles text; "
  2136. "maybe missing -sub_charenc option\n");
  2137. avsubtitle_free(sub);
  2138. return AVERROR_INVALIDDATA;
  2139. }
  2140. }
  2141. if (tmp.data != pkt_recoded.data) { // did we recode?
  2142. /* prevent from destroying side data from original packet */
  2143. pkt_recoded.side_data = NULL;
  2144. pkt_recoded.side_data_elems = 0;
  2145. av_free_packet(&pkt_recoded);
  2146. }
  2147. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2148. sub->format = 0;
  2149. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2150. sub->format = 1;
  2151. avctx->internal->pkt = NULL;
  2152. }
  2153. if (did_split) {
  2154. av_packet_free_side_data(&tmp);
  2155. if(ret == tmp.size)
  2156. ret = avpkt->size;
  2157. }
  2158. if (*got_sub_ptr)
  2159. avctx->frame_number++;
  2160. }
  2161. return ret;
  2162. }
  2163. void avsubtitle_free(AVSubtitle *sub)
  2164. {
  2165. int i;
  2166. for (i = 0; i < sub->num_rects; i++) {
  2167. av_freep(&sub->rects[i]->pict.data[0]);
  2168. av_freep(&sub->rects[i]->pict.data[1]);
  2169. av_freep(&sub->rects[i]->pict.data[2]);
  2170. av_freep(&sub->rects[i]->pict.data[3]);
  2171. av_freep(&sub->rects[i]->text);
  2172. av_freep(&sub->rects[i]->ass);
  2173. av_freep(&sub->rects[i]);
  2174. }
  2175. av_freep(&sub->rects);
  2176. memset(sub, 0, sizeof(AVSubtitle));
  2177. }
  2178. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  2179. {
  2180. int ret = 0;
  2181. ff_unlock_avcodec();
  2182. ret = avcodec_close(avctx);
  2183. ff_lock_avcodec(NULL);
  2184. return ret;
  2185. }
  2186. av_cold int avcodec_close(AVCodecContext *avctx)
  2187. {
  2188. int ret;
  2189. if (!avctx)
  2190. return 0;
  2191. ret = ff_lock_avcodec(avctx);
  2192. if (ret < 0)
  2193. return ret;
  2194. if (avcodec_is_open(avctx)) {
  2195. FramePool *pool = avctx->internal->pool;
  2196. int i;
  2197. if (CONFIG_FRAME_THREAD_ENCODER &&
  2198. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2199. ff_unlock_avcodec();
  2200. ff_frame_thread_encoder_free(avctx);
  2201. ff_lock_avcodec(avctx);
  2202. }
  2203. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2204. ff_thread_free(avctx);
  2205. if (avctx->codec && avctx->codec->close)
  2206. avctx->codec->close(avctx);
  2207. avctx->coded_frame = NULL;
  2208. avctx->internal->byte_buffer_size = 0;
  2209. av_freep(&avctx->internal->byte_buffer);
  2210. if (!avctx->refcounted_frames)
  2211. av_frame_unref(&avctx->internal->to_free);
  2212. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2213. av_buffer_pool_uninit(&pool->pools[i]);
  2214. av_freep(&avctx->internal->pool);
  2215. av_freep(&avctx->internal);
  2216. }
  2217. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2218. av_opt_free(avctx->priv_data);
  2219. av_opt_free(avctx);
  2220. av_freep(&avctx->priv_data);
  2221. if (av_codec_is_encoder(avctx->codec))
  2222. av_freep(&avctx->extradata);
  2223. avctx->codec = NULL;
  2224. avctx->active_thread_type = 0;
  2225. ff_unlock_avcodec();
  2226. return 0;
  2227. }
  2228. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2229. {
  2230. switch(id){
  2231. //This is for future deprecatec codec ids, its empty since
  2232. //last major bump but will fill up again over time, please don't remove it
  2233. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2234. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2235. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2236. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2237. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2238. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2239. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2240. case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
  2241. case AV_CODEC_ID_HEVC_DEPRECATED: return AV_CODEC_ID_HEVC;
  2242. default : return id;
  2243. }
  2244. }
  2245. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2246. {
  2247. AVCodec *p, *experimental = NULL;
  2248. p = first_avcodec;
  2249. id= remap_deprecated_codec_id(id);
  2250. while (p) {
  2251. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2252. p->id == id) {
  2253. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2254. experimental = p;
  2255. } else
  2256. return p;
  2257. }
  2258. p = p->next;
  2259. }
  2260. return experimental;
  2261. }
  2262. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2263. {
  2264. return find_encdec(id, 1);
  2265. }
  2266. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2267. {
  2268. AVCodec *p;
  2269. if (!name)
  2270. return NULL;
  2271. p = first_avcodec;
  2272. while (p) {
  2273. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2274. return p;
  2275. p = p->next;
  2276. }
  2277. return NULL;
  2278. }
  2279. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2280. {
  2281. return find_encdec(id, 0);
  2282. }
  2283. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2284. {
  2285. AVCodec *p;
  2286. if (!name)
  2287. return NULL;
  2288. p = first_avcodec;
  2289. while (p) {
  2290. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2291. return p;
  2292. p = p->next;
  2293. }
  2294. return NULL;
  2295. }
  2296. const char *avcodec_get_name(enum AVCodecID id)
  2297. {
  2298. const AVCodecDescriptor *cd;
  2299. AVCodec *codec;
  2300. if (id == AV_CODEC_ID_NONE)
  2301. return "none";
  2302. cd = avcodec_descriptor_get(id);
  2303. if (cd)
  2304. return cd->name;
  2305. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2306. codec = avcodec_find_decoder(id);
  2307. if (codec)
  2308. return codec->name;
  2309. codec = avcodec_find_encoder(id);
  2310. if (codec)
  2311. return codec->name;
  2312. return "unknown_codec";
  2313. }
  2314. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2315. {
  2316. int i, len, ret = 0;
  2317. #define TAG_PRINT(x) \
  2318. (((x) >= '0' && (x) <= '9') || \
  2319. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2320. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2321. for (i = 0; i < 4; i++) {
  2322. len = snprintf(buf, buf_size,
  2323. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2324. buf += len;
  2325. buf_size = buf_size > len ? buf_size - len : 0;
  2326. ret += len;
  2327. codec_tag >>= 8;
  2328. }
  2329. return ret;
  2330. }
  2331. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2332. {
  2333. const char *codec_type;
  2334. const char *codec_name;
  2335. const char *profile = NULL;
  2336. const AVCodec *p;
  2337. int bitrate;
  2338. AVRational display_aspect_ratio;
  2339. if (!buf || buf_size <= 0)
  2340. return;
  2341. codec_type = av_get_media_type_string(enc->codec_type);
  2342. codec_name = avcodec_get_name(enc->codec_id);
  2343. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2344. if (enc->codec)
  2345. p = enc->codec;
  2346. else
  2347. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2348. avcodec_find_decoder(enc->codec_id);
  2349. if (p)
  2350. profile = av_get_profile_name(p, enc->profile);
  2351. }
  2352. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2353. codec_name);
  2354. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2355. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2356. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2357. if (profile)
  2358. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2359. if (enc->codec_tag) {
  2360. char tag_buf[32];
  2361. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2362. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2363. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2364. }
  2365. switch (enc->codec_type) {
  2366. case AVMEDIA_TYPE_VIDEO:
  2367. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2368. char detail[256] = "(";
  2369. const char *colorspace_name;
  2370. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2371. ", %s",
  2372. av_get_pix_fmt_name(enc->pix_fmt));
  2373. if (enc->bits_per_raw_sample &&
  2374. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2375. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2376. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2377. av_strlcatf(detail, sizeof(detail),
  2378. enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
  2379. colorspace_name = av_get_colorspace_name(enc->colorspace);
  2380. if (colorspace_name)
  2381. av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
  2382. if (strlen(detail) > 1) {
  2383. detail[strlen(detail) - 2] = 0;
  2384. av_strlcatf(buf, buf_size, "%s)", detail);
  2385. }
  2386. }
  2387. if (enc->width) {
  2388. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2389. ", %dx%d",
  2390. enc->width, enc->height);
  2391. if (enc->sample_aspect_ratio.num) {
  2392. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2393. enc->width * enc->sample_aspect_ratio.num,
  2394. enc->height * enc->sample_aspect_ratio.den,
  2395. 1024 * 1024);
  2396. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2397. " [SAR %d:%d DAR %d:%d]",
  2398. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2399. display_aspect_ratio.num, display_aspect_ratio.den);
  2400. }
  2401. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2402. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2403. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2404. ", %d/%d",
  2405. enc->time_base.num / g, enc->time_base.den / g);
  2406. }
  2407. }
  2408. if (encode) {
  2409. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2410. ", q=%d-%d", enc->qmin, enc->qmax);
  2411. }
  2412. break;
  2413. case AVMEDIA_TYPE_AUDIO:
  2414. if (enc->sample_rate) {
  2415. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2416. ", %d Hz", enc->sample_rate);
  2417. }
  2418. av_strlcat(buf, ", ", buf_size);
  2419. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2420. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2421. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2422. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2423. }
  2424. break;
  2425. case AVMEDIA_TYPE_DATA:
  2426. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2427. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2428. if (g)
  2429. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2430. ", %d/%d",
  2431. enc->time_base.num / g, enc->time_base.den / g);
  2432. }
  2433. break;
  2434. case AVMEDIA_TYPE_SUBTITLE:
  2435. if (enc->width)
  2436. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2437. ", %dx%d", enc->width, enc->height);
  2438. break;
  2439. default:
  2440. return;
  2441. }
  2442. if (encode) {
  2443. if (enc->flags & CODEC_FLAG_PASS1)
  2444. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2445. ", pass 1");
  2446. if (enc->flags & CODEC_FLAG_PASS2)
  2447. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2448. ", pass 2");
  2449. }
  2450. bitrate = get_bit_rate(enc);
  2451. if (bitrate != 0) {
  2452. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2453. ", %d kb/s", bitrate / 1000);
  2454. } else if (enc->rc_max_rate > 0) {
  2455. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2456. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2457. }
  2458. }
  2459. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2460. {
  2461. const AVProfile *p;
  2462. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2463. return NULL;
  2464. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2465. if (p->profile == profile)
  2466. return p->name;
  2467. return NULL;
  2468. }
  2469. unsigned avcodec_version(void)
  2470. {
  2471. // av_assert0(AV_CODEC_ID_V410==164);
  2472. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2473. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2474. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2475. av_assert0(AV_CODEC_ID_SRT==94216);
  2476. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2477. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2478. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2479. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2480. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2481. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2482. return LIBAVCODEC_VERSION_INT;
  2483. }
  2484. const char *avcodec_configuration(void)
  2485. {
  2486. return FFMPEG_CONFIGURATION;
  2487. }
  2488. const char *avcodec_license(void)
  2489. {
  2490. #define LICENSE_PREFIX "libavcodec license: "
  2491. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2492. }
  2493. void avcodec_flush_buffers(AVCodecContext *avctx)
  2494. {
  2495. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2496. ff_thread_flush(avctx);
  2497. else if (avctx->codec->flush)
  2498. avctx->codec->flush(avctx);
  2499. avctx->pts_correction_last_pts =
  2500. avctx->pts_correction_last_dts = INT64_MIN;
  2501. if (!avctx->refcounted_frames)
  2502. av_frame_unref(&avctx->internal->to_free);
  2503. }
  2504. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2505. {
  2506. switch (codec_id) {
  2507. case AV_CODEC_ID_8SVX_EXP:
  2508. case AV_CODEC_ID_8SVX_FIB:
  2509. case AV_CODEC_ID_ADPCM_CT:
  2510. case AV_CODEC_ID_ADPCM_IMA_APC:
  2511. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2512. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2513. case AV_CODEC_ID_ADPCM_IMA_WS:
  2514. case AV_CODEC_ID_ADPCM_G722:
  2515. case AV_CODEC_ID_ADPCM_YAMAHA:
  2516. return 4;
  2517. case AV_CODEC_ID_PCM_ALAW:
  2518. case AV_CODEC_ID_PCM_MULAW:
  2519. case AV_CODEC_ID_PCM_S8:
  2520. case AV_CODEC_ID_PCM_S8_PLANAR:
  2521. case AV_CODEC_ID_PCM_U8:
  2522. case AV_CODEC_ID_PCM_ZORK:
  2523. return 8;
  2524. case AV_CODEC_ID_PCM_S16BE:
  2525. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2526. case AV_CODEC_ID_PCM_S16LE:
  2527. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2528. case AV_CODEC_ID_PCM_U16BE:
  2529. case AV_CODEC_ID_PCM_U16LE:
  2530. return 16;
  2531. case AV_CODEC_ID_PCM_S24DAUD:
  2532. case AV_CODEC_ID_PCM_S24BE:
  2533. case AV_CODEC_ID_PCM_S24LE:
  2534. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2535. case AV_CODEC_ID_PCM_U24BE:
  2536. case AV_CODEC_ID_PCM_U24LE:
  2537. return 24;
  2538. case AV_CODEC_ID_PCM_S32BE:
  2539. case AV_CODEC_ID_PCM_S32LE:
  2540. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2541. case AV_CODEC_ID_PCM_U32BE:
  2542. case AV_CODEC_ID_PCM_U32LE:
  2543. case AV_CODEC_ID_PCM_F32BE:
  2544. case AV_CODEC_ID_PCM_F32LE:
  2545. return 32;
  2546. case AV_CODEC_ID_PCM_F64BE:
  2547. case AV_CODEC_ID_PCM_F64LE:
  2548. return 64;
  2549. default:
  2550. return 0;
  2551. }
  2552. }
  2553. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2554. {
  2555. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2556. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2557. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2558. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2559. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2560. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2561. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2562. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2563. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2564. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2565. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2566. };
  2567. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2568. return AV_CODEC_ID_NONE;
  2569. if (be < 0 || be > 1)
  2570. be = AV_NE(1, 0);
  2571. return map[fmt][be];
  2572. }
  2573. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2574. {
  2575. switch (codec_id) {
  2576. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2577. return 2;
  2578. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2579. return 3;
  2580. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2581. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2582. case AV_CODEC_ID_ADPCM_IMA_QT:
  2583. case AV_CODEC_ID_ADPCM_SWF:
  2584. case AV_CODEC_ID_ADPCM_MS:
  2585. return 4;
  2586. default:
  2587. return av_get_exact_bits_per_sample(codec_id);
  2588. }
  2589. }
  2590. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2591. {
  2592. int id, sr, ch, ba, tag, bps;
  2593. id = avctx->codec_id;
  2594. sr = avctx->sample_rate;
  2595. ch = avctx->channels;
  2596. ba = avctx->block_align;
  2597. tag = avctx->codec_tag;
  2598. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2599. /* codecs with an exact constant bits per sample */
  2600. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2601. return (frame_bytes * 8LL) / (bps * ch);
  2602. bps = avctx->bits_per_coded_sample;
  2603. /* codecs with a fixed packet duration */
  2604. switch (id) {
  2605. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2606. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2607. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2608. case AV_CODEC_ID_AMR_NB:
  2609. case AV_CODEC_ID_EVRC:
  2610. case AV_CODEC_ID_GSM:
  2611. case AV_CODEC_ID_QCELP:
  2612. case AV_CODEC_ID_RA_288: return 160;
  2613. case AV_CODEC_ID_AMR_WB:
  2614. case AV_CODEC_ID_GSM_MS: return 320;
  2615. case AV_CODEC_ID_MP1: return 384;
  2616. case AV_CODEC_ID_ATRAC1: return 512;
  2617. case AV_CODEC_ID_ATRAC3: return 1024;
  2618. case AV_CODEC_ID_MP2:
  2619. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2620. case AV_CODEC_ID_AC3: return 1536;
  2621. }
  2622. if (sr > 0) {
  2623. /* calc from sample rate */
  2624. if (id == AV_CODEC_ID_TTA)
  2625. return 256 * sr / 245;
  2626. if (ch > 0) {
  2627. /* calc from sample rate and channels */
  2628. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2629. return (480 << (sr / 22050)) / ch;
  2630. }
  2631. }
  2632. if (ba > 0) {
  2633. /* calc from block_align */
  2634. if (id == AV_CODEC_ID_SIPR) {
  2635. switch (ba) {
  2636. case 20: return 160;
  2637. case 19: return 144;
  2638. case 29: return 288;
  2639. case 37: return 480;
  2640. }
  2641. } else if (id == AV_CODEC_ID_ILBC) {
  2642. switch (ba) {
  2643. case 38: return 160;
  2644. case 50: return 240;
  2645. }
  2646. }
  2647. }
  2648. if (frame_bytes > 0) {
  2649. /* calc from frame_bytes only */
  2650. if (id == AV_CODEC_ID_TRUESPEECH)
  2651. return 240 * (frame_bytes / 32);
  2652. if (id == AV_CODEC_ID_NELLYMOSER)
  2653. return 256 * (frame_bytes / 64);
  2654. if (id == AV_CODEC_ID_RA_144)
  2655. return 160 * (frame_bytes / 20);
  2656. if (id == AV_CODEC_ID_G723_1)
  2657. return 240 * (frame_bytes / 24);
  2658. if (bps > 0) {
  2659. /* calc from frame_bytes and bits_per_coded_sample */
  2660. if (id == AV_CODEC_ID_ADPCM_G726)
  2661. return frame_bytes * 8 / bps;
  2662. }
  2663. if (ch > 0) {
  2664. /* calc from frame_bytes and channels */
  2665. switch (id) {
  2666. case AV_CODEC_ID_ADPCM_AFC:
  2667. return frame_bytes / (9 * ch) * 16;
  2668. case AV_CODEC_ID_ADPCM_DTK:
  2669. return frame_bytes / (16 * ch) * 28;
  2670. case AV_CODEC_ID_ADPCM_4XM:
  2671. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2672. return (frame_bytes - 4 * ch) * 2 / ch;
  2673. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2674. return (frame_bytes - 4) * 2 / ch;
  2675. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2676. return (frame_bytes - 8) * 2 / ch;
  2677. case AV_CODEC_ID_ADPCM_XA:
  2678. return (frame_bytes / 128) * 224 / ch;
  2679. case AV_CODEC_ID_INTERPLAY_DPCM:
  2680. return (frame_bytes - 6 - ch) / ch;
  2681. case AV_CODEC_ID_ROQ_DPCM:
  2682. return (frame_bytes - 8) / ch;
  2683. case AV_CODEC_ID_XAN_DPCM:
  2684. return (frame_bytes - 2 * ch) / ch;
  2685. case AV_CODEC_ID_MACE3:
  2686. return 3 * frame_bytes / ch;
  2687. case AV_CODEC_ID_MACE6:
  2688. return 6 * frame_bytes / ch;
  2689. case AV_CODEC_ID_PCM_LXF:
  2690. return 2 * (frame_bytes / (5 * ch));
  2691. case AV_CODEC_ID_IAC:
  2692. case AV_CODEC_ID_IMC:
  2693. return 4 * frame_bytes / ch;
  2694. }
  2695. if (tag) {
  2696. /* calc from frame_bytes, channels, and codec_tag */
  2697. if (id == AV_CODEC_ID_SOL_DPCM) {
  2698. if (tag == 3)
  2699. return frame_bytes / ch;
  2700. else
  2701. return frame_bytes * 2 / ch;
  2702. }
  2703. }
  2704. if (ba > 0) {
  2705. /* calc from frame_bytes, channels, and block_align */
  2706. int blocks = frame_bytes / ba;
  2707. switch (avctx->codec_id) {
  2708. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2709. if (bps < 2 || bps > 5)
  2710. return 0;
  2711. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2712. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2713. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2714. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2715. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2716. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2717. return blocks * ((ba - 4 * ch) * 2 / ch);
  2718. case AV_CODEC_ID_ADPCM_MS:
  2719. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2720. }
  2721. }
  2722. if (bps > 0) {
  2723. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2724. switch (avctx->codec_id) {
  2725. case AV_CODEC_ID_PCM_DVD:
  2726. if(bps<4)
  2727. return 0;
  2728. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2729. case AV_CODEC_ID_PCM_BLURAY:
  2730. if(bps<4)
  2731. return 0;
  2732. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2733. case AV_CODEC_ID_S302M:
  2734. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2735. }
  2736. }
  2737. }
  2738. }
  2739. return 0;
  2740. }
  2741. #if !HAVE_THREADS
  2742. int ff_thread_init(AVCodecContext *s)
  2743. {
  2744. return -1;
  2745. }
  2746. #endif
  2747. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2748. {
  2749. unsigned int n = 0;
  2750. while (v >= 0xff) {
  2751. *s++ = 0xff;
  2752. v -= 0xff;
  2753. n++;
  2754. }
  2755. *s = v;
  2756. n++;
  2757. return n;
  2758. }
  2759. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2760. {
  2761. int i;
  2762. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2763. return i;
  2764. }
  2765. #if FF_API_MISSING_SAMPLE
  2766. FF_DISABLE_DEPRECATION_WARNINGS
  2767. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2768. {
  2769. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2770. "version to the newest one from Git. If the problem still "
  2771. "occurs, it means that your file has a feature which has not "
  2772. "been implemented.\n", feature);
  2773. if(want_sample)
  2774. av_log_ask_for_sample(avc, NULL);
  2775. }
  2776. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2777. {
  2778. va_list argument_list;
  2779. va_start(argument_list, msg);
  2780. if (msg)
  2781. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2782. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2783. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2784. "and contact the ffmpeg-devel mailing list.\n");
  2785. va_end(argument_list);
  2786. }
  2787. FF_ENABLE_DEPRECATION_WARNINGS
  2788. #endif /* FF_API_MISSING_SAMPLE */
  2789. static AVHWAccel *first_hwaccel = NULL;
  2790. void av_register_hwaccel(AVHWAccel *hwaccel)
  2791. {
  2792. AVHWAccel **p = &first_hwaccel;
  2793. hwaccel->next = NULL;
  2794. while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  2795. p = &(*p)->next;
  2796. }
  2797. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2798. {
  2799. return hwaccel ? hwaccel->next : first_hwaccel;
  2800. }
  2801. AVHWAccel *ff_find_hwaccel(AVCodecContext *avctx)
  2802. {
  2803. enum AVCodecID codec_id = avctx->codec->id;
  2804. enum AVPixelFormat pix_fmt = avctx->pix_fmt;
  2805. AVHWAccel *hwaccel = NULL;
  2806. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2807. if (hwaccel->id == codec_id
  2808. && hwaccel->pix_fmt == pix_fmt)
  2809. return hwaccel;
  2810. return NULL;
  2811. }
  2812. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2813. {
  2814. if (lockmgr_cb) {
  2815. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2816. return -1;
  2817. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2818. return -1;
  2819. }
  2820. lockmgr_cb = cb;
  2821. if (lockmgr_cb) {
  2822. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2823. return -1;
  2824. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2825. return -1;
  2826. }
  2827. return 0;
  2828. }
  2829. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2830. {
  2831. if (lockmgr_cb) {
  2832. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2833. return -1;
  2834. }
  2835. entangled_thread_counter++;
  2836. if (entangled_thread_counter != 1) {
  2837. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2838. if (!lockmgr_cb)
  2839. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  2840. ff_avcodec_locked = 1;
  2841. ff_unlock_avcodec();
  2842. return AVERROR(EINVAL);
  2843. }
  2844. av_assert0(!ff_avcodec_locked);
  2845. ff_avcodec_locked = 1;
  2846. return 0;
  2847. }
  2848. int ff_unlock_avcodec(void)
  2849. {
  2850. av_assert0(ff_avcodec_locked);
  2851. ff_avcodec_locked = 0;
  2852. entangled_thread_counter--;
  2853. if (lockmgr_cb) {
  2854. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2855. return -1;
  2856. }
  2857. return 0;
  2858. }
  2859. int avpriv_lock_avformat(void)
  2860. {
  2861. if (lockmgr_cb) {
  2862. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2863. return -1;
  2864. }
  2865. return 0;
  2866. }
  2867. int avpriv_unlock_avformat(void)
  2868. {
  2869. if (lockmgr_cb) {
  2870. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2871. return -1;
  2872. }
  2873. return 0;
  2874. }
  2875. unsigned int avpriv_toupper4(unsigned int x)
  2876. {
  2877. return av_toupper(x & 0xFF) +
  2878. (av_toupper((x >> 8) & 0xFF) << 8) +
  2879. (av_toupper((x >> 16) & 0xFF) << 16) +
  2880. (av_toupper((x >> 24) & 0xFF) << 24);
  2881. }
  2882. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2883. {
  2884. int ret;
  2885. dst->owner = src->owner;
  2886. ret = av_frame_ref(dst->f, src->f);
  2887. if (ret < 0)
  2888. return ret;
  2889. if (src->progress &&
  2890. !(dst->progress = av_buffer_ref(src->progress))) {
  2891. ff_thread_release_buffer(dst->owner, dst);
  2892. return AVERROR(ENOMEM);
  2893. }
  2894. return 0;
  2895. }
  2896. #if !HAVE_THREADS
  2897. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2898. {
  2899. return avctx->get_format(avctx, fmt);
  2900. }
  2901. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2902. {
  2903. f->owner = avctx;
  2904. return ff_get_buffer(avctx, f->f, flags);
  2905. }
  2906. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2907. {
  2908. av_frame_unref(f->f);
  2909. }
  2910. void ff_thread_finish_setup(AVCodecContext *avctx)
  2911. {
  2912. }
  2913. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2914. {
  2915. }
  2916. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2917. {
  2918. }
  2919. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2920. {
  2921. return 1;
  2922. }
  2923. int ff_alloc_entries(AVCodecContext *avctx, int count)
  2924. {
  2925. return 0;
  2926. }
  2927. void ff_reset_entries(AVCodecContext *avctx)
  2928. {
  2929. }
  2930. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  2931. {
  2932. }
  2933. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  2934. {
  2935. }
  2936. #endif
  2937. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2938. {
  2939. AVCodec *c= avcodec_find_decoder(codec_id);
  2940. if(!c)
  2941. c= avcodec_find_encoder(codec_id);
  2942. if(c)
  2943. return c->type;
  2944. if (codec_id <= AV_CODEC_ID_NONE)
  2945. return AVMEDIA_TYPE_UNKNOWN;
  2946. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2947. return AVMEDIA_TYPE_VIDEO;
  2948. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2949. return AVMEDIA_TYPE_AUDIO;
  2950. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2951. return AVMEDIA_TYPE_SUBTITLE;
  2952. return AVMEDIA_TYPE_UNKNOWN;
  2953. }
  2954. int avcodec_is_open(AVCodecContext *s)
  2955. {
  2956. return !!s->internal;
  2957. }
  2958. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2959. {
  2960. int ret;
  2961. char *str;
  2962. ret = av_bprint_finalize(buf, &str);
  2963. if (ret < 0)
  2964. return ret;
  2965. avctx->extradata = str;
  2966. /* Note: the string is NUL terminated (so extradata can be read as a
  2967. * string), but the ending character is not accounted in the size (in
  2968. * binary formats you are likely not supposed to mux that character). When
  2969. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2970. * zeros. */
  2971. avctx->extradata_size = buf->len;
  2972. return 0;
  2973. }
  2974. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  2975. const uint8_t *end,
  2976. uint32_t *av_restrict state)
  2977. {
  2978. int i;
  2979. av_assert0(p <= end);
  2980. if (p >= end)
  2981. return end;
  2982. for (i = 0; i < 3; i++) {
  2983. uint32_t tmp = *state << 8;
  2984. *state = tmp + *(p++);
  2985. if (tmp == 0x100 || p == end)
  2986. return p;
  2987. }
  2988. while (p < end) {
  2989. if (p[-1] > 1 ) p += 3;
  2990. else if (p[-2] ) p += 2;
  2991. else if (p[-3]|(p[-1]-1)) p++;
  2992. else {
  2993. p++;
  2994. break;
  2995. }
  2996. }
  2997. p = FFMIN(p, end) - 4;
  2998. *state = AV_RB32(p);
  2999. return p + 4;
  3000. }