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.

3449 lines
112KB

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