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.

3843 lines
127KB

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