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.

3802 lines
125KB

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