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.

3826 lines
126KB

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