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.

3829 lines
126KB

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