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.

3753 lines
123KB

  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. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  1324. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  1325. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  1326. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  1327. avctx->color_range = AVCOL_RANGE_JPEG;
  1328. }
  1329. if (avctx->codec->supported_samplerates) {
  1330. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1331. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1332. break;
  1333. if (avctx->codec->supported_samplerates[i] == 0) {
  1334. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1335. avctx->sample_rate);
  1336. ret = AVERROR(EINVAL);
  1337. goto free_and_end;
  1338. }
  1339. }
  1340. if (avctx->codec->channel_layouts) {
  1341. if (!avctx->channel_layout) {
  1342. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1343. } else {
  1344. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1345. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1346. break;
  1347. if (avctx->codec->channel_layouts[i] == 0) {
  1348. char buf[512];
  1349. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1350. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1351. ret = AVERROR(EINVAL);
  1352. goto free_and_end;
  1353. }
  1354. }
  1355. }
  1356. if (avctx->channel_layout && avctx->channels) {
  1357. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1358. if (channels != avctx->channels) {
  1359. char buf[512];
  1360. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1361. av_log(avctx, AV_LOG_ERROR,
  1362. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1363. buf, channels, avctx->channels);
  1364. ret = AVERROR(EINVAL);
  1365. goto free_and_end;
  1366. }
  1367. } else if (avctx->channel_layout) {
  1368. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1369. }
  1370. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1371. if (avctx->width <= 0 || avctx->height <= 0) {
  1372. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1373. ret = AVERROR(EINVAL);
  1374. goto free_and_end;
  1375. }
  1376. }
  1377. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1378. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1379. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1380. }
  1381. if (!avctx->rc_initial_buffer_occupancy)
  1382. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1383. }
  1384. avctx->pts_correction_num_faulty_pts =
  1385. avctx->pts_correction_num_faulty_dts = 0;
  1386. avctx->pts_correction_last_pts =
  1387. avctx->pts_correction_last_dts = INT64_MIN;
  1388. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1389. || avctx->internal->frame_thread_encoder)) {
  1390. ret = avctx->codec->init(avctx);
  1391. if (ret < 0) {
  1392. goto free_and_end;
  1393. }
  1394. }
  1395. ret=0;
  1396. if (av_codec_is_decoder(avctx->codec)) {
  1397. if (!avctx->bit_rate)
  1398. avctx->bit_rate = get_bit_rate(avctx);
  1399. /* validate channel layout from the decoder */
  1400. if (avctx->channel_layout) {
  1401. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1402. if (!avctx->channels)
  1403. avctx->channels = channels;
  1404. else if (channels != avctx->channels) {
  1405. char buf[512];
  1406. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1407. av_log(avctx, AV_LOG_WARNING,
  1408. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1409. "ignoring specified channel layout\n",
  1410. buf, channels, avctx->channels);
  1411. avctx->channel_layout = 0;
  1412. }
  1413. }
  1414. if (avctx->channels && avctx->channels < 0 ||
  1415. avctx->channels > FF_SANE_NB_CHANNELS) {
  1416. ret = AVERROR(EINVAL);
  1417. goto free_and_end;
  1418. }
  1419. if (avctx->sub_charenc) {
  1420. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1421. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1422. "supported with subtitles codecs\n");
  1423. ret = AVERROR(EINVAL);
  1424. goto free_and_end;
  1425. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1426. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1427. "subtitles character encoding will be ignored\n",
  1428. avctx->codec_descriptor->name);
  1429. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1430. } else {
  1431. /* input character encoding is set for a text based subtitle
  1432. * codec at this point */
  1433. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1434. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1435. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1436. #if CONFIG_ICONV
  1437. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1438. if (cd == (iconv_t)-1) {
  1439. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1440. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1441. ret = AVERROR(errno);
  1442. goto free_and_end;
  1443. }
  1444. iconv_close(cd);
  1445. #else
  1446. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1447. "conversion needs a libavcodec built with iconv support "
  1448. "for this codec\n");
  1449. ret = AVERROR(ENOSYS);
  1450. goto free_and_end;
  1451. #endif
  1452. }
  1453. }
  1454. }
  1455. }
  1456. end:
  1457. ff_unlock_avcodec();
  1458. if (options) {
  1459. av_dict_free(options);
  1460. *options = tmp;
  1461. }
  1462. return ret;
  1463. free_and_end:
  1464. av_dict_free(&tmp);
  1465. av_freep(&avctx->priv_data);
  1466. if (avctx->internal) {
  1467. av_frame_free(&avctx->internal->to_free);
  1468. av_freep(&avctx->internal->pool);
  1469. }
  1470. av_freep(&avctx->internal);
  1471. avctx->codec = NULL;
  1472. goto end;
  1473. }
  1474. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1475. {
  1476. if (avpkt->size < 0) {
  1477. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1478. return AVERROR(EINVAL);
  1479. }
  1480. if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1481. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1482. size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1483. return AVERROR(EINVAL);
  1484. }
  1485. if (avctx) {
  1486. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1487. if (!avpkt->data || avpkt->size < size) {
  1488. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1489. avpkt->data = avctx->internal->byte_buffer;
  1490. avpkt->size = avctx->internal->byte_buffer_size;
  1491. #if FF_API_DESTRUCT_PACKET
  1492. FF_DISABLE_DEPRECATION_WARNINGS
  1493. avpkt->destruct = NULL;
  1494. FF_ENABLE_DEPRECATION_WARNINGS
  1495. #endif
  1496. }
  1497. }
  1498. if (avpkt->data) {
  1499. AVBufferRef *buf = avpkt->buf;
  1500. #if FF_API_DESTRUCT_PACKET
  1501. FF_DISABLE_DEPRECATION_WARNINGS
  1502. void *destruct = avpkt->destruct;
  1503. FF_ENABLE_DEPRECATION_WARNINGS
  1504. #endif
  1505. if (avpkt->size < size) {
  1506. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1507. return AVERROR(EINVAL);
  1508. }
  1509. av_init_packet(avpkt);
  1510. #if FF_API_DESTRUCT_PACKET
  1511. FF_DISABLE_DEPRECATION_WARNINGS
  1512. avpkt->destruct = destruct;
  1513. FF_ENABLE_DEPRECATION_WARNINGS
  1514. #endif
  1515. avpkt->buf = buf;
  1516. avpkt->size = size;
  1517. return 0;
  1518. } else {
  1519. int ret = av_new_packet(avpkt, size);
  1520. if (ret < 0)
  1521. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1522. return ret;
  1523. }
  1524. }
  1525. int ff_alloc_packet(AVPacket *avpkt, int size)
  1526. {
  1527. return ff_alloc_packet2(NULL, avpkt, size);
  1528. }
  1529. /**
  1530. * Pad last frame with silence.
  1531. */
  1532. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1533. {
  1534. AVFrame *frame = NULL;
  1535. int ret;
  1536. if (!(frame = av_frame_alloc()))
  1537. return AVERROR(ENOMEM);
  1538. frame->format = src->format;
  1539. frame->channel_layout = src->channel_layout;
  1540. av_frame_set_channels(frame, av_frame_get_channels(src));
  1541. frame->nb_samples = s->frame_size;
  1542. ret = av_frame_get_buffer(frame, 32);
  1543. if (ret < 0)
  1544. goto fail;
  1545. ret = av_frame_copy_props(frame, src);
  1546. if (ret < 0)
  1547. goto fail;
  1548. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1549. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1550. goto fail;
  1551. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1552. frame->nb_samples - src->nb_samples,
  1553. s->channels, s->sample_fmt)) < 0)
  1554. goto fail;
  1555. *dst = frame;
  1556. return 0;
  1557. fail:
  1558. av_frame_free(&frame);
  1559. return ret;
  1560. }
  1561. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1562. AVPacket *avpkt,
  1563. const AVFrame *frame,
  1564. int *got_packet_ptr)
  1565. {
  1566. AVFrame *extended_frame = NULL;
  1567. AVFrame *padded_frame = NULL;
  1568. int ret;
  1569. AVPacket user_pkt = *avpkt;
  1570. int needs_realloc = !user_pkt.data;
  1571. *got_packet_ptr = 0;
  1572. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1573. av_free_packet(avpkt);
  1574. av_init_packet(avpkt);
  1575. return 0;
  1576. }
  1577. /* ensure that extended_data is properly set */
  1578. if (frame && !frame->extended_data) {
  1579. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1580. avctx->channels > AV_NUM_DATA_POINTERS) {
  1581. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1582. "with more than %d channels, but extended_data is not set.\n",
  1583. AV_NUM_DATA_POINTERS);
  1584. return AVERROR(EINVAL);
  1585. }
  1586. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1587. extended_frame = av_frame_alloc();
  1588. if (!extended_frame)
  1589. return AVERROR(ENOMEM);
  1590. memcpy(extended_frame, frame, sizeof(AVFrame));
  1591. extended_frame->extended_data = extended_frame->data;
  1592. frame = extended_frame;
  1593. }
  1594. /* check for valid frame size */
  1595. if (frame) {
  1596. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1597. if (frame->nb_samples > avctx->frame_size) {
  1598. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1599. ret = AVERROR(EINVAL);
  1600. goto end;
  1601. }
  1602. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1603. if (frame->nb_samples < avctx->frame_size &&
  1604. !avctx->internal->last_audio_frame) {
  1605. ret = pad_last_frame(avctx, &padded_frame, frame);
  1606. if (ret < 0)
  1607. goto end;
  1608. frame = padded_frame;
  1609. avctx->internal->last_audio_frame = 1;
  1610. }
  1611. if (frame->nb_samples != avctx->frame_size) {
  1612. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1613. ret = AVERROR(EINVAL);
  1614. goto end;
  1615. }
  1616. }
  1617. }
  1618. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1619. if (!ret) {
  1620. if (*got_packet_ptr) {
  1621. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1622. if (avpkt->pts == AV_NOPTS_VALUE)
  1623. avpkt->pts = frame->pts;
  1624. if (!avpkt->duration)
  1625. avpkt->duration = ff_samples_to_time_base(avctx,
  1626. frame->nb_samples);
  1627. }
  1628. avpkt->dts = avpkt->pts;
  1629. } else {
  1630. avpkt->size = 0;
  1631. }
  1632. }
  1633. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1634. needs_realloc = 0;
  1635. if (user_pkt.data) {
  1636. if (user_pkt.size >= avpkt->size) {
  1637. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1638. } else {
  1639. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1640. avpkt->size = user_pkt.size;
  1641. ret = -1;
  1642. }
  1643. avpkt->buf = user_pkt.buf;
  1644. avpkt->data = user_pkt.data;
  1645. #if FF_API_DESTRUCT_PACKET
  1646. FF_DISABLE_DEPRECATION_WARNINGS
  1647. avpkt->destruct = user_pkt.destruct;
  1648. FF_ENABLE_DEPRECATION_WARNINGS
  1649. #endif
  1650. } else {
  1651. if (av_dup_packet(avpkt) < 0) {
  1652. ret = AVERROR(ENOMEM);
  1653. }
  1654. }
  1655. }
  1656. if (!ret) {
  1657. if (needs_realloc && avpkt->data) {
  1658. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1659. if (ret >= 0)
  1660. avpkt->data = avpkt->buf->data;
  1661. }
  1662. avctx->frame_number++;
  1663. }
  1664. if (ret < 0 || !*got_packet_ptr) {
  1665. av_free_packet(avpkt);
  1666. av_init_packet(avpkt);
  1667. goto end;
  1668. }
  1669. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1670. * this needs to be moved to the encoders, but for now we can do it
  1671. * here to simplify things */
  1672. avpkt->flags |= AV_PKT_FLAG_KEY;
  1673. end:
  1674. av_frame_free(&padded_frame);
  1675. av_free(extended_frame);
  1676. return ret;
  1677. }
  1678. #if FF_API_OLD_ENCODE_AUDIO
  1679. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1680. uint8_t *buf, int buf_size,
  1681. const short *samples)
  1682. {
  1683. AVPacket pkt;
  1684. AVFrame *frame;
  1685. int ret, samples_size, got_packet;
  1686. av_init_packet(&pkt);
  1687. pkt.data = buf;
  1688. pkt.size = buf_size;
  1689. if (samples) {
  1690. frame = av_frame_alloc();
  1691. if (!frame)
  1692. return AVERROR(ENOMEM);
  1693. if (avctx->frame_size) {
  1694. frame->nb_samples = avctx->frame_size;
  1695. } else {
  1696. /* if frame_size is not set, the number of samples must be
  1697. * calculated from the buffer size */
  1698. int64_t nb_samples;
  1699. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1700. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1701. "support this codec\n");
  1702. av_frame_free(&frame);
  1703. return AVERROR(EINVAL);
  1704. }
  1705. nb_samples = (int64_t)buf_size * 8 /
  1706. (av_get_bits_per_sample(avctx->codec_id) *
  1707. avctx->channels);
  1708. if (nb_samples >= INT_MAX) {
  1709. av_frame_free(&frame);
  1710. return AVERROR(EINVAL);
  1711. }
  1712. frame->nb_samples = nb_samples;
  1713. }
  1714. /* it is assumed that the samples buffer is large enough based on the
  1715. * relevant parameters */
  1716. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1717. frame->nb_samples,
  1718. avctx->sample_fmt, 1);
  1719. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1720. avctx->sample_fmt,
  1721. (const uint8_t *)samples,
  1722. samples_size, 1)) < 0) {
  1723. av_frame_free(&frame);
  1724. return ret;
  1725. }
  1726. /* fabricate frame pts from sample count.
  1727. * this is needed because the avcodec_encode_audio() API does not have
  1728. * a way for the user to provide pts */
  1729. if (avctx->sample_rate && avctx->time_base.num)
  1730. frame->pts = ff_samples_to_time_base(avctx,
  1731. avctx->internal->sample_count);
  1732. else
  1733. frame->pts = AV_NOPTS_VALUE;
  1734. avctx->internal->sample_count += frame->nb_samples;
  1735. } else {
  1736. frame = NULL;
  1737. }
  1738. got_packet = 0;
  1739. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1740. if (!ret && got_packet && avctx->coded_frame) {
  1741. avctx->coded_frame->pts = pkt.pts;
  1742. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1743. }
  1744. /* free any side data since we cannot return it */
  1745. av_packet_free_side_data(&pkt);
  1746. if (frame && frame->extended_data != frame->data)
  1747. av_freep(&frame->extended_data);
  1748. av_frame_free(&frame);
  1749. return ret ? ret : pkt.size;
  1750. }
  1751. #endif
  1752. #if FF_API_OLD_ENCODE_VIDEO
  1753. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1754. const AVFrame *pict)
  1755. {
  1756. AVPacket pkt;
  1757. int ret, got_packet = 0;
  1758. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1759. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1760. return -1;
  1761. }
  1762. av_init_packet(&pkt);
  1763. pkt.data = buf;
  1764. pkt.size = buf_size;
  1765. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1766. if (!ret && got_packet && avctx->coded_frame) {
  1767. avctx->coded_frame->pts = pkt.pts;
  1768. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1769. }
  1770. /* free any side data since we cannot return it */
  1771. if (pkt.side_data_elems > 0) {
  1772. int i;
  1773. for (i = 0; i < pkt.side_data_elems; i++)
  1774. av_free(pkt.side_data[i].data);
  1775. av_freep(&pkt.side_data);
  1776. pkt.side_data_elems = 0;
  1777. }
  1778. return ret ? ret : pkt.size;
  1779. }
  1780. #endif
  1781. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1782. AVPacket *avpkt,
  1783. const AVFrame *frame,
  1784. int *got_packet_ptr)
  1785. {
  1786. int ret;
  1787. AVPacket user_pkt = *avpkt;
  1788. int needs_realloc = !user_pkt.data;
  1789. *got_packet_ptr = 0;
  1790. if(CONFIG_FRAME_THREAD_ENCODER &&
  1791. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1792. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1793. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1794. avctx->stats_out[0] = '\0';
  1795. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1796. av_free_packet(avpkt);
  1797. av_init_packet(avpkt);
  1798. avpkt->size = 0;
  1799. return 0;
  1800. }
  1801. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1802. return AVERROR(EINVAL);
  1803. av_assert0(avctx->codec->encode2);
  1804. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1805. av_assert0(ret <= 0);
  1806. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1807. needs_realloc = 0;
  1808. if (user_pkt.data) {
  1809. if (user_pkt.size >= avpkt->size) {
  1810. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1811. } else {
  1812. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1813. avpkt->size = user_pkt.size;
  1814. ret = -1;
  1815. }
  1816. avpkt->buf = user_pkt.buf;
  1817. avpkt->data = user_pkt.data;
  1818. #if FF_API_DESTRUCT_PACKET
  1819. FF_DISABLE_DEPRECATION_WARNINGS
  1820. avpkt->destruct = user_pkt.destruct;
  1821. FF_ENABLE_DEPRECATION_WARNINGS
  1822. #endif
  1823. } else {
  1824. if (av_dup_packet(avpkt) < 0) {
  1825. ret = AVERROR(ENOMEM);
  1826. }
  1827. }
  1828. }
  1829. if (!ret) {
  1830. if (!*got_packet_ptr)
  1831. avpkt->size = 0;
  1832. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1833. avpkt->pts = avpkt->dts = frame->pts;
  1834. if (needs_realloc && avpkt->data) {
  1835. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1836. if (ret >= 0)
  1837. avpkt->data = avpkt->buf->data;
  1838. }
  1839. avctx->frame_number++;
  1840. }
  1841. if (ret < 0 || !*got_packet_ptr)
  1842. av_free_packet(avpkt);
  1843. else
  1844. av_packet_merge_side_data(avpkt);
  1845. emms_c();
  1846. return ret;
  1847. }
  1848. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1849. const AVSubtitle *sub)
  1850. {
  1851. int ret;
  1852. if (sub->start_display_time) {
  1853. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1854. return -1;
  1855. }
  1856. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1857. avctx->frame_number++;
  1858. return ret;
  1859. }
  1860. /**
  1861. * Attempt to guess proper monotonic timestamps for decoded video frames
  1862. * which might have incorrect times. Input timestamps may wrap around, in
  1863. * which case the output will as well.
  1864. *
  1865. * @param pts the pts field of the decoded AVPacket, as passed through
  1866. * AVFrame.pkt_pts
  1867. * @param dts the dts field of the decoded AVPacket
  1868. * @return one of the input values, may be AV_NOPTS_VALUE
  1869. */
  1870. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1871. int64_t reordered_pts, int64_t dts)
  1872. {
  1873. int64_t pts = AV_NOPTS_VALUE;
  1874. if (dts != AV_NOPTS_VALUE) {
  1875. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1876. ctx->pts_correction_last_dts = dts;
  1877. } else if (reordered_pts != AV_NOPTS_VALUE)
  1878. ctx->pts_correction_last_dts = reordered_pts;
  1879. if (reordered_pts != AV_NOPTS_VALUE) {
  1880. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1881. ctx->pts_correction_last_pts = reordered_pts;
  1882. } else if(dts != AV_NOPTS_VALUE)
  1883. ctx->pts_correction_last_pts = dts;
  1884. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1885. && reordered_pts != AV_NOPTS_VALUE)
  1886. pts = reordered_pts;
  1887. else
  1888. pts = dts;
  1889. return pts;
  1890. }
  1891. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1892. {
  1893. int size = 0, ret;
  1894. const uint8_t *data;
  1895. uint32_t flags;
  1896. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1897. if (!data)
  1898. return 0;
  1899. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
  1900. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1901. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1902. return AVERROR(EINVAL);
  1903. }
  1904. if (size < 4)
  1905. goto fail;
  1906. flags = bytestream_get_le32(&data);
  1907. size -= 4;
  1908. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1909. if (size < 4)
  1910. goto fail;
  1911. avctx->channels = bytestream_get_le32(&data);
  1912. size -= 4;
  1913. }
  1914. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1915. if (size < 8)
  1916. goto fail;
  1917. avctx->channel_layout = bytestream_get_le64(&data);
  1918. size -= 8;
  1919. }
  1920. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1921. if (size < 4)
  1922. goto fail;
  1923. avctx->sample_rate = bytestream_get_le32(&data);
  1924. size -= 4;
  1925. }
  1926. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1927. if (size < 8)
  1928. goto fail;
  1929. avctx->width = bytestream_get_le32(&data);
  1930. avctx->height = bytestream_get_le32(&data);
  1931. size -= 8;
  1932. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1933. if (ret < 0)
  1934. return ret;
  1935. }
  1936. return 0;
  1937. fail:
  1938. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1939. return AVERROR_INVALIDDATA;
  1940. }
  1941. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1942. {
  1943. int size;
  1944. const uint8_t *side_metadata;
  1945. AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
  1946. side_metadata = av_packet_get_side_data(avctx->internal->pkt,
  1947. AV_PKT_DATA_STRINGS_METADATA, &size);
  1948. return av_packet_unpack_dictionary(side_metadata, size, frame_md);
  1949. }
  1950. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1951. {
  1952. int ret;
  1953. /* move the original frame to our backup */
  1954. av_frame_unref(avci->to_free);
  1955. av_frame_move_ref(avci->to_free, frame);
  1956. /* now copy everything except the AVBufferRefs back
  1957. * note that we make a COPY of the side data, so calling av_frame_free() on
  1958. * the caller's frame will work properly */
  1959. ret = av_frame_copy_props(frame, avci->to_free);
  1960. if (ret < 0)
  1961. return ret;
  1962. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1963. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1964. if (avci->to_free->extended_data != avci->to_free->data) {
  1965. int planes = av_frame_get_channels(avci->to_free);
  1966. int size = planes * sizeof(*frame->extended_data);
  1967. if (!size) {
  1968. av_frame_unref(frame);
  1969. return AVERROR_BUG;
  1970. }
  1971. frame->extended_data = av_malloc(size);
  1972. if (!frame->extended_data) {
  1973. av_frame_unref(frame);
  1974. return AVERROR(ENOMEM);
  1975. }
  1976. memcpy(frame->extended_data, avci->to_free->extended_data,
  1977. size);
  1978. } else
  1979. frame->extended_data = frame->data;
  1980. frame->format = avci->to_free->format;
  1981. frame->width = avci->to_free->width;
  1982. frame->height = avci->to_free->height;
  1983. frame->channel_layout = avci->to_free->channel_layout;
  1984. frame->nb_samples = avci->to_free->nb_samples;
  1985. av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
  1986. return 0;
  1987. }
  1988. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1989. int *got_picture_ptr,
  1990. const AVPacket *avpkt)
  1991. {
  1992. AVCodecInternal *avci = avctx->internal;
  1993. int ret;
  1994. // copy to ensure we do not change avpkt
  1995. AVPacket tmp = *avpkt;
  1996. if (!avctx->codec)
  1997. return AVERROR(EINVAL);
  1998. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1999. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  2000. return AVERROR(EINVAL);
  2001. }
  2002. *got_picture_ptr = 0;
  2003. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  2004. return AVERROR(EINVAL);
  2005. av_frame_unref(picture);
  2006. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2007. int did_split = av_packet_split_side_data(&tmp);
  2008. ret = apply_param_change(avctx, &tmp);
  2009. if (ret < 0) {
  2010. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2011. if (avctx->err_recognition & AV_EF_EXPLODE)
  2012. goto fail;
  2013. }
  2014. avctx->internal->pkt = &tmp;
  2015. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2016. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  2017. &tmp);
  2018. else {
  2019. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  2020. &tmp);
  2021. picture->pkt_dts = avpkt->dts;
  2022. if(!avctx->has_b_frames){
  2023. av_frame_set_pkt_pos(picture, avpkt->pos);
  2024. }
  2025. //FIXME these should be under if(!avctx->has_b_frames)
  2026. /* get_buffer is supposed to set frame parameters */
  2027. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  2028. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  2029. if (!picture->width) picture->width = avctx->width;
  2030. if (!picture->height) picture->height = avctx->height;
  2031. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  2032. }
  2033. }
  2034. add_metadata_from_side_data(avctx, picture);
  2035. fail:
  2036. emms_c(); //needed to avoid an emms_c() call before every return;
  2037. avctx->internal->pkt = NULL;
  2038. if (did_split) {
  2039. av_packet_free_side_data(&tmp);
  2040. if(ret == tmp.size)
  2041. ret = avpkt->size;
  2042. }
  2043. if (*got_picture_ptr) {
  2044. if (!avctx->refcounted_frames) {
  2045. int err = unrefcount_frame(avci, picture);
  2046. if (err < 0)
  2047. return err;
  2048. }
  2049. avctx->frame_number++;
  2050. av_frame_set_best_effort_timestamp(picture,
  2051. guess_correct_pts(avctx,
  2052. picture->pkt_pts,
  2053. picture->pkt_dts));
  2054. } else
  2055. av_frame_unref(picture);
  2056. } else
  2057. ret = 0;
  2058. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  2059. * make sure it's set correctly */
  2060. av_assert0(!picture->extended_data || picture->extended_data == picture->data);
  2061. return ret;
  2062. }
  2063. #if FF_API_OLD_DECODE_AUDIO
  2064. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  2065. int *frame_size_ptr,
  2066. AVPacket *avpkt)
  2067. {
  2068. AVFrame *frame = av_frame_alloc();
  2069. int ret, got_frame = 0;
  2070. if (!frame)
  2071. return AVERROR(ENOMEM);
  2072. if (avctx->get_buffer != avcodec_default_get_buffer) {
  2073. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  2074. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  2075. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  2076. "avcodec_decode_audio4()\n");
  2077. avctx->get_buffer = avcodec_default_get_buffer;
  2078. avctx->release_buffer = avcodec_default_release_buffer;
  2079. }
  2080. ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
  2081. if (ret >= 0 && got_frame) {
  2082. int ch, plane_size;
  2083. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  2084. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  2085. frame->nb_samples,
  2086. avctx->sample_fmt, 1);
  2087. if (*frame_size_ptr < data_size) {
  2088. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  2089. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  2090. av_frame_free(&frame);
  2091. return AVERROR(EINVAL);
  2092. }
  2093. memcpy(samples, frame->extended_data[0], plane_size);
  2094. if (planar && avctx->channels > 1) {
  2095. uint8_t *out = ((uint8_t *)samples) + plane_size;
  2096. for (ch = 1; ch < avctx->channels; ch++) {
  2097. memcpy(out, frame->extended_data[ch], plane_size);
  2098. out += plane_size;
  2099. }
  2100. }
  2101. *frame_size_ptr = data_size;
  2102. } else {
  2103. *frame_size_ptr = 0;
  2104. }
  2105. av_frame_free(&frame);
  2106. return ret;
  2107. }
  2108. #endif
  2109. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  2110. AVFrame *frame,
  2111. int *got_frame_ptr,
  2112. const AVPacket *avpkt)
  2113. {
  2114. AVCodecInternal *avci = avctx->internal;
  2115. int ret = 0;
  2116. *got_frame_ptr = 0;
  2117. if (!avpkt->data && avpkt->size) {
  2118. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2119. return AVERROR(EINVAL);
  2120. }
  2121. if (!avctx->codec)
  2122. return AVERROR(EINVAL);
  2123. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  2124. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  2125. return AVERROR(EINVAL);
  2126. }
  2127. av_frame_unref(frame);
  2128. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  2129. uint8_t *side;
  2130. int side_size;
  2131. uint32_t discard_padding = 0;
  2132. uint8_t skip_reason = 0;
  2133. uint8_t discard_reason = 0;
  2134. // copy to ensure we do not change avpkt
  2135. AVPacket tmp = *avpkt;
  2136. int did_split = av_packet_split_side_data(&tmp);
  2137. ret = apply_param_change(avctx, &tmp);
  2138. if (ret < 0) {
  2139. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  2140. if (avctx->err_recognition & AV_EF_EXPLODE)
  2141. goto fail;
  2142. }
  2143. avctx->internal->pkt = &tmp;
  2144. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2145. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  2146. else {
  2147. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  2148. frame->pkt_dts = avpkt->dts;
  2149. }
  2150. if (ret >= 0 && *got_frame_ptr) {
  2151. add_metadata_from_side_data(avctx, frame);
  2152. avctx->frame_number++;
  2153. av_frame_set_best_effort_timestamp(frame,
  2154. guess_correct_pts(avctx,
  2155. frame->pkt_pts,
  2156. frame->pkt_dts));
  2157. if (frame->format == AV_SAMPLE_FMT_NONE)
  2158. frame->format = avctx->sample_fmt;
  2159. if (!frame->channel_layout)
  2160. frame->channel_layout = avctx->channel_layout;
  2161. if (!av_frame_get_channels(frame))
  2162. av_frame_set_channels(frame, avctx->channels);
  2163. if (!frame->sample_rate)
  2164. frame->sample_rate = avctx->sample_rate;
  2165. }
  2166. side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  2167. if(side && side_size>=10) {
  2168. avctx->internal->skip_samples = AV_RL32(side);
  2169. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  2170. avctx->internal->skip_samples);
  2171. discard_padding = AV_RL32(side + 4);
  2172. skip_reason = AV_RL8(side + 8);
  2173. discard_reason = AV_RL8(side + 9);
  2174. }
  2175. if (avctx->internal->skip_samples && *got_frame_ptr &&
  2176. !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
  2177. if(frame->nb_samples <= avctx->internal->skip_samples){
  2178. *got_frame_ptr = 0;
  2179. avctx->internal->skip_samples -= frame->nb_samples;
  2180. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  2181. avctx->internal->skip_samples);
  2182. } else {
  2183. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  2184. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  2185. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2186. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  2187. (AVRational){1, avctx->sample_rate},
  2188. avctx->pkt_timebase);
  2189. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  2190. frame->pkt_pts += diff_ts;
  2191. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  2192. frame->pkt_dts += diff_ts;
  2193. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2194. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2195. } else {
  2196. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  2197. }
  2198. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  2199. avctx->internal->skip_samples, frame->nb_samples);
  2200. frame->nb_samples -= avctx->internal->skip_samples;
  2201. avctx->internal->skip_samples = 0;
  2202. }
  2203. }
  2204. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
  2205. !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
  2206. if (discard_padding == frame->nb_samples) {
  2207. *got_frame_ptr = 0;
  2208. } else {
  2209. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  2210. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  2211. (AVRational){1, avctx->sample_rate},
  2212. avctx->pkt_timebase);
  2213. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  2214. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  2215. } else {
  2216. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  2217. }
  2218. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  2219. discard_padding, frame->nb_samples);
  2220. frame->nb_samples -= discard_padding;
  2221. }
  2222. }
  2223. if ((avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
  2224. AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
  2225. if (fside) {
  2226. AV_WL32(fside->data, avctx->internal->skip_samples);
  2227. AV_WL32(fside->data + 4, discard_padding);
  2228. AV_WL8(fside->data + 8, skip_reason);
  2229. AV_WL8(fside->data + 9, discard_reason);
  2230. avctx->internal->skip_samples = 0;
  2231. }
  2232. }
  2233. fail:
  2234. avctx->internal->pkt = NULL;
  2235. if (did_split) {
  2236. av_packet_free_side_data(&tmp);
  2237. if(ret == tmp.size)
  2238. ret = avpkt->size;
  2239. }
  2240. if (ret >= 0 && *got_frame_ptr) {
  2241. if (!avctx->refcounted_frames) {
  2242. int err = unrefcount_frame(avci, frame);
  2243. if (err < 0)
  2244. return err;
  2245. }
  2246. } else
  2247. av_frame_unref(frame);
  2248. }
  2249. return ret;
  2250. }
  2251. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  2252. static int recode_subtitle(AVCodecContext *avctx,
  2253. AVPacket *outpkt, const AVPacket *inpkt)
  2254. {
  2255. #if CONFIG_ICONV
  2256. iconv_t cd = (iconv_t)-1;
  2257. int ret = 0;
  2258. char *inb, *outb;
  2259. size_t inl, outl;
  2260. AVPacket tmp;
  2261. #endif
  2262. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
  2263. return 0;
  2264. #if CONFIG_ICONV
  2265. cd = iconv_open("UTF-8", avctx->sub_charenc);
  2266. av_assert0(cd != (iconv_t)-1);
  2267. inb = inpkt->data;
  2268. inl = inpkt->size;
  2269. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  2270. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  2271. ret = AVERROR(ENOMEM);
  2272. goto end;
  2273. }
  2274. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  2275. if (ret < 0)
  2276. goto end;
  2277. outpkt->buf = tmp.buf;
  2278. outpkt->data = tmp.data;
  2279. outpkt->size = tmp.size;
  2280. outb = outpkt->data;
  2281. outl = outpkt->size;
  2282. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  2283. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  2284. outl >= outpkt->size || inl != 0) {
  2285. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  2286. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2287. av_free_packet(&tmp);
  2288. ret = AVERROR(errno);
  2289. goto end;
  2290. }
  2291. outpkt->size -= outl;
  2292. memset(outpkt->data + outpkt->size, 0, outl);
  2293. end:
  2294. if (cd != (iconv_t)-1)
  2295. iconv_close(cd);
  2296. return ret;
  2297. #else
  2298. av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
  2299. return AVERROR(EINVAL);
  2300. #endif
  2301. }
  2302. static int utf8_check(const uint8_t *str)
  2303. {
  2304. const uint8_t *byte;
  2305. uint32_t codepoint, min;
  2306. while (*str) {
  2307. byte = str;
  2308. GET_UTF8(codepoint, *(byte++), return 0;);
  2309. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2310. 1 << (5 * (byte - str) - 4);
  2311. if (codepoint < min || codepoint >= 0x110000 ||
  2312. codepoint == 0xFFFE /* BOM */ ||
  2313. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2314. return 0;
  2315. str = byte;
  2316. }
  2317. return 1;
  2318. }
  2319. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2320. int *got_sub_ptr,
  2321. AVPacket *avpkt)
  2322. {
  2323. int i, ret = 0;
  2324. if (!avpkt->data && avpkt->size) {
  2325. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  2326. return AVERROR(EINVAL);
  2327. }
  2328. if (!avctx->codec)
  2329. return AVERROR(EINVAL);
  2330. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2331. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2332. return AVERROR(EINVAL);
  2333. }
  2334. *got_sub_ptr = 0;
  2335. get_subtitle_defaults(sub);
  2336. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  2337. AVPacket pkt_recoded;
  2338. AVPacket tmp = *avpkt;
  2339. int did_split = av_packet_split_side_data(&tmp);
  2340. //apply_param_change(avctx, &tmp);
  2341. if (did_split) {
  2342. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  2343. * proper padding.
  2344. * If the side data is smaller than the buffer padding size, the
  2345. * remaining bytes should have already been filled with zeros by the
  2346. * original packet allocation anyway. */
  2347. memset(tmp.data + tmp.size, 0,
  2348. FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
  2349. }
  2350. pkt_recoded = tmp;
  2351. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2352. if (ret < 0) {
  2353. *got_sub_ptr = 0;
  2354. } else {
  2355. avctx->internal->pkt = &pkt_recoded;
  2356. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2357. sub->pts = av_rescale_q(avpkt->pts,
  2358. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2359. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2360. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2361. !!*got_sub_ptr >= !!sub->num_rects);
  2362. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2363. avctx->pkt_timebase.num) {
  2364. AVRational ms = { 1, 1000 };
  2365. sub->end_display_time = av_rescale_q(avpkt->duration,
  2366. avctx->pkt_timebase, ms);
  2367. }
  2368. for (i = 0; i < sub->num_rects; i++) {
  2369. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2370. av_log(avctx, AV_LOG_ERROR,
  2371. "Invalid UTF-8 in decoded subtitles text; "
  2372. "maybe missing -sub_charenc option\n");
  2373. avsubtitle_free(sub);
  2374. return AVERROR_INVALIDDATA;
  2375. }
  2376. }
  2377. if (tmp.data != pkt_recoded.data) { // did we recode?
  2378. /* prevent from destroying side data from original packet */
  2379. pkt_recoded.side_data = NULL;
  2380. pkt_recoded.side_data_elems = 0;
  2381. av_free_packet(&pkt_recoded);
  2382. }
  2383. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2384. sub->format = 0;
  2385. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2386. sub->format = 1;
  2387. avctx->internal->pkt = NULL;
  2388. }
  2389. if (did_split) {
  2390. av_packet_free_side_data(&tmp);
  2391. if(ret == tmp.size)
  2392. ret = avpkt->size;
  2393. }
  2394. if (*got_sub_ptr)
  2395. avctx->frame_number++;
  2396. }
  2397. return ret;
  2398. }
  2399. void avsubtitle_free(AVSubtitle *sub)
  2400. {
  2401. int i;
  2402. for (i = 0; i < sub->num_rects; i++) {
  2403. av_freep(&sub->rects[i]->pict.data[0]);
  2404. av_freep(&sub->rects[i]->pict.data[1]);
  2405. av_freep(&sub->rects[i]->pict.data[2]);
  2406. av_freep(&sub->rects[i]->pict.data[3]);
  2407. av_freep(&sub->rects[i]->text);
  2408. av_freep(&sub->rects[i]->ass);
  2409. av_freep(&sub->rects[i]);
  2410. }
  2411. av_freep(&sub->rects);
  2412. memset(sub, 0, sizeof(AVSubtitle));
  2413. }
  2414. av_cold int avcodec_close(AVCodecContext *avctx)
  2415. {
  2416. if (!avctx)
  2417. return 0;
  2418. if (avcodec_is_open(avctx)) {
  2419. FramePool *pool = avctx->internal->pool;
  2420. int i;
  2421. if (CONFIG_FRAME_THREAD_ENCODER &&
  2422. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2423. ff_frame_thread_encoder_free(avctx);
  2424. }
  2425. if (HAVE_THREADS && avctx->internal->thread_ctx)
  2426. ff_thread_free(avctx);
  2427. if (avctx->codec && avctx->codec->close)
  2428. avctx->codec->close(avctx);
  2429. avctx->coded_frame = NULL;
  2430. avctx->internal->byte_buffer_size = 0;
  2431. av_freep(&avctx->internal->byte_buffer);
  2432. av_frame_free(&avctx->internal->to_free);
  2433. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2434. av_buffer_pool_uninit(&pool->pools[i]);
  2435. av_freep(&avctx->internal->pool);
  2436. if (avctx->hwaccel && avctx->hwaccel->uninit)
  2437. avctx->hwaccel->uninit(avctx);
  2438. av_freep(&avctx->internal->hwaccel_priv_data);
  2439. av_freep(&avctx->internal);
  2440. }
  2441. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2442. av_opt_free(avctx->priv_data);
  2443. av_opt_free(avctx);
  2444. av_freep(&avctx->priv_data);
  2445. if (av_codec_is_encoder(avctx->codec))
  2446. av_freep(&avctx->extradata);
  2447. avctx->codec = NULL;
  2448. avctx->active_thread_type = 0;
  2449. return 0;
  2450. }
  2451. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2452. {
  2453. switch(id){
  2454. //This is for future deprecatec codec ids, its empty since
  2455. //last major bump but will fill up again over time, please don't remove it
  2456. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2457. case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
  2458. case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
  2459. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2460. case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
  2461. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2462. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2463. case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
  2464. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2465. case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
  2466. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2467. case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
  2468. case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
  2469. case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
  2470. case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
  2471. case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
  2472. case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
  2473. case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
  2474. case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
  2475. default : return id;
  2476. }
  2477. }
  2478. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2479. {
  2480. AVCodec *p, *experimental = NULL;
  2481. p = first_avcodec;
  2482. id= remap_deprecated_codec_id(id);
  2483. while (p) {
  2484. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2485. p->id == id) {
  2486. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2487. experimental = p;
  2488. } else
  2489. return p;
  2490. }
  2491. p = p->next;
  2492. }
  2493. return experimental;
  2494. }
  2495. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2496. {
  2497. return find_encdec(id, 1);
  2498. }
  2499. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2500. {
  2501. AVCodec *p;
  2502. if (!name)
  2503. return NULL;
  2504. p = first_avcodec;
  2505. while (p) {
  2506. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2507. return p;
  2508. p = p->next;
  2509. }
  2510. return NULL;
  2511. }
  2512. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2513. {
  2514. return find_encdec(id, 0);
  2515. }
  2516. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2517. {
  2518. AVCodec *p;
  2519. if (!name)
  2520. return NULL;
  2521. p = first_avcodec;
  2522. while (p) {
  2523. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2524. return p;
  2525. p = p->next;
  2526. }
  2527. return NULL;
  2528. }
  2529. const char *avcodec_get_name(enum AVCodecID id)
  2530. {
  2531. const AVCodecDescriptor *cd;
  2532. AVCodec *codec;
  2533. if (id == AV_CODEC_ID_NONE)
  2534. return "none";
  2535. cd = avcodec_descriptor_get(id);
  2536. if (cd)
  2537. return cd->name;
  2538. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2539. codec = avcodec_find_decoder(id);
  2540. if (codec)
  2541. return codec->name;
  2542. codec = avcodec_find_encoder(id);
  2543. if (codec)
  2544. return codec->name;
  2545. return "unknown_codec";
  2546. }
  2547. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2548. {
  2549. int i, len, ret = 0;
  2550. #define TAG_PRINT(x) \
  2551. (((x) >= '0' && (x) <= '9') || \
  2552. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2553. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2554. for (i = 0; i < 4; i++) {
  2555. len = snprintf(buf, buf_size,
  2556. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2557. buf += len;
  2558. buf_size = buf_size > len ? buf_size - len : 0;
  2559. ret += len;
  2560. codec_tag >>= 8;
  2561. }
  2562. return ret;
  2563. }
  2564. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2565. {
  2566. const char *codec_type;
  2567. const char *codec_name;
  2568. const char *profile = NULL;
  2569. const AVCodec *p;
  2570. int bitrate;
  2571. int new_line = 0;
  2572. AVRational display_aspect_ratio;
  2573. const char *separator = enc->dump_separator ? enc->dump_separator : ", ";
  2574. if (!buf || buf_size <= 0)
  2575. return;
  2576. codec_type = av_get_media_type_string(enc->codec_type);
  2577. codec_name = avcodec_get_name(enc->codec_id);
  2578. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2579. if (enc->codec)
  2580. p = enc->codec;
  2581. else
  2582. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2583. avcodec_find_decoder(enc->codec_id);
  2584. if (p)
  2585. profile = av_get_profile_name(p, enc->profile);
  2586. }
  2587. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2588. codec_name);
  2589. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2590. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2591. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2592. if (profile)
  2593. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2594. if (enc->codec_tag) {
  2595. char tag_buf[32];
  2596. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2597. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2598. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2599. }
  2600. switch (enc->codec_type) {
  2601. case AVMEDIA_TYPE_VIDEO:
  2602. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2603. char detail[256] = "(";
  2604. av_strlcat(buf, separator, buf_size);
  2605. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2606. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  2607. av_get_pix_fmt_name(enc->pix_fmt));
  2608. if (enc->bits_per_raw_sample &&
  2609. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2610. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2611. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2612. av_strlcatf(detail, sizeof(detail), "%s, ",
  2613. av_color_range_name(enc->color_range));
  2614. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  2615. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  2616. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  2617. if (enc->colorspace != enc->color_primaries ||
  2618. enc->colorspace != enc->color_trc) {
  2619. new_line = 1;
  2620. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  2621. av_color_space_name(enc->colorspace),
  2622. av_color_primaries_name(enc->color_primaries),
  2623. av_color_transfer_name(enc->color_trc));
  2624. } else
  2625. av_strlcatf(detail, sizeof(detail), "%s, ",
  2626. av_get_colorspace_name(enc->colorspace));
  2627. }
  2628. if (av_log_get_level() >= AV_LOG_DEBUG &&
  2629. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  2630. av_strlcatf(detail, sizeof(detail), "%s, ",
  2631. av_chroma_location_name(enc->chroma_sample_location));
  2632. if (strlen(detail) > 1) {
  2633. detail[strlen(detail) - 2] = 0;
  2634. av_strlcatf(buf, buf_size, "%s)", detail);
  2635. }
  2636. }
  2637. if (enc->width) {
  2638. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  2639. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2640. "%dx%d",
  2641. enc->width, enc->height);
  2642. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  2643. (enc->width != enc->coded_width ||
  2644. enc->height != enc->coded_height))
  2645. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2646. " (%dx%d)", enc->coded_width, enc->coded_height);
  2647. if (enc->sample_aspect_ratio.num) {
  2648. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2649. enc->width * enc->sample_aspect_ratio.num,
  2650. enc->height * enc->sample_aspect_ratio.den,
  2651. 1024 * 1024);
  2652. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2653. " [SAR %d:%d DAR %d:%d]",
  2654. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2655. display_aspect_ratio.num, display_aspect_ratio.den);
  2656. }
  2657. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2658. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2659. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2660. ", %d/%d",
  2661. enc->time_base.num / g, enc->time_base.den / g);
  2662. }
  2663. }
  2664. if (encode) {
  2665. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2666. ", q=%d-%d", enc->qmin, enc->qmax);
  2667. }
  2668. break;
  2669. case AVMEDIA_TYPE_AUDIO:
  2670. av_strlcat(buf, separator, buf_size);
  2671. if (enc->sample_rate) {
  2672. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2673. "%d Hz, ", enc->sample_rate);
  2674. }
  2675. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2676. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2677. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2678. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2679. }
  2680. if ( enc->bits_per_raw_sample > 0
  2681. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  2682. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2683. " (%d bit)", enc->bits_per_raw_sample);
  2684. break;
  2685. case AVMEDIA_TYPE_DATA:
  2686. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2687. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2688. if (g)
  2689. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2690. ", %d/%d",
  2691. enc->time_base.num / g, enc->time_base.den / g);
  2692. }
  2693. break;
  2694. case AVMEDIA_TYPE_SUBTITLE:
  2695. if (enc->width)
  2696. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2697. ", %dx%d", enc->width, enc->height);
  2698. break;
  2699. default:
  2700. return;
  2701. }
  2702. if (encode) {
  2703. if (enc->flags & CODEC_FLAG_PASS1)
  2704. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2705. ", pass 1");
  2706. if (enc->flags & CODEC_FLAG_PASS2)
  2707. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2708. ", pass 2");
  2709. }
  2710. bitrate = get_bit_rate(enc);
  2711. if (bitrate != 0) {
  2712. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2713. ", %d kb/s", bitrate / 1000);
  2714. } else if (enc->rc_max_rate > 0) {
  2715. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2716. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2717. }
  2718. }
  2719. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2720. {
  2721. const AVProfile *p;
  2722. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2723. return NULL;
  2724. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2725. if (p->profile == profile)
  2726. return p->name;
  2727. return NULL;
  2728. }
  2729. unsigned avcodec_version(void)
  2730. {
  2731. // av_assert0(AV_CODEC_ID_V410==164);
  2732. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2733. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2734. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2735. av_assert0(AV_CODEC_ID_SRT==94216);
  2736. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2737. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2738. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2739. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2740. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2741. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2742. return LIBAVCODEC_VERSION_INT;
  2743. }
  2744. const char *avcodec_configuration(void)
  2745. {
  2746. return FFMPEG_CONFIGURATION;
  2747. }
  2748. const char *avcodec_license(void)
  2749. {
  2750. #define LICENSE_PREFIX "libavcodec license: "
  2751. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2752. }
  2753. void avcodec_flush_buffers(AVCodecContext *avctx)
  2754. {
  2755. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2756. ff_thread_flush(avctx);
  2757. else if (avctx->codec->flush)
  2758. avctx->codec->flush(avctx);
  2759. avctx->pts_correction_last_pts =
  2760. avctx->pts_correction_last_dts = INT64_MIN;
  2761. if (!avctx->refcounted_frames)
  2762. av_frame_unref(avctx->internal->to_free);
  2763. }
  2764. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2765. {
  2766. switch (codec_id) {
  2767. case AV_CODEC_ID_8SVX_EXP:
  2768. case AV_CODEC_ID_8SVX_FIB:
  2769. case AV_CODEC_ID_ADPCM_CT:
  2770. case AV_CODEC_ID_ADPCM_IMA_APC:
  2771. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2772. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2773. case AV_CODEC_ID_ADPCM_IMA_WS:
  2774. case AV_CODEC_ID_ADPCM_G722:
  2775. case AV_CODEC_ID_ADPCM_YAMAHA:
  2776. return 4;
  2777. case AV_CODEC_ID_DSD_LSBF:
  2778. case AV_CODEC_ID_DSD_MSBF:
  2779. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  2780. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  2781. case AV_CODEC_ID_PCM_ALAW:
  2782. case AV_CODEC_ID_PCM_MULAW:
  2783. case AV_CODEC_ID_PCM_S8:
  2784. case AV_CODEC_ID_PCM_S8_PLANAR:
  2785. case AV_CODEC_ID_PCM_U8:
  2786. case AV_CODEC_ID_PCM_ZORK:
  2787. return 8;
  2788. case AV_CODEC_ID_PCM_S16BE:
  2789. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2790. case AV_CODEC_ID_PCM_S16LE:
  2791. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2792. case AV_CODEC_ID_PCM_U16BE:
  2793. case AV_CODEC_ID_PCM_U16LE:
  2794. return 16;
  2795. case AV_CODEC_ID_PCM_S24DAUD:
  2796. case AV_CODEC_ID_PCM_S24BE:
  2797. case AV_CODEC_ID_PCM_S24LE:
  2798. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2799. case AV_CODEC_ID_PCM_U24BE:
  2800. case AV_CODEC_ID_PCM_U24LE:
  2801. return 24;
  2802. case AV_CODEC_ID_PCM_S32BE:
  2803. case AV_CODEC_ID_PCM_S32LE:
  2804. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2805. case AV_CODEC_ID_PCM_U32BE:
  2806. case AV_CODEC_ID_PCM_U32LE:
  2807. case AV_CODEC_ID_PCM_F32BE:
  2808. case AV_CODEC_ID_PCM_F32LE:
  2809. return 32;
  2810. case AV_CODEC_ID_PCM_F64BE:
  2811. case AV_CODEC_ID_PCM_F64LE:
  2812. return 64;
  2813. default:
  2814. return 0;
  2815. }
  2816. }
  2817. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2818. {
  2819. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2820. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2821. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2822. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2823. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2824. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2825. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2826. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2827. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2828. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2829. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2830. };
  2831. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2832. return AV_CODEC_ID_NONE;
  2833. if (be < 0 || be > 1)
  2834. be = AV_NE(1, 0);
  2835. return map[fmt][be];
  2836. }
  2837. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2838. {
  2839. switch (codec_id) {
  2840. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2841. return 2;
  2842. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2843. return 3;
  2844. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2845. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2846. case AV_CODEC_ID_ADPCM_IMA_QT:
  2847. case AV_CODEC_ID_ADPCM_SWF:
  2848. case AV_CODEC_ID_ADPCM_MS:
  2849. return 4;
  2850. default:
  2851. return av_get_exact_bits_per_sample(codec_id);
  2852. }
  2853. }
  2854. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2855. {
  2856. int id, sr, ch, ba, tag, bps;
  2857. id = avctx->codec_id;
  2858. sr = avctx->sample_rate;
  2859. ch = avctx->channels;
  2860. ba = avctx->block_align;
  2861. tag = avctx->codec_tag;
  2862. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2863. /* codecs with an exact constant bits per sample */
  2864. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2865. return (frame_bytes * 8LL) / (bps * ch);
  2866. bps = avctx->bits_per_coded_sample;
  2867. /* codecs with a fixed packet duration */
  2868. switch (id) {
  2869. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2870. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2871. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2872. case AV_CODEC_ID_AMR_NB:
  2873. case AV_CODEC_ID_EVRC:
  2874. case AV_CODEC_ID_GSM:
  2875. case AV_CODEC_ID_QCELP:
  2876. case AV_CODEC_ID_RA_288: return 160;
  2877. case AV_CODEC_ID_AMR_WB:
  2878. case AV_CODEC_ID_GSM_MS: return 320;
  2879. case AV_CODEC_ID_MP1: return 384;
  2880. case AV_CODEC_ID_ATRAC1: return 512;
  2881. case AV_CODEC_ID_ATRAC3: return 1024;
  2882. case AV_CODEC_ID_MP2:
  2883. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2884. case AV_CODEC_ID_AC3: return 1536;
  2885. }
  2886. if (sr > 0) {
  2887. /* calc from sample rate */
  2888. if (id == AV_CODEC_ID_TTA)
  2889. return 256 * sr / 245;
  2890. if (ch > 0) {
  2891. /* calc from sample rate and channels */
  2892. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2893. return (480 << (sr / 22050)) / ch;
  2894. }
  2895. }
  2896. if (ba > 0) {
  2897. /* calc from block_align */
  2898. if (id == AV_CODEC_ID_SIPR) {
  2899. switch (ba) {
  2900. case 20: return 160;
  2901. case 19: return 144;
  2902. case 29: return 288;
  2903. case 37: return 480;
  2904. }
  2905. } else if (id == AV_CODEC_ID_ILBC) {
  2906. switch (ba) {
  2907. case 38: return 160;
  2908. case 50: return 240;
  2909. }
  2910. }
  2911. }
  2912. if (frame_bytes > 0) {
  2913. /* calc from frame_bytes only */
  2914. if (id == AV_CODEC_ID_TRUESPEECH)
  2915. return 240 * (frame_bytes / 32);
  2916. if (id == AV_CODEC_ID_NELLYMOSER)
  2917. return 256 * (frame_bytes / 64);
  2918. if (id == AV_CODEC_ID_RA_144)
  2919. return 160 * (frame_bytes / 20);
  2920. if (id == AV_CODEC_ID_G723_1)
  2921. return 240 * (frame_bytes / 24);
  2922. if (bps > 0) {
  2923. /* calc from frame_bytes and bits_per_coded_sample */
  2924. if (id == AV_CODEC_ID_ADPCM_G726)
  2925. return frame_bytes * 8 / bps;
  2926. }
  2927. if (ch > 0) {
  2928. /* calc from frame_bytes and channels */
  2929. switch (id) {
  2930. case AV_CODEC_ID_ADPCM_AFC:
  2931. return frame_bytes / (9 * ch) * 16;
  2932. case AV_CODEC_ID_ADPCM_DTK:
  2933. return frame_bytes / (16 * ch) * 28;
  2934. case AV_CODEC_ID_ADPCM_4XM:
  2935. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2936. return (frame_bytes - 4 * ch) * 2 / ch;
  2937. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2938. return (frame_bytes - 4) * 2 / ch;
  2939. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2940. return (frame_bytes - 8) * 2 / ch;
  2941. case AV_CODEC_ID_ADPCM_XA:
  2942. return (frame_bytes / 128) * 224 / ch;
  2943. case AV_CODEC_ID_INTERPLAY_DPCM:
  2944. return (frame_bytes - 6 - ch) / ch;
  2945. case AV_CODEC_ID_ROQ_DPCM:
  2946. return (frame_bytes - 8) / ch;
  2947. case AV_CODEC_ID_XAN_DPCM:
  2948. return (frame_bytes - 2 * ch) / ch;
  2949. case AV_CODEC_ID_MACE3:
  2950. return 3 * frame_bytes / ch;
  2951. case AV_CODEC_ID_MACE6:
  2952. return 6 * frame_bytes / ch;
  2953. case AV_CODEC_ID_PCM_LXF:
  2954. return 2 * (frame_bytes / (5 * ch));
  2955. case AV_CODEC_ID_IAC:
  2956. case AV_CODEC_ID_IMC:
  2957. return 4 * frame_bytes / ch;
  2958. }
  2959. if (tag) {
  2960. /* calc from frame_bytes, channels, and codec_tag */
  2961. if (id == AV_CODEC_ID_SOL_DPCM) {
  2962. if (tag == 3)
  2963. return frame_bytes / ch;
  2964. else
  2965. return frame_bytes * 2 / ch;
  2966. }
  2967. }
  2968. if (ba > 0) {
  2969. /* calc from frame_bytes, channels, and block_align */
  2970. int blocks = frame_bytes / ba;
  2971. switch (avctx->codec_id) {
  2972. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2973. if (bps < 2 || bps > 5)
  2974. return 0;
  2975. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2976. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2977. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2978. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2979. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2980. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2981. return blocks * ((ba - 4 * ch) * 2 / ch);
  2982. case AV_CODEC_ID_ADPCM_MS:
  2983. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2984. }
  2985. }
  2986. if (bps > 0) {
  2987. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2988. switch (avctx->codec_id) {
  2989. case AV_CODEC_ID_PCM_DVD:
  2990. if(bps<4)
  2991. return 0;
  2992. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2993. case AV_CODEC_ID_PCM_BLURAY:
  2994. if(bps<4)
  2995. return 0;
  2996. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2997. case AV_CODEC_ID_S302M:
  2998. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2999. }
  3000. }
  3001. }
  3002. }
  3003. /* Fall back on using frame_size */
  3004. if (avctx->frame_size > 1 && frame_bytes)
  3005. return avctx->frame_size;
  3006. //For WMA we currently have no other means to calculate duration thus we
  3007. //do it here by assuming CBR, which is true for all known cases.
  3008. if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
  3009. if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
  3010. return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
  3011. }
  3012. return 0;
  3013. }
  3014. #if !HAVE_THREADS
  3015. int ff_thread_init(AVCodecContext *s)
  3016. {
  3017. return -1;
  3018. }
  3019. #endif
  3020. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  3021. {
  3022. unsigned int n = 0;
  3023. while (v >= 0xff) {
  3024. *s++ = 0xff;
  3025. v -= 0xff;
  3026. n++;
  3027. }
  3028. *s = v;
  3029. n++;
  3030. return n;
  3031. }
  3032. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  3033. {
  3034. int i;
  3035. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  3036. return i;
  3037. }
  3038. #if FF_API_MISSING_SAMPLE
  3039. FF_DISABLE_DEPRECATION_WARNINGS
  3040. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  3041. {
  3042. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  3043. "version to the newest one from Git. If the problem still "
  3044. "occurs, it means that your file has a feature which has not "
  3045. "been implemented.\n", feature);
  3046. if(want_sample)
  3047. av_log_ask_for_sample(avc, NULL);
  3048. }
  3049. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  3050. {
  3051. va_list argument_list;
  3052. va_start(argument_list, msg);
  3053. if (msg)
  3054. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  3055. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  3056. "of this file to ftp://upload.ffmpeg.org/incoming/ "
  3057. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
  3058. va_end(argument_list);
  3059. }
  3060. FF_ENABLE_DEPRECATION_WARNINGS
  3061. #endif /* FF_API_MISSING_SAMPLE */
  3062. static AVHWAccel *first_hwaccel = NULL;
  3063. static AVHWAccel **last_hwaccel = &first_hwaccel;
  3064. void av_register_hwaccel(AVHWAccel *hwaccel)
  3065. {
  3066. AVHWAccel **p = last_hwaccel;
  3067. hwaccel->next = NULL;
  3068. while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  3069. p = &(*p)->next;
  3070. last_hwaccel = &hwaccel->next;
  3071. }
  3072. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  3073. {
  3074. return hwaccel ? hwaccel->next : first_hwaccel;
  3075. }
  3076. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  3077. {
  3078. if (lockmgr_cb) {
  3079. // There is no good way to rollback a failure to destroy the
  3080. // mutex, so we ignore failures.
  3081. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  3082. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  3083. lockmgr_cb = NULL;
  3084. codec_mutex = NULL;
  3085. avformat_mutex = NULL;
  3086. }
  3087. if (cb) {
  3088. void *new_codec_mutex = NULL;
  3089. void *new_avformat_mutex = NULL;
  3090. int err;
  3091. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  3092. return err > 0 ? AVERROR_UNKNOWN : err;
  3093. }
  3094. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  3095. // Ignore failures to destroy the newly created mutex.
  3096. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  3097. return err > 0 ? AVERROR_UNKNOWN : err;
  3098. }
  3099. lockmgr_cb = cb;
  3100. codec_mutex = new_codec_mutex;
  3101. avformat_mutex = new_avformat_mutex;
  3102. }
  3103. return 0;
  3104. }
  3105. int ff_lock_avcodec(AVCodecContext *log_ctx)
  3106. {
  3107. if (lockmgr_cb) {
  3108. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  3109. return -1;
  3110. }
  3111. entangled_thread_counter++;
  3112. if (entangled_thread_counter != 1) {
  3113. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  3114. if (!lockmgr_cb)
  3115. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  3116. ff_avcodec_locked = 1;
  3117. ff_unlock_avcodec();
  3118. return AVERROR(EINVAL);
  3119. }
  3120. av_assert0(!ff_avcodec_locked);
  3121. ff_avcodec_locked = 1;
  3122. return 0;
  3123. }
  3124. int ff_unlock_avcodec(void)
  3125. {
  3126. av_assert0(ff_avcodec_locked);
  3127. ff_avcodec_locked = 0;
  3128. entangled_thread_counter--;
  3129. if (lockmgr_cb) {
  3130. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  3131. return -1;
  3132. }
  3133. return 0;
  3134. }
  3135. int avpriv_lock_avformat(void)
  3136. {
  3137. if (lockmgr_cb) {
  3138. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  3139. return -1;
  3140. }
  3141. return 0;
  3142. }
  3143. int avpriv_unlock_avformat(void)
  3144. {
  3145. if (lockmgr_cb) {
  3146. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  3147. return -1;
  3148. }
  3149. return 0;
  3150. }
  3151. unsigned int avpriv_toupper4(unsigned int x)
  3152. {
  3153. return av_toupper(x & 0xFF) +
  3154. (av_toupper((x >> 8) & 0xFF) << 8) +
  3155. (av_toupper((x >> 16) & 0xFF) << 16) +
  3156. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  3157. }
  3158. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  3159. {
  3160. int ret;
  3161. dst->owner = src->owner;
  3162. ret = av_frame_ref(dst->f, src->f);
  3163. if (ret < 0)
  3164. return ret;
  3165. if (src->progress &&
  3166. !(dst->progress = av_buffer_ref(src->progress))) {
  3167. ff_thread_release_buffer(dst->owner, dst);
  3168. return AVERROR(ENOMEM);
  3169. }
  3170. return 0;
  3171. }
  3172. #if !HAVE_THREADS
  3173. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  3174. {
  3175. return ff_get_format(avctx, fmt);
  3176. }
  3177. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  3178. {
  3179. f->owner = avctx;
  3180. return ff_get_buffer(avctx, f->f, flags);
  3181. }
  3182. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  3183. {
  3184. if (f->f)
  3185. av_frame_unref(f->f);
  3186. }
  3187. void ff_thread_finish_setup(AVCodecContext *avctx)
  3188. {
  3189. }
  3190. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  3191. {
  3192. }
  3193. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  3194. {
  3195. }
  3196. int ff_thread_can_start_frame(AVCodecContext *avctx)
  3197. {
  3198. return 1;
  3199. }
  3200. int ff_alloc_entries(AVCodecContext *avctx, int count)
  3201. {
  3202. return 0;
  3203. }
  3204. void ff_reset_entries(AVCodecContext *avctx)
  3205. {
  3206. }
  3207. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  3208. {
  3209. }
  3210. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  3211. {
  3212. }
  3213. #endif
  3214. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  3215. {
  3216. AVCodec *c= avcodec_find_decoder(codec_id);
  3217. if(!c)
  3218. c= avcodec_find_encoder(codec_id);
  3219. if(c)
  3220. return c->type;
  3221. if (codec_id <= AV_CODEC_ID_NONE)
  3222. return AVMEDIA_TYPE_UNKNOWN;
  3223. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  3224. return AVMEDIA_TYPE_VIDEO;
  3225. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  3226. return AVMEDIA_TYPE_AUDIO;
  3227. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  3228. return AVMEDIA_TYPE_SUBTITLE;
  3229. return AVMEDIA_TYPE_UNKNOWN;
  3230. }
  3231. int avcodec_is_open(AVCodecContext *s)
  3232. {
  3233. return !!s->internal;
  3234. }
  3235. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  3236. {
  3237. int ret;
  3238. char *str;
  3239. ret = av_bprint_finalize(buf, &str);
  3240. if (ret < 0)
  3241. return ret;
  3242. avctx->extradata = str;
  3243. /* Note: the string is NUL terminated (so extradata can be read as a
  3244. * string), but the ending character is not accounted in the size (in
  3245. * binary formats you are likely not supposed to mux that character). When
  3246. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  3247. * zeros. */
  3248. avctx->extradata_size = buf->len;
  3249. return 0;
  3250. }
  3251. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  3252. const uint8_t *end,
  3253. uint32_t *av_restrict state)
  3254. {
  3255. int i;
  3256. av_assert0(p <= end);
  3257. if (p >= end)
  3258. return end;
  3259. for (i = 0; i < 3; i++) {
  3260. uint32_t tmp = *state << 8;
  3261. *state = tmp + *(p++);
  3262. if (tmp == 0x100 || p == end)
  3263. return p;
  3264. }
  3265. while (p < end) {
  3266. if (p[-1] > 1 ) p += 3;
  3267. else if (p[-2] ) p += 2;
  3268. else if (p[-3]|(p[-1]-1)) p++;
  3269. else {
  3270. p++;
  3271. break;
  3272. }
  3273. }
  3274. p = FFMIN(p, end) - 4;
  3275. *state = AV_RB32(p);
  3276. return p + 4;
  3277. }