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.

2576 lines
82KB

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