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.

2686 lines
86KB

  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 "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "thread.h"
  40. #include "frame_thread_encoder.h"
  41. #include "audioconvert.h"
  42. #include "internal.h"
  43. #include "bytestream.h"
  44. #include <stdlib.h>
  45. #include <stdarg.h>
  46. #include <limits.h>
  47. #include <float.h>
  48. static int volatile entangled_thread_counter = 0;
  49. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  50. static void *codec_mutex;
  51. static void *avformat_mutex;
  52. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  53. {
  54. if (min_size < *size)
  55. return ptr;
  56. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  57. ptr = av_realloc(ptr, min_size);
  58. /* we could set this to the unmodified min_size but this is safer
  59. * if the user lost the ptr and uses NULL now
  60. */
  61. if (!ptr)
  62. min_size = 0;
  63. *size = min_size;
  64. return ptr;
  65. }
  66. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  67. {
  68. void **p = ptr;
  69. if (min_size < *size)
  70. return 0;
  71. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  72. av_free(*p);
  73. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  74. if (!*p)
  75. min_size = 0;
  76. *size = min_size;
  77. return 1;
  78. }
  79. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  80. {
  81. ff_fast_malloc(ptr, size, min_size, 0);
  82. }
  83. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  84. {
  85. uint8_t **p = ptr;
  86. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  87. av_freep(p);
  88. *size = 0;
  89. return;
  90. }
  91. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  92. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  93. }
  94. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  95. {
  96. uint8_t **p = ptr;
  97. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  98. av_freep(p);
  99. *size = 0;
  100. return;
  101. }
  102. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  103. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  104. }
  105. /* encoder management */
  106. static AVCodec *first_avcodec = NULL;
  107. AVCodec *av_codec_next(const AVCodec *c)
  108. {
  109. if (c)
  110. return c->next;
  111. else
  112. return first_avcodec;
  113. }
  114. static void avcodec_init(void)
  115. {
  116. static int initialized = 0;
  117. if (initialized != 0)
  118. return;
  119. initialized = 1;
  120. ff_dsputil_static_init();
  121. }
  122. int av_codec_is_encoder(const AVCodec *codec)
  123. {
  124. return codec && (codec->encode_sub || codec->encode2);
  125. }
  126. int av_codec_is_decoder(const AVCodec *codec)
  127. {
  128. return codec && codec->decode;
  129. }
  130. void avcodec_register(AVCodec *codec)
  131. {
  132. AVCodec **p;
  133. avcodec_init();
  134. p = &first_avcodec;
  135. while (*p != NULL)
  136. p = &(*p)->next;
  137. *p = codec;
  138. codec->next = NULL;
  139. if (codec->init_static_data)
  140. codec->init_static_data(codec);
  141. }
  142. unsigned avcodec_get_edge_width(void)
  143. {
  144. return EDGE_WIDTH;
  145. }
  146. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  147. {
  148. s->coded_width = width;
  149. s->coded_height = height;
  150. s->width = -((-width ) >> s->lowres);
  151. s->height = -((-height) >> s->lowres);
  152. }
  153. #define INTERNAL_BUFFER_SIZE (32 + 1)
  154. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  155. int linesize_align[AV_NUM_DATA_POINTERS])
  156. {
  157. int i;
  158. int w_align = 1;
  159. int h_align = 1;
  160. switch (s->pix_fmt) {
  161. case AV_PIX_FMT_YUV420P:
  162. case AV_PIX_FMT_YUYV422:
  163. case AV_PIX_FMT_UYVY422:
  164. case AV_PIX_FMT_YUV422P:
  165. case AV_PIX_FMT_YUV440P:
  166. case AV_PIX_FMT_YUV444P:
  167. case AV_PIX_FMT_GBRP:
  168. case AV_PIX_FMT_GRAY8:
  169. case AV_PIX_FMT_GRAY16BE:
  170. case AV_PIX_FMT_GRAY16LE:
  171. case AV_PIX_FMT_YUVJ420P:
  172. case AV_PIX_FMT_YUVJ422P:
  173. case AV_PIX_FMT_YUVJ440P:
  174. case AV_PIX_FMT_YUVJ444P:
  175. case AV_PIX_FMT_YUVA420P:
  176. case AV_PIX_FMT_YUVA422P:
  177. case AV_PIX_FMT_YUVA444P:
  178. case AV_PIX_FMT_YUV420P9LE:
  179. case AV_PIX_FMT_YUV420P9BE:
  180. case AV_PIX_FMT_YUV420P10LE:
  181. case AV_PIX_FMT_YUV420P10BE:
  182. case AV_PIX_FMT_YUV420P12LE:
  183. case AV_PIX_FMT_YUV420P12BE:
  184. case AV_PIX_FMT_YUV420P14LE:
  185. case AV_PIX_FMT_YUV420P14BE:
  186. case AV_PIX_FMT_YUV422P9LE:
  187. case AV_PIX_FMT_YUV422P9BE:
  188. case AV_PIX_FMT_YUV422P10LE:
  189. case AV_PIX_FMT_YUV422P10BE:
  190. case AV_PIX_FMT_YUV422P12LE:
  191. case AV_PIX_FMT_YUV422P12BE:
  192. case AV_PIX_FMT_YUV422P14LE:
  193. case AV_PIX_FMT_YUV422P14BE:
  194. case AV_PIX_FMT_YUV444P9LE:
  195. case AV_PIX_FMT_YUV444P9BE:
  196. case AV_PIX_FMT_YUV444P10LE:
  197. case AV_PIX_FMT_YUV444P10BE:
  198. case AV_PIX_FMT_YUV444P12LE:
  199. case AV_PIX_FMT_YUV444P12BE:
  200. case AV_PIX_FMT_YUV444P14LE:
  201. case AV_PIX_FMT_YUV444P14BE:
  202. case AV_PIX_FMT_GBRP9LE:
  203. case AV_PIX_FMT_GBRP9BE:
  204. case AV_PIX_FMT_GBRP10LE:
  205. case AV_PIX_FMT_GBRP10BE:
  206. case AV_PIX_FMT_GBRP12LE:
  207. case AV_PIX_FMT_GBRP12BE:
  208. case AV_PIX_FMT_GBRP14LE:
  209. case AV_PIX_FMT_GBRP14BE:
  210. w_align = 16; //FIXME assume 16 pixel per macroblock
  211. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  212. break;
  213. case AV_PIX_FMT_YUV411P:
  214. case AV_PIX_FMT_UYYVYY411:
  215. w_align = 32;
  216. h_align = 8;
  217. break;
  218. case AV_PIX_FMT_YUV410P:
  219. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  220. w_align = 64;
  221. h_align = 64;
  222. }
  223. break;
  224. case AV_PIX_FMT_RGB555:
  225. if (s->codec_id == AV_CODEC_ID_RPZA) {
  226. w_align = 4;
  227. h_align = 4;
  228. }
  229. break;
  230. case AV_PIX_FMT_PAL8:
  231. case AV_PIX_FMT_BGR8:
  232. case AV_PIX_FMT_RGB8:
  233. if (s->codec_id == AV_CODEC_ID_SMC) {
  234. w_align = 4;
  235. h_align = 4;
  236. }
  237. break;
  238. case AV_PIX_FMT_BGR24:
  239. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  240. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  241. w_align = 4;
  242. h_align = 4;
  243. }
  244. break;
  245. default:
  246. w_align = 1;
  247. h_align = 1;
  248. break;
  249. }
  250. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  251. w_align = FFMAX(w_align, 8);
  252. }
  253. *width = FFALIGN(*width, w_align);
  254. *height = FFALIGN(*height, h_align);
  255. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  256. // some of the optimized chroma MC reads one line too much
  257. // which is also done in mpeg decoders with lowres > 0
  258. *height += 2;
  259. for (i = 0; i < 4; i++)
  260. linesize_align[i] = STRIDE_ALIGN;
  261. }
  262. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  263. {
  264. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  265. int chroma_shift = desc->log2_chroma_w;
  266. int linesize_align[AV_NUM_DATA_POINTERS];
  267. int align;
  268. avcodec_align_dimensions2(s, width, height, linesize_align);
  269. align = FFMAX(linesize_align[0], linesize_align[3]);
  270. linesize_align[1] <<= chroma_shift;
  271. linesize_align[2] <<= chroma_shift;
  272. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  273. *width = FFALIGN(*width, align);
  274. }
  275. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  276. {
  277. if (s->pkt) {
  278. frame->pkt_pts = s->pkt->pts;
  279. frame->pkt_pos = s->pkt->pos;
  280. frame->pkt_duration = s->pkt->duration;
  281. } else {
  282. frame->pkt_pts = AV_NOPTS_VALUE;
  283. frame->pkt_pos = -1;
  284. frame->pkt_duration = 0;
  285. }
  286. frame->reordered_opaque = s->reordered_opaque;
  287. switch (s->codec->type) {
  288. case AVMEDIA_TYPE_VIDEO:
  289. frame->width = s->width;
  290. frame->height = s->height;
  291. frame->format = s->pix_fmt;
  292. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  293. break;
  294. case AVMEDIA_TYPE_AUDIO:
  295. frame->sample_rate = s->sample_rate;
  296. frame->format = s->sample_fmt;
  297. frame->channel_layout = s->channel_layout;
  298. frame->channels = s->channels;
  299. break;
  300. }
  301. }
  302. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  303. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  304. int buf_size, int align)
  305. {
  306. int ch, planar, needed_size, ret = 0;
  307. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  308. frame->nb_samples, sample_fmt,
  309. align);
  310. if (buf_size < needed_size)
  311. return AVERROR(EINVAL);
  312. planar = av_sample_fmt_is_planar(sample_fmt);
  313. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  314. if (!(frame->extended_data = av_mallocz(nb_channels *
  315. sizeof(*frame->extended_data))))
  316. return AVERROR(ENOMEM);
  317. } else {
  318. frame->extended_data = frame->data;
  319. }
  320. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  321. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  322. sample_fmt, align)) < 0) {
  323. if (frame->extended_data != frame->data)
  324. av_freep(&frame->extended_data);
  325. return ret;
  326. }
  327. if (frame->extended_data != frame->data) {
  328. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  329. frame->data[ch] = frame->extended_data[ch];
  330. }
  331. return ret;
  332. }
  333. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  334. {
  335. AVCodecInternal *avci = avctx->internal;
  336. InternalBuffer *buf;
  337. int buf_size, ret;
  338. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  339. frame->nb_samples, avctx->sample_fmt,
  340. 0);
  341. if (buf_size < 0)
  342. return AVERROR(EINVAL);
  343. /* allocate InternalBuffer if needed */
  344. if (!avci->buffer) {
  345. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  346. if (!avci->buffer)
  347. return AVERROR(ENOMEM);
  348. }
  349. buf = avci->buffer;
  350. /* if there is a previously-used internal buffer, check its size and
  351. * channel count to see if we can reuse it */
  352. if (buf->extended_data) {
  353. /* if current buffer is too small, free it */
  354. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  355. av_free(buf->extended_data[0]);
  356. if (buf->extended_data != buf->data)
  357. av_free(buf->extended_data);
  358. buf->extended_data = NULL;
  359. buf->data[0] = NULL;
  360. }
  361. /* if number of channels has changed, reset and/or free extended data
  362. * pointers but leave data buffer in buf->data[0] for reuse */
  363. if (buf->nb_channels != avctx->channels) {
  364. if (buf->extended_data != buf->data)
  365. av_free(buf->extended_data);
  366. buf->extended_data = NULL;
  367. }
  368. }
  369. /* if there is no previous buffer or the previous buffer cannot be used
  370. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  371. if (!buf->extended_data) {
  372. if (!buf->data[0]) {
  373. if (!(buf->data[0] = av_mallocz(buf_size)))
  374. return AVERROR(ENOMEM);
  375. buf->audio_data_size = buf_size;
  376. }
  377. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  378. avctx->sample_fmt, buf->data[0],
  379. buf->audio_data_size, 0)))
  380. return ret;
  381. if (frame->extended_data == frame->data)
  382. buf->extended_data = buf->data;
  383. else
  384. buf->extended_data = frame->extended_data;
  385. memcpy(buf->data, frame->data, sizeof(frame->data));
  386. buf->linesize[0] = frame->linesize[0];
  387. buf->nb_channels = avctx->channels;
  388. } else {
  389. /* copy InternalBuffer info to the AVFrame */
  390. frame->extended_data = buf->extended_data;
  391. frame->linesize[0] = buf->linesize[0];
  392. memcpy(frame->data, buf->data, sizeof(frame->data));
  393. }
  394. frame->type = FF_BUFFER_TYPE_INTERNAL;
  395. ff_init_buffer_info(avctx, frame);
  396. if (avctx->debug & FF_DEBUG_BUFFERS)
  397. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  398. "internal audio buffer used\n", frame);
  399. return 0;
  400. }
  401. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  402. {
  403. int i;
  404. int w = s->width;
  405. int h = s->height;
  406. InternalBuffer *buf;
  407. AVCodecInternal *avci = s->internal;
  408. if (pic->data[0] != NULL) {
  409. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  410. return -1;
  411. }
  412. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  413. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  414. return -1;
  415. }
  416. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  417. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  418. return -1;
  419. }
  420. if (!avci->buffer) {
  421. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  422. sizeof(InternalBuffer));
  423. }
  424. buf = &avci->buffer[avci->buffer_count];
  425. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  426. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  427. av_freep(&buf->base[i]);
  428. buf->data[i] = NULL;
  429. }
  430. }
  431. if (!buf->base[0]) {
  432. int h_chroma_shift, v_chroma_shift;
  433. int size[4] = { 0 };
  434. int tmpsize;
  435. int unaligned;
  436. AVPicture picture;
  437. int stride_align[AV_NUM_DATA_POINTERS];
  438. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  439. const int pixel_size = desc->comp[0].step_minus1 + 1;
  440. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  441. avcodec_align_dimensions2(s, &w, &h, stride_align);
  442. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  443. w += EDGE_WIDTH * 2;
  444. h += EDGE_WIDTH * 2;
  445. }
  446. do {
  447. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  448. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  449. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  450. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  451. w += w & ~(w - 1);
  452. unaligned = 0;
  453. for (i = 0; i < 4; i++)
  454. unaligned |= picture.linesize[i] % stride_align[i];
  455. } while (unaligned);
  456. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  457. if (tmpsize < 0)
  458. return -1;
  459. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  460. size[i] = picture.data[i + 1] - picture.data[i];
  461. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  462. memset(buf->base, 0, sizeof(buf->base));
  463. memset(buf->data, 0, sizeof(buf->data));
  464. for (i = 0; i < 4 && size[i]; i++) {
  465. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  466. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  467. buf->linesize[i] = picture.linesize[i];
  468. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  469. if (buf->base[i] == NULL)
  470. return AVERROR(ENOMEM);
  471. memset(buf->base[i], 128, size[i]);
  472. // no edge if EDGE EMU or not planar YUV
  473. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  474. buf->data[i] = buf->base[i];
  475. else
  476. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  477. }
  478. for (; i < AV_NUM_DATA_POINTERS; i++) {
  479. buf->base[i] = buf->data[i] = NULL;
  480. buf->linesize[i] = 0;
  481. }
  482. if (size[1] && !size[2])
  483. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  484. buf->width = s->width;
  485. buf->height = s->height;
  486. buf->pix_fmt = s->pix_fmt;
  487. }
  488. pic->type = FF_BUFFER_TYPE_INTERNAL;
  489. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  490. pic->base[i] = buf->base[i];
  491. pic->data[i] = buf->data[i];
  492. pic->linesize[i] = buf->linesize[i];
  493. }
  494. pic->extended_data = pic->data;
  495. avci->buffer_count++;
  496. pic->width = buf->width;
  497. pic->height = buf->height;
  498. pic->format = buf->pix_fmt;
  499. ff_init_buffer_info(s, pic);
  500. if (s->debug & FF_DEBUG_BUFFERS)
  501. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  502. "buffers used\n", pic, avci->buffer_count);
  503. return 0;
  504. }
  505. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  506. {
  507. switch (avctx->codec_type) {
  508. case AVMEDIA_TYPE_VIDEO:
  509. return video_get_buffer(avctx, frame);
  510. case AVMEDIA_TYPE_AUDIO:
  511. return audio_get_buffer(avctx, frame);
  512. default:
  513. return -1;
  514. }
  515. }
  516. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  517. {
  518. int i;
  519. InternalBuffer *buf, *last;
  520. AVCodecInternal *avci = s->internal;
  521. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  522. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  523. assert(avci->buffer_count);
  524. if (avci->buffer) {
  525. buf = NULL; /* avoids warning */
  526. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  527. buf = &avci->buffer[i];
  528. if (buf->data[0] == pic->data[0])
  529. break;
  530. }
  531. av_assert0(i < avci->buffer_count);
  532. avci->buffer_count--;
  533. last = &avci->buffer[avci->buffer_count];
  534. if (buf != last)
  535. FFSWAP(InternalBuffer, *buf, *last);
  536. }
  537. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  538. pic->data[i] = NULL;
  539. // pic->base[i]=NULL;
  540. if (s->debug & FF_DEBUG_BUFFERS)
  541. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  542. "buffers used\n", pic, avci->buffer_count);
  543. }
  544. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  545. {
  546. AVFrame temp_pic;
  547. int i, ret;
  548. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  549. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  550. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  551. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  552. s->release_buffer(s, pic);
  553. }
  554. ff_init_buffer_info(s, pic);
  555. /* If no picture return a new buffer */
  556. if (pic->data[0] == NULL) {
  557. /* We will copy from buffer, so must be readable */
  558. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  559. return s->get_buffer(s, pic);
  560. }
  561. assert(s->pix_fmt == pic->format);
  562. /* If internal buffer type return the same buffer */
  563. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  564. return 0;
  565. }
  566. /*
  567. * Not internal type and reget_buffer not overridden, emulate cr buffer
  568. */
  569. temp_pic = *pic;
  570. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  571. pic->data[i] = pic->base[i] = NULL;
  572. pic->opaque = NULL;
  573. /* Allocate new frame */
  574. if ((ret = s->get_buffer(s, pic)))
  575. return ret;
  576. /* Copy image data from old buffer to new buffer */
  577. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  578. s->height);
  579. s->release_buffer(s, &temp_pic); // Release old frame
  580. return 0;
  581. }
  582. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  583. {
  584. int i;
  585. for (i = 0; i < count; i++) {
  586. int r = func(c, (char *)arg + i * size);
  587. if (ret)
  588. ret[i] = r;
  589. }
  590. return 0;
  591. }
  592. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  593. {
  594. int i;
  595. for (i = 0; i < count; i++) {
  596. int r = func(c, arg, i, 0);
  597. if (ret)
  598. ret[i] = r;
  599. }
  600. return 0;
  601. }
  602. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  603. {
  604. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  605. ++fmt;
  606. return fmt[0];
  607. }
  608. void avcodec_get_frame_defaults(AVFrame *frame)
  609. {
  610. #if LIBAVCODEC_VERSION_MAJOR >= 55
  611. // extended_data should explicitly be freed when needed, this code is unsafe currently
  612. // also this is not compatible to the <55 ABI/API
  613. if (frame->extended_data != frame->data && 0)
  614. av_freep(&frame->extended_data);
  615. #endif
  616. memset(frame, 0, sizeof(AVFrame));
  617. frame->pts =
  618. frame->pkt_dts =
  619. frame->pkt_pts =
  620. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  621. frame->pkt_duration = 0;
  622. frame->pkt_pos = -1;
  623. frame->key_frame = 1;
  624. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  625. frame->format = -1; /* unknown */
  626. frame->extended_data = frame->data;
  627. }
  628. AVFrame *avcodec_alloc_frame(void)
  629. {
  630. AVFrame *frame = av_malloc(sizeof(AVFrame));
  631. if (frame == NULL)
  632. return NULL;
  633. frame->extended_data = NULL;
  634. avcodec_get_frame_defaults(frame);
  635. return frame;
  636. }
  637. void avcodec_free_frame(AVFrame **frame)
  638. {
  639. AVFrame *f;
  640. if (!frame || !*frame)
  641. return;
  642. f = *frame;
  643. if (f->extended_data != f->data)
  644. av_freep(&f->extended_data);
  645. av_freep(frame);
  646. }
  647. #define MAKE_ACCESSORS(str, name, type, field) \
  648. type av_##name##_get_##field(const str *s) { return s->field; } \
  649. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  650. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  651. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  652. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  653. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  654. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  655. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  656. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  657. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  658. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  659. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  660. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  661. {
  662. memset(sub, 0, sizeof(*sub));
  663. sub->pts = AV_NOPTS_VALUE;
  664. }
  665. static int get_bit_rate(AVCodecContext *ctx)
  666. {
  667. int bit_rate;
  668. int bits_per_sample;
  669. switch (ctx->codec_type) {
  670. case AVMEDIA_TYPE_VIDEO:
  671. case AVMEDIA_TYPE_DATA:
  672. case AVMEDIA_TYPE_SUBTITLE:
  673. case AVMEDIA_TYPE_ATTACHMENT:
  674. bit_rate = ctx->bit_rate;
  675. break;
  676. case AVMEDIA_TYPE_AUDIO:
  677. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  678. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  679. break;
  680. default:
  681. bit_rate = 0;
  682. break;
  683. }
  684. return bit_rate;
  685. }
  686. #if FF_API_AVCODEC_OPEN
  687. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  688. {
  689. return avcodec_open2(avctx, codec, NULL);
  690. }
  691. #endif
  692. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  693. {
  694. int ret = 0;
  695. entangled_thread_counter--;
  696. /* Release any user-supplied mutex. */
  697. if (ff_lockmgr_cb) {
  698. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  699. }
  700. ret = avcodec_open2(avctx, codec, options);
  701. /* If there is a user-supplied mutex locking routine, call it. */
  702. if (ff_lockmgr_cb) {
  703. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  704. return -1;
  705. }
  706. entangled_thread_counter++;
  707. return ret;
  708. }
  709. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  710. {
  711. int ret = 0;
  712. AVDictionary *tmp = NULL;
  713. if (avcodec_is_open(avctx))
  714. return 0;
  715. if ((!codec && !avctx->codec)) {
  716. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  717. return AVERROR(EINVAL);
  718. }
  719. if ((codec && avctx->codec && codec != avctx->codec)) {
  720. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  721. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  722. return AVERROR(EINVAL);
  723. }
  724. if (!codec)
  725. codec = avctx->codec;
  726. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  727. return AVERROR(EINVAL);
  728. if (options)
  729. av_dict_copy(&tmp, *options, 0);
  730. /* If there is a user-supplied mutex locking routine, call it. */
  731. if (ff_lockmgr_cb) {
  732. if ((ret = (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) < 0)
  733. return ret;
  734. }
  735. entangled_thread_counter++;
  736. if (entangled_thread_counter != 1) {
  737. av_log(avctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  738. ret = AVERROR(EINVAL);
  739. goto end;
  740. }
  741. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  742. if (!avctx->internal) {
  743. ret = AVERROR(ENOMEM);
  744. goto end;
  745. }
  746. if (codec->priv_data_size > 0) {
  747. if (!avctx->priv_data) {
  748. avctx->priv_data = av_mallocz(codec->priv_data_size);
  749. if (!avctx->priv_data) {
  750. ret = AVERROR(ENOMEM);
  751. goto end;
  752. }
  753. if (codec->priv_class) {
  754. *(const AVClass **)avctx->priv_data = codec->priv_class;
  755. av_opt_set_defaults(avctx->priv_data);
  756. }
  757. }
  758. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  759. goto free_and_end;
  760. } else {
  761. avctx->priv_data = NULL;
  762. }
  763. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  764. goto free_and_end;
  765. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  766. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  767. if (avctx->coded_width && avctx->coded_height)
  768. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  769. else if (avctx->width && avctx->height)
  770. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  771. }
  772. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  773. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  774. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  775. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  776. avcodec_set_dimensions(avctx, 0, 0);
  777. }
  778. /* if the decoder init function was already called previously,
  779. * free the already allocated subtitle_header before overwriting it */
  780. if (av_codec_is_decoder(codec))
  781. av_freep(&avctx->subtitle_header);
  782. #define SANE_NB_CHANNELS 128U
  783. if (avctx->channels > SANE_NB_CHANNELS) {
  784. ret = AVERROR(EINVAL);
  785. goto free_and_end;
  786. }
  787. avctx->codec = codec;
  788. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  789. avctx->codec_id == AV_CODEC_ID_NONE) {
  790. avctx->codec_type = codec->type;
  791. avctx->codec_id = codec->id;
  792. }
  793. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  794. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  795. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  796. ret = AVERROR(EINVAL);
  797. goto free_and_end;
  798. }
  799. avctx->frame_number = 0;
  800. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  801. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  802. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  803. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  804. AVCodec *codec2;
  805. av_log(NULL, AV_LOG_ERROR,
  806. "The %s '%s' is experimental but experimental codecs are not enabled, "
  807. "add '-strict %d' if you want to use it.\n",
  808. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  809. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  810. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  811. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  812. codec_string, codec2->name);
  813. ret = AVERROR_EXPERIMENTAL;
  814. goto free_and_end;
  815. }
  816. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  817. (!avctx->time_base.num || !avctx->time_base.den)) {
  818. avctx->time_base.num = 1;
  819. avctx->time_base.den = avctx->sample_rate;
  820. }
  821. if (!HAVE_THREADS)
  822. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  823. if (HAVE_THREADS) {
  824. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  825. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  826. entangled_thread_counter++;
  827. if (ret < 0)
  828. goto free_and_end;
  829. }
  830. if (HAVE_THREADS && !avctx->thread_opaque
  831. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  832. ret = ff_thread_init(avctx);
  833. if (ret < 0) {
  834. goto free_and_end;
  835. }
  836. }
  837. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  838. avctx->thread_count = 1;
  839. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  840. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  841. avctx->codec->max_lowres);
  842. ret = AVERROR(EINVAL);
  843. goto free_and_end;
  844. }
  845. if (av_codec_is_encoder(avctx->codec)) {
  846. int i;
  847. if (avctx->codec->sample_fmts) {
  848. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  849. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  850. break;
  851. if (avctx->channels == 1 &&
  852. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  853. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  854. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  855. break;
  856. }
  857. }
  858. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  859. char buf[128];
  860. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  861. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  862. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  863. ret = AVERROR(EINVAL);
  864. goto free_and_end;
  865. }
  866. }
  867. if (avctx->codec->pix_fmts) {
  868. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  869. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  870. break;
  871. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  872. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  873. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  874. char buf[128];
  875. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  876. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  877. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  878. ret = AVERROR(EINVAL);
  879. goto free_and_end;
  880. }
  881. }
  882. if (avctx->codec->supported_samplerates) {
  883. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  884. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  885. break;
  886. if (avctx->codec->supported_samplerates[i] == 0) {
  887. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  888. avctx->sample_rate);
  889. ret = AVERROR(EINVAL);
  890. goto free_and_end;
  891. }
  892. }
  893. if (avctx->codec->channel_layouts) {
  894. if (!avctx->channel_layout) {
  895. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  896. } else {
  897. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  898. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  899. break;
  900. if (avctx->codec->channel_layouts[i] == 0) {
  901. char buf[512];
  902. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  903. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  904. ret = AVERROR(EINVAL);
  905. goto free_and_end;
  906. }
  907. }
  908. }
  909. if (avctx->channel_layout && avctx->channels) {
  910. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  911. if (channels != avctx->channels) {
  912. char buf[512];
  913. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  914. av_log(avctx, AV_LOG_ERROR,
  915. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  916. buf, channels, avctx->channels);
  917. ret = AVERROR(EINVAL);
  918. goto free_and_end;
  919. }
  920. } else if (avctx->channel_layout) {
  921. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  922. }
  923. }
  924. avctx->pts_correction_num_faulty_pts =
  925. avctx->pts_correction_num_faulty_dts = 0;
  926. avctx->pts_correction_last_pts =
  927. avctx->pts_correction_last_dts = INT64_MIN;
  928. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  929. || avctx->internal->frame_thread_encoder)) {
  930. ret = avctx->codec->init(avctx);
  931. if (ret < 0) {
  932. goto free_and_end;
  933. }
  934. }
  935. ret=0;
  936. if (av_codec_is_decoder(avctx->codec)) {
  937. if (!avctx->bit_rate)
  938. avctx->bit_rate = get_bit_rate(avctx);
  939. /* validate channel layout from the decoder */
  940. if (avctx->channel_layout) {
  941. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  942. if (!avctx->channels)
  943. avctx->channels = channels;
  944. else if (channels != avctx->channels) {
  945. char buf[512];
  946. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  947. av_log(avctx, AV_LOG_WARNING,
  948. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  949. "ignoring specified channel layout\n",
  950. buf, channels, avctx->channels);
  951. avctx->channel_layout = 0;
  952. }
  953. }
  954. }
  955. end:
  956. entangled_thread_counter--;
  957. /* Release any user-supplied mutex. */
  958. if (ff_lockmgr_cb) {
  959. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  960. }
  961. if (options) {
  962. av_dict_free(options);
  963. *options = tmp;
  964. }
  965. return ret;
  966. free_and_end:
  967. av_dict_free(&tmp);
  968. av_freep(&avctx->priv_data);
  969. av_freep(&avctx->internal);
  970. avctx->codec = NULL;
  971. goto end;
  972. }
  973. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  974. {
  975. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  976. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  977. return AVERROR(EINVAL);
  978. }
  979. if (avctx) {
  980. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  981. if (!avpkt->data || avpkt->size < size) {
  982. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  983. avpkt->data = avctx->internal->byte_buffer;
  984. avpkt->size = avctx->internal->byte_buffer_size;
  985. avpkt->destruct = NULL;
  986. }
  987. }
  988. if (avpkt->data) {
  989. void *destruct = avpkt->destruct;
  990. if (avpkt->size < size) {
  991. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  992. return AVERROR(EINVAL);
  993. }
  994. av_init_packet(avpkt);
  995. avpkt->destruct = destruct;
  996. avpkt->size = size;
  997. return 0;
  998. } else {
  999. int ret = av_new_packet(avpkt, size);
  1000. if (ret < 0)
  1001. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1002. return ret;
  1003. }
  1004. }
  1005. int ff_alloc_packet(AVPacket *avpkt, int size)
  1006. {
  1007. return ff_alloc_packet2(NULL, avpkt, size);
  1008. }
  1009. /**
  1010. * Pad last frame with silence.
  1011. */
  1012. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1013. {
  1014. AVFrame *frame = NULL;
  1015. uint8_t *buf = NULL;
  1016. int ret;
  1017. if (!(frame = avcodec_alloc_frame()))
  1018. return AVERROR(ENOMEM);
  1019. *frame = *src;
  1020. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1021. s->frame_size, s->sample_fmt, 0)) < 0)
  1022. goto fail;
  1023. if (!(buf = av_malloc(ret))) {
  1024. ret = AVERROR(ENOMEM);
  1025. goto fail;
  1026. }
  1027. frame->nb_samples = s->frame_size;
  1028. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1029. buf, ret, 0)) < 0)
  1030. goto fail;
  1031. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1032. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1033. goto fail;
  1034. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1035. frame->nb_samples - src->nb_samples,
  1036. s->channels, s->sample_fmt)) < 0)
  1037. goto fail;
  1038. *dst = frame;
  1039. return 0;
  1040. fail:
  1041. if (frame->extended_data != frame->data)
  1042. av_freep(&frame->extended_data);
  1043. av_freep(&buf);
  1044. av_freep(&frame);
  1045. return ret;
  1046. }
  1047. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1048. AVPacket *avpkt,
  1049. const AVFrame *frame,
  1050. int *got_packet_ptr)
  1051. {
  1052. AVFrame tmp;
  1053. AVFrame *padded_frame = NULL;
  1054. int ret;
  1055. AVPacket user_pkt = *avpkt;
  1056. int needs_realloc = !user_pkt.data;
  1057. *got_packet_ptr = 0;
  1058. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1059. av_free_packet(avpkt);
  1060. av_init_packet(avpkt);
  1061. return 0;
  1062. }
  1063. /* ensure that extended_data is properly set */
  1064. if (frame && !frame->extended_data) {
  1065. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1066. avctx->channels > AV_NUM_DATA_POINTERS) {
  1067. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1068. "with more than %d channels, but extended_data is not set.\n",
  1069. AV_NUM_DATA_POINTERS);
  1070. return AVERROR(EINVAL);
  1071. }
  1072. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1073. tmp = *frame;
  1074. tmp.extended_data = tmp.data;
  1075. frame = &tmp;
  1076. }
  1077. /* check for valid frame size */
  1078. if (frame) {
  1079. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1080. if (frame->nb_samples > avctx->frame_size) {
  1081. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1082. return AVERROR(EINVAL);
  1083. }
  1084. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1085. if (frame->nb_samples < avctx->frame_size &&
  1086. !avctx->internal->last_audio_frame) {
  1087. ret = pad_last_frame(avctx, &padded_frame, frame);
  1088. if (ret < 0)
  1089. return ret;
  1090. frame = padded_frame;
  1091. avctx->internal->last_audio_frame = 1;
  1092. }
  1093. if (frame->nb_samples != avctx->frame_size) {
  1094. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1095. ret = AVERROR(EINVAL);
  1096. goto end;
  1097. }
  1098. }
  1099. }
  1100. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1101. if (!ret) {
  1102. if (*got_packet_ptr) {
  1103. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1104. if (avpkt->pts == AV_NOPTS_VALUE)
  1105. avpkt->pts = frame->pts;
  1106. if (!avpkt->duration)
  1107. avpkt->duration = ff_samples_to_time_base(avctx,
  1108. frame->nb_samples);
  1109. }
  1110. avpkt->dts = avpkt->pts;
  1111. } else {
  1112. avpkt->size = 0;
  1113. }
  1114. }
  1115. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1116. needs_realloc = 0;
  1117. if (user_pkt.data) {
  1118. if (user_pkt.size >= avpkt->size) {
  1119. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1120. } else {
  1121. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1122. avpkt->size = user_pkt.size;
  1123. ret = -1;
  1124. }
  1125. avpkt->data = user_pkt.data;
  1126. avpkt->destruct = user_pkt.destruct;
  1127. } else {
  1128. if (av_dup_packet(avpkt) < 0) {
  1129. ret = AVERROR(ENOMEM);
  1130. }
  1131. }
  1132. }
  1133. if (!ret) {
  1134. if (needs_realloc && avpkt->data) {
  1135. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1136. if (new_data)
  1137. avpkt->data = new_data;
  1138. }
  1139. avctx->frame_number++;
  1140. }
  1141. if (ret < 0 || !*got_packet_ptr) {
  1142. av_free_packet(avpkt);
  1143. av_init_packet(avpkt);
  1144. goto end;
  1145. }
  1146. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1147. * this needs to be moved to the encoders, but for now we can do it
  1148. * here to simplify things */
  1149. avpkt->flags |= AV_PKT_FLAG_KEY;
  1150. end:
  1151. if (padded_frame) {
  1152. av_freep(&padded_frame->data[0]);
  1153. if (padded_frame->extended_data != padded_frame->data)
  1154. av_freep(&padded_frame->extended_data);
  1155. av_freep(&padded_frame);
  1156. }
  1157. return ret;
  1158. }
  1159. #if FF_API_OLD_ENCODE_AUDIO
  1160. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1161. uint8_t *buf, int buf_size,
  1162. const short *samples)
  1163. {
  1164. AVPacket pkt;
  1165. AVFrame frame0 = { 0 };
  1166. AVFrame *frame;
  1167. int ret, samples_size, got_packet;
  1168. av_init_packet(&pkt);
  1169. pkt.data = buf;
  1170. pkt.size = buf_size;
  1171. if (samples) {
  1172. frame = &frame0;
  1173. avcodec_get_frame_defaults(frame);
  1174. if (avctx->frame_size) {
  1175. frame->nb_samples = avctx->frame_size;
  1176. } else {
  1177. /* if frame_size is not set, the number of samples must be
  1178. * calculated from the buffer size */
  1179. int64_t nb_samples;
  1180. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1181. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1182. "support this codec\n");
  1183. return AVERROR(EINVAL);
  1184. }
  1185. nb_samples = (int64_t)buf_size * 8 /
  1186. (av_get_bits_per_sample(avctx->codec_id) *
  1187. avctx->channels);
  1188. if (nb_samples >= INT_MAX)
  1189. return AVERROR(EINVAL);
  1190. frame->nb_samples = nb_samples;
  1191. }
  1192. /* it is assumed that the samples buffer is large enough based on the
  1193. * relevant parameters */
  1194. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1195. frame->nb_samples,
  1196. avctx->sample_fmt, 1);
  1197. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1198. avctx->sample_fmt,
  1199. (const uint8_t *)samples,
  1200. samples_size, 1)))
  1201. return ret;
  1202. /* fabricate frame pts from sample count.
  1203. * this is needed because the avcodec_encode_audio() API does not have
  1204. * a way for the user to provide pts */
  1205. if (avctx->sample_rate && avctx->time_base.num)
  1206. frame->pts = ff_samples_to_time_base(avctx,
  1207. avctx->internal->sample_count);
  1208. else
  1209. frame->pts = AV_NOPTS_VALUE;
  1210. avctx->internal->sample_count += frame->nb_samples;
  1211. } else {
  1212. frame = NULL;
  1213. }
  1214. got_packet = 0;
  1215. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1216. if (!ret && got_packet && avctx->coded_frame) {
  1217. avctx->coded_frame->pts = pkt.pts;
  1218. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1219. }
  1220. /* free any side data since we cannot return it */
  1221. ff_packet_free_side_data(&pkt);
  1222. if (frame && frame->extended_data != frame->data)
  1223. av_freep(&frame->extended_data);
  1224. return ret ? ret : pkt.size;
  1225. }
  1226. #endif
  1227. #if FF_API_OLD_ENCODE_VIDEO
  1228. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1229. const AVFrame *pict)
  1230. {
  1231. AVPacket pkt;
  1232. int ret, got_packet = 0;
  1233. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1234. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1235. return -1;
  1236. }
  1237. av_init_packet(&pkt);
  1238. pkt.data = buf;
  1239. pkt.size = buf_size;
  1240. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1241. if (!ret && got_packet && avctx->coded_frame) {
  1242. avctx->coded_frame->pts = pkt.pts;
  1243. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1244. }
  1245. /* free any side data since we cannot return it */
  1246. if (pkt.side_data_elems > 0) {
  1247. int i;
  1248. for (i = 0; i < pkt.side_data_elems; i++)
  1249. av_free(pkt.side_data[i].data);
  1250. av_freep(&pkt.side_data);
  1251. pkt.side_data_elems = 0;
  1252. }
  1253. return ret ? ret : pkt.size;
  1254. }
  1255. #endif
  1256. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1257. AVPacket *avpkt,
  1258. const AVFrame *frame,
  1259. int *got_packet_ptr)
  1260. {
  1261. int ret;
  1262. AVPacket user_pkt = *avpkt;
  1263. int needs_realloc = !user_pkt.data;
  1264. *got_packet_ptr = 0;
  1265. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1266. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1267. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1268. av_free_packet(avpkt);
  1269. av_init_packet(avpkt);
  1270. avpkt->size = 0;
  1271. return 0;
  1272. }
  1273. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1274. return AVERROR(EINVAL);
  1275. av_assert0(avctx->codec->encode2);
  1276. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1277. av_assert0(ret <= 0);
  1278. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1279. needs_realloc = 0;
  1280. if (user_pkt.data) {
  1281. if (user_pkt.size >= avpkt->size) {
  1282. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1283. } else {
  1284. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1285. avpkt->size = user_pkt.size;
  1286. ret = -1;
  1287. }
  1288. avpkt->data = user_pkt.data;
  1289. avpkt->destruct = user_pkt.destruct;
  1290. } else {
  1291. if (av_dup_packet(avpkt) < 0) {
  1292. ret = AVERROR(ENOMEM);
  1293. }
  1294. }
  1295. }
  1296. if (!ret) {
  1297. if (!*got_packet_ptr)
  1298. avpkt->size = 0;
  1299. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1300. avpkt->pts = avpkt->dts = frame->pts;
  1301. if (needs_realloc && avpkt->data &&
  1302. avpkt->destruct == av_destruct_packet) {
  1303. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1304. if (new_data)
  1305. avpkt->data = new_data;
  1306. }
  1307. avctx->frame_number++;
  1308. }
  1309. if (ret < 0 || !*got_packet_ptr)
  1310. av_free_packet(avpkt);
  1311. emms_c();
  1312. return ret;
  1313. }
  1314. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1315. const AVSubtitle *sub)
  1316. {
  1317. int ret;
  1318. if (sub->start_display_time) {
  1319. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1320. return -1;
  1321. }
  1322. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1323. avctx->frame_number++;
  1324. return ret;
  1325. }
  1326. /**
  1327. * Attempt to guess proper monotonic timestamps for decoded video frames
  1328. * which might have incorrect times. Input timestamps may wrap around, in
  1329. * which case the output will as well.
  1330. *
  1331. * @param pts the pts field of the decoded AVPacket, as passed through
  1332. * AVFrame.pkt_pts
  1333. * @param dts the dts field of the decoded AVPacket
  1334. * @return one of the input values, may be AV_NOPTS_VALUE
  1335. */
  1336. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1337. int64_t reordered_pts, int64_t dts)
  1338. {
  1339. int64_t pts = AV_NOPTS_VALUE;
  1340. if (dts != AV_NOPTS_VALUE) {
  1341. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1342. ctx->pts_correction_last_dts = dts;
  1343. }
  1344. if (reordered_pts != AV_NOPTS_VALUE) {
  1345. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1346. ctx->pts_correction_last_pts = reordered_pts;
  1347. }
  1348. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1349. && reordered_pts != AV_NOPTS_VALUE)
  1350. pts = reordered_pts;
  1351. else
  1352. pts = dts;
  1353. return pts;
  1354. }
  1355. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1356. {
  1357. int size = 0;
  1358. const uint8_t *data;
  1359. uint32_t flags;
  1360. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1361. return;
  1362. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1363. if (!data || size < 4)
  1364. return;
  1365. flags = bytestream_get_le32(&data);
  1366. size -= 4;
  1367. if (size < 4) /* Required for any of the changes */
  1368. return;
  1369. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1370. avctx->channels = bytestream_get_le32(&data);
  1371. size -= 4;
  1372. }
  1373. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1374. if (size < 8)
  1375. return;
  1376. avctx->channel_layout = bytestream_get_le64(&data);
  1377. size -= 8;
  1378. }
  1379. if (size < 4)
  1380. return;
  1381. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1382. avctx->sample_rate = bytestream_get_le32(&data);
  1383. size -= 4;
  1384. }
  1385. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1386. if (size < 8)
  1387. return;
  1388. avctx->width = bytestream_get_le32(&data);
  1389. avctx->height = bytestream_get_le32(&data);
  1390. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1391. size -= 8;
  1392. }
  1393. }
  1394. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1395. {
  1396. int size, ret = 0;
  1397. const uint8_t *side_metadata;
  1398. const uint8_t *end;
  1399. av_dict_free(&avctx->metadata);
  1400. side_metadata = av_packet_get_side_data(avctx->pkt,
  1401. AV_PKT_DATA_STRINGS_METADATA, &size);
  1402. if (!side_metadata)
  1403. goto end;
  1404. end = side_metadata + size;
  1405. while (side_metadata < end) {
  1406. const uint8_t *key = side_metadata;
  1407. const uint8_t *val = side_metadata + strlen(key) + 1;
  1408. int ret = av_dict_set(&frame->metadata, key, val, 0);
  1409. if (ret < 0)
  1410. break;
  1411. side_metadata = val + strlen(val) + 1;
  1412. }
  1413. end:
  1414. avctx->metadata = frame->metadata;
  1415. return ret;
  1416. }
  1417. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1418. int *got_picture_ptr,
  1419. const AVPacket *avpkt)
  1420. {
  1421. int ret;
  1422. // copy to ensure we do not change avpkt
  1423. AVPacket tmp = *avpkt;
  1424. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1425. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1426. return AVERROR(EINVAL);
  1427. }
  1428. *got_picture_ptr = 0;
  1429. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1430. return AVERROR(EINVAL);
  1431. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1432. int did_split = av_packet_split_side_data(&tmp);
  1433. apply_param_change(avctx, &tmp);
  1434. avctx->pkt = &tmp;
  1435. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1436. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1437. &tmp);
  1438. else {
  1439. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1440. &tmp);
  1441. picture->pkt_dts = avpkt->dts;
  1442. if(!avctx->has_b_frames){
  1443. picture->pkt_pos = avpkt->pos;
  1444. }
  1445. //FIXME these should be under if(!avctx->has_b_frames)
  1446. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1447. if (!picture->width) picture->width = avctx->width;
  1448. if (!picture->height) picture->height = avctx->height;
  1449. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1450. }
  1451. add_metadata_from_side_data(avctx, picture);
  1452. emms_c(); //needed to avoid an emms_c() call before every return;
  1453. avctx->pkt = NULL;
  1454. if (did_split) {
  1455. ff_packet_free_side_data(&tmp);
  1456. if(ret == tmp.size)
  1457. ret = avpkt->size;
  1458. }
  1459. if (*got_picture_ptr){
  1460. avctx->frame_number++;
  1461. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1462. picture->pkt_pts,
  1463. picture->pkt_dts);
  1464. }
  1465. } else
  1466. ret = 0;
  1467. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1468. * make sure it's set correctly */
  1469. picture->extended_data = picture->data;
  1470. return ret;
  1471. }
  1472. #if FF_API_OLD_DECODE_AUDIO
  1473. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1474. int *frame_size_ptr,
  1475. AVPacket *avpkt)
  1476. {
  1477. AVFrame frame;
  1478. int ret, got_frame = 0;
  1479. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1480. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1481. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1482. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1483. "avcodec_decode_audio4()\n");
  1484. avctx->get_buffer = avcodec_default_get_buffer;
  1485. avctx->release_buffer = avcodec_default_release_buffer;
  1486. }
  1487. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1488. if (ret >= 0 && got_frame) {
  1489. int ch, plane_size;
  1490. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1491. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1492. frame.nb_samples,
  1493. avctx->sample_fmt, 1);
  1494. if (*frame_size_ptr < data_size) {
  1495. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1496. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1497. return AVERROR(EINVAL);
  1498. }
  1499. memcpy(samples, frame.extended_data[0], plane_size);
  1500. if (planar && avctx->channels > 1) {
  1501. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1502. for (ch = 1; ch < avctx->channels; ch++) {
  1503. memcpy(out, frame.extended_data[ch], plane_size);
  1504. out += plane_size;
  1505. }
  1506. }
  1507. *frame_size_ptr = data_size;
  1508. } else {
  1509. *frame_size_ptr = 0;
  1510. }
  1511. return ret;
  1512. }
  1513. #endif
  1514. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1515. AVFrame *frame,
  1516. int *got_frame_ptr,
  1517. const AVPacket *avpkt)
  1518. {
  1519. int planar, channels;
  1520. int ret = 0;
  1521. *got_frame_ptr = 0;
  1522. if (!avpkt->data && avpkt->size) {
  1523. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1524. return AVERROR(EINVAL);
  1525. }
  1526. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1527. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1528. return AVERROR(EINVAL);
  1529. }
  1530. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1531. uint8_t *side;
  1532. int side_size;
  1533. // copy to ensure we do not change avpkt
  1534. AVPacket tmp = *avpkt;
  1535. int did_split = av_packet_split_side_data(&tmp);
  1536. apply_param_change(avctx, &tmp);
  1537. avctx->pkt = &tmp;
  1538. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1539. if (ret >= 0 && *got_frame_ptr) {
  1540. avctx->frame_number++;
  1541. frame->pkt_dts = avpkt->dts;
  1542. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1543. frame->pkt_pts,
  1544. frame->pkt_dts);
  1545. if (frame->format == AV_SAMPLE_FMT_NONE)
  1546. frame->format = avctx->sample_fmt;
  1547. if (!frame->channel_layout)
  1548. frame->channel_layout = avctx->channel_layout;
  1549. if (!frame->channels)
  1550. frame->channels = avctx->channels;
  1551. if (!frame->sample_rate)
  1552. frame->sample_rate = avctx->sample_rate;
  1553. }
  1554. add_metadata_from_side_data(avctx, frame);
  1555. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1556. if(side && side_size>=10) {
  1557. avctx->internal->skip_samples = AV_RL32(side);
  1558. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1559. avctx->internal->skip_samples);
  1560. }
  1561. if (avctx->internal->skip_samples) {
  1562. if(frame->nb_samples <= avctx->internal->skip_samples){
  1563. *got_frame_ptr = 0;
  1564. avctx->internal->skip_samples -= frame->nb_samples;
  1565. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1566. avctx->internal->skip_samples);
  1567. } else {
  1568. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1569. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1570. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1571. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1572. (AVRational){1, avctx->sample_rate},
  1573. avctx->pkt_timebase);
  1574. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1575. frame->pkt_pts += diff_ts;
  1576. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1577. frame->pkt_dts += diff_ts;
  1578. if (frame->pkt_duration >= diff_ts)
  1579. frame->pkt_duration -= diff_ts;
  1580. } else {
  1581. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1582. }
  1583. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1584. avctx->internal->skip_samples, frame->nb_samples);
  1585. frame->nb_samples -= avctx->internal->skip_samples;
  1586. avctx->internal->skip_samples = 0;
  1587. }
  1588. }
  1589. avctx->pkt = NULL;
  1590. if (did_split) {
  1591. ff_packet_free_side_data(&tmp);
  1592. if(ret == tmp.size)
  1593. ret = avpkt->size;
  1594. }
  1595. }
  1596. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1597. * make sure it's set correctly; assume decoders that actually use
  1598. * extended_data are doing it correctly */
  1599. if (*got_frame_ptr) {
  1600. planar = av_sample_fmt_is_planar(frame->format);
  1601. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1602. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1603. frame->extended_data = frame->data;
  1604. } else {
  1605. frame->extended_data = NULL;
  1606. }
  1607. return ret;
  1608. }
  1609. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1610. int *got_sub_ptr,
  1611. AVPacket *avpkt)
  1612. {
  1613. int ret;
  1614. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1615. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1616. return AVERROR(EINVAL);
  1617. }
  1618. avctx->pkt = avpkt;
  1619. *got_sub_ptr = 0;
  1620. avcodec_get_subtitle_defaults(sub);
  1621. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1622. sub->pts = av_rescale_q(avpkt->pts,
  1623. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1624. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1625. if (*got_sub_ptr)
  1626. avctx->frame_number++;
  1627. return ret;
  1628. }
  1629. void avsubtitle_free(AVSubtitle *sub)
  1630. {
  1631. int i;
  1632. for (i = 0; i < sub->num_rects; i++) {
  1633. av_freep(&sub->rects[i]->pict.data[0]);
  1634. av_freep(&sub->rects[i]->pict.data[1]);
  1635. av_freep(&sub->rects[i]->pict.data[2]);
  1636. av_freep(&sub->rects[i]->pict.data[3]);
  1637. av_freep(&sub->rects[i]->text);
  1638. av_freep(&sub->rects[i]->ass);
  1639. av_freep(&sub->rects[i]);
  1640. }
  1641. av_freep(&sub->rects);
  1642. memset(sub, 0, sizeof(AVSubtitle));
  1643. }
  1644. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1645. {
  1646. int ret = 0;
  1647. entangled_thread_counter--;
  1648. /* Release any user-supplied mutex. */
  1649. if (ff_lockmgr_cb) {
  1650. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1651. }
  1652. ret = avcodec_close(avctx);
  1653. /* If there is a user-supplied mutex locking routine, call it. */
  1654. if (ff_lockmgr_cb) {
  1655. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1656. return -1;
  1657. }
  1658. entangled_thread_counter++;
  1659. return ret;
  1660. }
  1661. av_cold int avcodec_close(AVCodecContext *avctx)
  1662. {
  1663. /* If there is a user-supplied mutex locking routine, call it. */
  1664. if (ff_lockmgr_cb) {
  1665. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1666. return -1;
  1667. }
  1668. entangled_thread_counter++;
  1669. if (entangled_thread_counter != 1) {
  1670. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1671. entangled_thread_counter--;
  1672. return -1;
  1673. }
  1674. if (avcodec_is_open(avctx)) {
  1675. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1676. entangled_thread_counter --;
  1677. ff_frame_thread_encoder_free(avctx);
  1678. entangled_thread_counter ++;
  1679. }
  1680. if (HAVE_THREADS && avctx->thread_opaque)
  1681. ff_thread_free(avctx);
  1682. if (avctx->codec && avctx->codec->close)
  1683. avctx->codec->close(avctx);
  1684. avcodec_default_free_buffers(avctx);
  1685. avctx->coded_frame = NULL;
  1686. avctx->internal->byte_buffer_size = 0;
  1687. av_freep(&avctx->internal->byte_buffer);
  1688. av_freep(&avctx->internal);
  1689. av_dict_free(&avctx->metadata);
  1690. }
  1691. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1692. av_opt_free(avctx->priv_data);
  1693. av_opt_free(avctx);
  1694. av_freep(&avctx->priv_data);
  1695. if (av_codec_is_encoder(avctx->codec))
  1696. av_freep(&avctx->extradata);
  1697. avctx->codec = NULL;
  1698. avctx->active_thread_type = 0;
  1699. entangled_thread_counter--;
  1700. /* Release any user-supplied mutex. */
  1701. if (ff_lockmgr_cb) {
  1702. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1703. }
  1704. return 0;
  1705. }
  1706. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1707. {
  1708. switch(id){
  1709. //This is for future deprecatec codec ids, its empty since
  1710. //last major bump but will fill up again over time, please don't remove it
  1711. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1712. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1713. default : return id;
  1714. }
  1715. }
  1716. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1717. {
  1718. AVCodec *p, *experimental = NULL;
  1719. p = first_avcodec;
  1720. id= remap_deprecated_codec_id(id);
  1721. while (p) {
  1722. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1723. p->id == id) {
  1724. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1725. experimental = p;
  1726. } else
  1727. return p;
  1728. }
  1729. p = p->next;
  1730. }
  1731. return experimental;
  1732. }
  1733. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1734. {
  1735. return find_encdec(id, 1);
  1736. }
  1737. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1738. {
  1739. AVCodec *p;
  1740. if (!name)
  1741. return NULL;
  1742. p = first_avcodec;
  1743. while (p) {
  1744. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1745. return p;
  1746. p = p->next;
  1747. }
  1748. return NULL;
  1749. }
  1750. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1751. {
  1752. return find_encdec(id, 0);
  1753. }
  1754. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1755. {
  1756. AVCodec *p;
  1757. if (!name)
  1758. return NULL;
  1759. p = first_avcodec;
  1760. while (p) {
  1761. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1762. return p;
  1763. p = p->next;
  1764. }
  1765. return NULL;
  1766. }
  1767. const char *avcodec_get_name(enum AVCodecID id)
  1768. {
  1769. const AVCodecDescriptor *cd;
  1770. AVCodec *codec;
  1771. if (id == AV_CODEC_ID_NONE)
  1772. return "none";
  1773. cd = avcodec_descriptor_get(id);
  1774. if (cd)
  1775. return cd->name;
  1776. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1777. codec = avcodec_find_decoder(id);
  1778. if (codec)
  1779. return codec->name;
  1780. codec = avcodec_find_encoder(id);
  1781. if (codec)
  1782. return codec->name;
  1783. return "unknown_codec";
  1784. }
  1785. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1786. {
  1787. int i, len, ret = 0;
  1788. #define IS_PRINT(x) \
  1789. (((x) >= '0' && (x) <= '9') || \
  1790. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1791. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1792. for (i = 0; i < 4; i++) {
  1793. len = snprintf(buf, buf_size,
  1794. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1795. buf += len;
  1796. buf_size = buf_size > len ? buf_size - len : 0;
  1797. ret += len;
  1798. codec_tag >>= 8;
  1799. }
  1800. return ret;
  1801. }
  1802. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1803. {
  1804. const char *codec_type;
  1805. const char *codec_name;
  1806. const char *profile = NULL;
  1807. const AVCodec *p;
  1808. int bitrate;
  1809. AVRational display_aspect_ratio;
  1810. if (!buf || buf_size <= 0)
  1811. return;
  1812. codec_type = av_get_media_type_string(enc->codec_type);
  1813. codec_name = avcodec_get_name(enc->codec_id);
  1814. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1815. if (enc->codec)
  1816. p = enc->codec;
  1817. else
  1818. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1819. avcodec_find_decoder(enc->codec_id);
  1820. if (p)
  1821. profile = av_get_profile_name(p, enc->profile);
  1822. }
  1823. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1824. codec_name, enc->mb_decision ? " (hq)" : "");
  1825. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1826. if (profile)
  1827. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1828. if (enc->codec_tag) {
  1829. char tag_buf[32];
  1830. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1831. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1832. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1833. }
  1834. switch (enc->codec_type) {
  1835. case AVMEDIA_TYPE_VIDEO:
  1836. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1837. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1838. ", %s",
  1839. av_get_pix_fmt_name(enc->pix_fmt));
  1840. }
  1841. if (enc->width) {
  1842. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1843. ", %dx%d",
  1844. enc->width, enc->height);
  1845. if (enc->sample_aspect_ratio.num) {
  1846. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1847. enc->width * enc->sample_aspect_ratio.num,
  1848. enc->height * enc->sample_aspect_ratio.den,
  1849. 1024 * 1024);
  1850. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1851. " [SAR %d:%d DAR %d:%d]",
  1852. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1853. display_aspect_ratio.num, display_aspect_ratio.den);
  1854. }
  1855. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1856. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1857. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1858. ", %d/%d",
  1859. enc->time_base.num / g, enc->time_base.den / g);
  1860. }
  1861. }
  1862. if (encode) {
  1863. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1864. ", q=%d-%d", enc->qmin, enc->qmax);
  1865. }
  1866. break;
  1867. case AVMEDIA_TYPE_AUDIO:
  1868. if (enc->sample_rate) {
  1869. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1870. ", %d Hz", enc->sample_rate);
  1871. }
  1872. av_strlcat(buf, ", ", buf_size);
  1873. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1874. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1875. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1876. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1877. }
  1878. break;
  1879. default:
  1880. return;
  1881. }
  1882. if (encode) {
  1883. if (enc->flags & CODEC_FLAG_PASS1)
  1884. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1885. ", pass 1");
  1886. if (enc->flags & CODEC_FLAG_PASS2)
  1887. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1888. ", pass 2");
  1889. }
  1890. bitrate = get_bit_rate(enc);
  1891. if (bitrate != 0) {
  1892. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1893. ", %d kb/s", bitrate / 1000);
  1894. }
  1895. }
  1896. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1897. {
  1898. const AVProfile *p;
  1899. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1900. return NULL;
  1901. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1902. if (p->profile == profile)
  1903. return p->name;
  1904. return NULL;
  1905. }
  1906. unsigned avcodec_version(void)
  1907. {
  1908. // av_assert0(AV_CODEC_ID_V410==164);
  1909. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1910. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1911. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1912. av_assert0(AV_CODEC_ID_SRT==94216);
  1913. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1914. return LIBAVCODEC_VERSION_INT;
  1915. }
  1916. const char *avcodec_configuration(void)
  1917. {
  1918. return FFMPEG_CONFIGURATION;
  1919. }
  1920. const char *avcodec_license(void)
  1921. {
  1922. #define LICENSE_PREFIX "libavcodec license: "
  1923. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1924. }
  1925. void avcodec_flush_buffers(AVCodecContext *avctx)
  1926. {
  1927. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1928. ff_thread_flush(avctx);
  1929. else if (avctx->codec->flush)
  1930. avctx->codec->flush(avctx);
  1931. avctx->pts_correction_last_pts =
  1932. avctx->pts_correction_last_dts = INT64_MIN;
  1933. }
  1934. static void video_free_buffers(AVCodecContext *s)
  1935. {
  1936. AVCodecInternal *avci = s->internal;
  1937. int i, j;
  1938. if (!avci->buffer)
  1939. return;
  1940. if (avci->buffer_count)
  1941. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1942. avci->buffer_count);
  1943. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1944. InternalBuffer *buf = &avci->buffer[i];
  1945. for (j = 0; j < 4; j++) {
  1946. av_freep(&buf->base[j]);
  1947. buf->data[j] = NULL;
  1948. }
  1949. }
  1950. av_freep(&avci->buffer);
  1951. avci->buffer_count = 0;
  1952. }
  1953. static void audio_free_buffers(AVCodecContext *avctx)
  1954. {
  1955. AVCodecInternal *avci = avctx->internal;
  1956. InternalBuffer *buf;
  1957. if (!avci->buffer)
  1958. return;
  1959. buf = avci->buffer;
  1960. if (buf->extended_data) {
  1961. av_free(buf->extended_data[0]);
  1962. if (buf->extended_data != buf->data)
  1963. av_freep(&buf->extended_data);
  1964. }
  1965. av_freep(&avci->buffer);
  1966. }
  1967. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1968. {
  1969. switch (avctx->codec_type) {
  1970. case AVMEDIA_TYPE_VIDEO:
  1971. video_free_buffers(avctx);
  1972. break;
  1973. case AVMEDIA_TYPE_AUDIO:
  1974. audio_free_buffers(avctx);
  1975. break;
  1976. default:
  1977. break;
  1978. }
  1979. }
  1980. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1981. {
  1982. switch (codec_id) {
  1983. case AV_CODEC_ID_ADPCM_CT:
  1984. case AV_CODEC_ID_ADPCM_IMA_APC:
  1985. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1986. case AV_CODEC_ID_ADPCM_IMA_WS:
  1987. case AV_CODEC_ID_ADPCM_G722:
  1988. case AV_CODEC_ID_ADPCM_YAMAHA:
  1989. return 4;
  1990. case AV_CODEC_ID_PCM_ALAW:
  1991. case AV_CODEC_ID_PCM_MULAW:
  1992. case AV_CODEC_ID_PCM_S8:
  1993. case AV_CODEC_ID_PCM_U8:
  1994. case AV_CODEC_ID_PCM_ZORK:
  1995. return 8;
  1996. case AV_CODEC_ID_PCM_S16BE:
  1997. case AV_CODEC_ID_PCM_S16LE:
  1998. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1999. case AV_CODEC_ID_PCM_U16BE:
  2000. case AV_CODEC_ID_PCM_U16LE:
  2001. return 16;
  2002. case AV_CODEC_ID_PCM_S24DAUD:
  2003. case AV_CODEC_ID_PCM_S24BE:
  2004. case AV_CODEC_ID_PCM_S24LE:
  2005. case AV_CODEC_ID_PCM_U24BE:
  2006. case AV_CODEC_ID_PCM_U24LE:
  2007. return 24;
  2008. case AV_CODEC_ID_PCM_S32BE:
  2009. case AV_CODEC_ID_PCM_S32LE:
  2010. case AV_CODEC_ID_PCM_U32BE:
  2011. case AV_CODEC_ID_PCM_U32LE:
  2012. case AV_CODEC_ID_PCM_F32BE:
  2013. case AV_CODEC_ID_PCM_F32LE:
  2014. return 32;
  2015. case AV_CODEC_ID_PCM_F64BE:
  2016. case AV_CODEC_ID_PCM_F64LE:
  2017. return 64;
  2018. default:
  2019. return 0;
  2020. }
  2021. }
  2022. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2023. {
  2024. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2025. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2026. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2027. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2028. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2029. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2030. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2031. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2032. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2033. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2034. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2035. };
  2036. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2037. return AV_CODEC_ID_NONE;
  2038. if (be < 0 || be > 1)
  2039. be = AV_NE(1, 0);
  2040. return map[fmt][be];
  2041. }
  2042. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2043. {
  2044. switch (codec_id) {
  2045. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2046. return 2;
  2047. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2048. return 3;
  2049. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2050. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2051. case AV_CODEC_ID_ADPCM_IMA_QT:
  2052. case AV_CODEC_ID_ADPCM_SWF:
  2053. case AV_CODEC_ID_ADPCM_MS:
  2054. return 4;
  2055. default:
  2056. return av_get_exact_bits_per_sample(codec_id);
  2057. }
  2058. }
  2059. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2060. {
  2061. int id, sr, ch, ba, tag, bps;
  2062. id = avctx->codec_id;
  2063. sr = avctx->sample_rate;
  2064. ch = avctx->channels;
  2065. ba = avctx->block_align;
  2066. tag = avctx->codec_tag;
  2067. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2068. /* codecs with an exact constant bits per sample */
  2069. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2070. return (frame_bytes * 8LL) / (bps * ch);
  2071. bps = avctx->bits_per_coded_sample;
  2072. /* codecs with a fixed packet duration */
  2073. switch (id) {
  2074. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2075. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2076. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2077. case AV_CODEC_ID_AMR_NB:
  2078. case AV_CODEC_ID_GSM:
  2079. case AV_CODEC_ID_QCELP:
  2080. case AV_CODEC_ID_RA_288: return 160;
  2081. case AV_CODEC_ID_IMC: return 256;
  2082. case AV_CODEC_ID_AMR_WB:
  2083. case AV_CODEC_ID_GSM_MS: return 320;
  2084. case AV_CODEC_ID_MP1: return 384;
  2085. case AV_CODEC_ID_ATRAC1: return 512;
  2086. case AV_CODEC_ID_ATRAC3: return 1024;
  2087. case AV_CODEC_ID_MP2:
  2088. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2089. case AV_CODEC_ID_AC3: return 1536;
  2090. }
  2091. if (sr > 0) {
  2092. /* calc from sample rate */
  2093. if (id == AV_CODEC_ID_TTA)
  2094. return 256 * sr / 245;
  2095. if (ch > 0) {
  2096. /* calc from sample rate and channels */
  2097. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2098. return (480 << (sr / 22050)) / ch;
  2099. }
  2100. }
  2101. if (ba > 0) {
  2102. /* calc from block_align */
  2103. if (id == AV_CODEC_ID_SIPR) {
  2104. switch (ba) {
  2105. case 20: return 160;
  2106. case 19: return 144;
  2107. case 29: return 288;
  2108. case 37: return 480;
  2109. }
  2110. } else if (id == AV_CODEC_ID_ILBC) {
  2111. switch (ba) {
  2112. case 38: return 160;
  2113. case 50: return 240;
  2114. }
  2115. }
  2116. }
  2117. if (frame_bytes > 0) {
  2118. /* calc from frame_bytes only */
  2119. if (id == AV_CODEC_ID_TRUESPEECH)
  2120. return 240 * (frame_bytes / 32);
  2121. if (id == AV_CODEC_ID_NELLYMOSER)
  2122. return 256 * (frame_bytes / 64);
  2123. if (id == AV_CODEC_ID_RA_144)
  2124. return 160 * (frame_bytes / 20);
  2125. if (bps > 0) {
  2126. /* calc from frame_bytes and bits_per_coded_sample */
  2127. if (id == AV_CODEC_ID_ADPCM_G726)
  2128. return frame_bytes * 8 / bps;
  2129. }
  2130. if (ch > 0) {
  2131. /* calc from frame_bytes and channels */
  2132. switch (id) {
  2133. case AV_CODEC_ID_ADPCM_4XM:
  2134. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2135. return (frame_bytes - 4 * ch) * 2 / ch;
  2136. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2137. return (frame_bytes - 4) * 2 / ch;
  2138. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2139. return (frame_bytes - 8) * 2 / ch;
  2140. case AV_CODEC_ID_ADPCM_XA:
  2141. return (frame_bytes / 128) * 224 / ch;
  2142. case AV_CODEC_ID_INTERPLAY_DPCM:
  2143. return (frame_bytes - 6 - ch) / ch;
  2144. case AV_CODEC_ID_ROQ_DPCM:
  2145. return (frame_bytes - 8) / ch;
  2146. case AV_CODEC_ID_XAN_DPCM:
  2147. return (frame_bytes - 2 * ch) / ch;
  2148. case AV_CODEC_ID_MACE3:
  2149. return 3 * frame_bytes / ch;
  2150. case AV_CODEC_ID_MACE6:
  2151. return 6 * frame_bytes / ch;
  2152. case AV_CODEC_ID_PCM_LXF:
  2153. return 2 * (frame_bytes / (5 * ch));
  2154. }
  2155. if (tag) {
  2156. /* calc from frame_bytes, channels, and codec_tag */
  2157. if (id == AV_CODEC_ID_SOL_DPCM) {
  2158. if (tag == 3)
  2159. return frame_bytes / ch;
  2160. else
  2161. return frame_bytes * 2 / ch;
  2162. }
  2163. }
  2164. if (ba > 0) {
  2165. /* calc from frame_bytes, channels, and block_align */
  2166. int blocks = frame_bytes / ba;
  2167. switch (avctx->codec_id) {
  2168. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2169. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2170. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2171. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2172. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2173. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2174. case AV_CODEC_ID_ADPCM_MS:
  2175. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2176. }
  2177. }
  2178. if (bps > 0) {
  2179. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2180. switch (avctx->codec_id) {
  2181. case AV_CODEC_ID_PCM_DVD:
  2182. if(bps<4)
  2183. return 0;
  2184. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2185. case AV_CODEC_ID_PCM_BLURAY:
  2186. if(bps<4)
  2187. return 0;
  2188. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2189. case AV_CODEC_ID_S302M:
  2190. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2191. }
  2192. }
  2193. }
  2194. }
  2195. return 0;
  2196. }
  2197. #if !HAVE_THREADS
  2198. int ff_thread_init(AVCodecContext *s)
  2199. {
  2200. return -1;
  2201. }
  2202. #endif
  2203. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2204. {
  2205. unsigned int n = 0;
  2206. while (v >= 0xff) {
  2207. *s++ = 0xff;
  2208. v -= 0xff;
  2209. n++;
  2210. }
  2211. *s = v;
  2212. n++;
  2213. return n;
  2214. }
  2215. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2216. {
  2217. int i;
  2218. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2219. return i;
  2220. }
  2221. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2222. {
  2223. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2224. "version to the newest one from Git. If the problem still "
  2225. "occurs, it means that your file has a feature which has not "
  2226. "been implemented.\n", feature);
  2227. if(want_sample)
  2228. av_log_ask_for_sample(avc, NULL);
  2229. }
  2230. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2231. {
  2232. va_list argument_list;
  2233. va_start(argument_list, msg);
  2234. if (msg)
  2235. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2236. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2237. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2238. "and contact the ffmpeg-devel mailing list.\n");
  2239. va_end(argument_list);
  2240. }
  2241. static AVHWAccel *first_hwaccel = NULL;
  2242. void av_register_hwaccel(AVHWAccel *hwaccel)
  2243. {
  2244. AVHWAccel **p = &first_hwaccel;
  2245. while (*p)
  2246. p = &(*p)->next;
  2247. *p = hwaccel;
  2248. hwaccel->next = NULL;
  2249. }
  2250. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2251. {
  2252. return hwaccel ? hwaccel->next : first_hwaccel;
  2253. }
  2254. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2255. {
  2256. AVHWAccel *hwaccel = NULL;
  2257. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2258. if (hwaccel->id == codec_id
  2259. && hwaccel->pix_fmt == pix_fmt)
  2260. return hwaccel;
  2261. return NULL;
  2262. }
  2263. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2264. {
  2265. if (ff_lockmgr_cb) {
  2266. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2267. return -1;
  2268. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2269. return -1;
  2270. }
  2271. ff_lockmgr_cb = cb;
  2272. if (ff_lockmgr_cb) {
  2273. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2274. return -1;
  2275. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2276. return -1;
  2277. }
  2278. return 0;
  2279. }
  2280. int avpriv_lock_avformat(void)
  2281. {
  2282. if (ff_lockmgr_cb) {
  2283. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2284. return -1;
  2285. }
  2286. return 0;
  2287. }
  2288. int avpriv_unlock_avformat(void)
  2289. {
  2290. if (ff_lockmgr_cb) {
  2291. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2292. return -1;
  2293. }
  2294. return 0;
  2295. }
  2296. unsigned int avpriv_toupper4(unsigned int x)
  2297. {
  2298. return toupper(x & 0xFF)
  2299. + (toupper((x >> 8) & 0xFF) << 8)
  2300. + (toupper((x >> 16) & 0xFF) << 16)
  2301. + (toupper((x >> 24) & 0xFF) << 24);
  2302. }
  2303. #if !HAVE_THREADS
  2304. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2305. {
  2306. f->owner = avctx;
  2307. ff_init_buffer_info(avctx, f);
  2308. return avctx->get_buffer(avctx, f);
  2309. }
  2310. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2311. {
  2312. f->owner->release_buffer(f->owner, f);
  2313. }
  2314. void ff_thread_finish_setup(AVCodecContext *avctx)
  2315. {
  2316. }
  2317. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2318. {
  2319. }
  2320. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2321. {
  2322. }
  2323. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2324. {
  2325. return 1;
  2326. }
  2327. #endif
  2328. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2329. {
  2330. AVCodec *c= avcodec_find_decoder(codec_id);
  2331. if(!c)
  2332. c= avcodec_find_encoder(codec_id);
  2333. if(c)
  2334. return c->type;
  2335. if (codec_id <= AV_CODEC_ID_NONE)
  2336. return AVMEDIA_TYPE_UNKNOWN;
  2337. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2338. return AVMEDIA_TYPE_VIDEO;
  2339. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2340. return AVMEDIA_TYPE_AUDIO;
  2341. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2342. return AVMEDIA_TYPE_SUBTITLE;
  2343. return AVMEDIA_TYPE_UNKNOWN;
  2344. }
  2345. int avcodec_is_open(AVCodecContext *s)
  2346. {
  2347. return !!s->internal;
  2348. }