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.

3713 lines
122KB

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