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.

3718 lines
122KB

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