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.

3633 lines
119KB

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