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.

3655 lines
119KB

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