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.

3597 lines
117KB

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