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.

3773 lines
124KB

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