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.

3421 lines
111KB

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