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 "imgconvert.h"
  40. #include "thread.h"
  41. #include "frame_thread_encoder.h"
  42. #include "audioconvert.h"
  43. #include "internal.h"
  44. #include "bytestream.h"
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <limits.h>
  48. #include <float.h>
  49. static int volatile entangled_thread_counter = 0;
  50. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  51. static void *codec_mutex;
  52. static void *avformat_mutex;
  53. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  54. {
  55. if (min_size < *size)
  56. return ptr;
  57. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  58. ptr = av_realloc(ptr, min_size);
  59. /* we could set this to the unmodified min_size but this is safer
  60. * if the user lost the ptr and uses NULL now
  61. */
  62. if (!ptr)
  63. min_size = 0;
  64. *size = min_size;
  65. return ptr;
  66. }
  67. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  68. {
  69. void **p = ptr;
  70. if (min_size < *size)
  71. return 0;
  72. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  73. av_free(*p);
  74. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  75. if (!*p)
  76. min_size = 0;
  77. *size = min_size;
  78. return 1;
  79. }
  80. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  81. {
  82. ff_fast_malloc(ptr, size, min_size, 0);
  83. }
  84. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  85. {
  86. uint8_t **p = ptr;
  87. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  88. av_freep(p);
  89. *size = 0;
  90. return;
  91. }
  92. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  93. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  94. }
  95. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  96. {
  97. uint8_t **p = ptr;
  98. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  99. av_freep(p);
  100. *size = 0;
  101. return;
  102. }
  103. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  104. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  105. }
  106. /* encoder management */
  107. static AVCodec *first_avcodec = NULL;
  108. AVCodec *av_codec_next(const AVCodec *c)
  109. {
  110. if (c)
  111. return c->next;
  112. else
  113. return first_avcodec;
  114. }
  115. static void avcodec_init(void)
  116. {
  117. static int initialized = 0;
  118. if (initialized != 0)
  119. return;
  120. initialized = 1;
  121. ff_dsputil_static_init();
  122. }
  123. int av_codec_is_encoder(const AVCodec *codec)
  124. {
  125. return codec && (codec->encode_sub || codec->encode2);
  126. }
  127. int av_codec_is_decoder(const AVCodec *codec)
  128. {
  129. return codec && codec->decode;
  130. }
  131. void avcodec_register(AVCodec *codec)
  132. {
  133. AVCodec **p;
  134. avcodec_init();
  135. p = &first_avcodec;
  136. while (*p != NULL)
  137. p = &(*p)->next;
  138. *p = codec;
  139. codec->next = NULL;
  140. if (codec->init_static_data)
  141. codec->init_static_data(codec);
  142. }
  143. unsigned avcodec_get_edge_width(void)
  144. {
  145. return EDGE_WIDTH;
  146. }
  147. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  148. {
  149. s->coded_width = width;
  150. s->coded_height = height;
  151. s->width = -((-width ) >> s->lowres);
  152. s->height = -((-height) >> s->lowres);
  153. }
  154. #define INTERNAL_BUFFER_SIZE (32 + 1)
  155. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  156. int linesize_align[AV_NUM_DATA_POINTERS])
  157. {
  158. int i;
  159. int w_align = 1;
  160. int h_align = 1;
  161. switch (s->pix_fmt) {
  162. case PIX_FMT_YUV420P:
  163. case PIX_FMT_YUYV422:
  164. case PIX_FMT_UYVY422:
  165. case PIX_FMT_YUV422P:
  166. case PIX_FMT_YUV440P:
  167. case PIX_FMT_YUV444P:
  168. case PIX_FMT_GBRP:
  169. case PIX_FMT_GRAY8:
  170. case PIX_FMT_GRAY16BE:
  171. case PIX_FMT_GRAY16LE:
  172. case PIX_FMT_YUVJ420P:
  173. case PIX_FMT_YUVJ422P:
  174. case PIX_FMT_YUVJ440P:
  175. case PIX_FMT_YUVJ444P:
  176. case PIX_FMT_YUVA420P:
  177. case PIX_FMT_YUVA422P:
  178. case PIX_FMT_YUVA444P:
  179. case PIX_FMT_YUV420P9LE:
  180. case PIX_FMT_YUV420P9BE:
  181. case PIX_FMT_YUV420P10LE:
  182. case PIX_FMT_YUV420P10BE:
  183. case PIX_FMT_YUV420P12LE:
  184. case PIX_FMT_YUV420P12BE:
  185. case PIX_FMT_YUV420P14LE:
  186. case PIX_FMT_YUV420P14BE:
  187. case PIX_FMT_YUV422P9LE:
  188. case PIX_FMT_YUV422P9BE:
  189. case PIX_FMT_YUV422P10LE:
  190. case PIX_FMT_YUV422P10BE:
  191. case PIX_FMT_YUV422P12LE:
  192. case PIX_FMT_YUV422P12BE:
  193. case PIX_FMT_YUV422P14LE:
  194. case PIX_FMT_YUV422P14BE:
  195. case PIX_FMT_YUV444P9LE:
  196. case PIX_FMT_YUV444P9BE:
  197. case PIX_FMT_YUV444P10LE:
  198. case PIX_FMT_YUV444P10BE:
  199. case PIX_FMT_YUV444P12LE:
  200. case PIX_FMT_YUV444P12BE:
  201. case PIX_FMT_YUV444P14LE:
  202. case PIX_FMT_YUV444P14BE:
  203. case PIX_FMT_GBRP9LE:
  204. case PIX_FMT_GBRP9BE:
  205. case PIX_FMT_GBRP10LE:
  206. case PIX_FMT_GBRP10BE:
  207. case PIX_FMT_GBRP12LE:
  208. case PIX_FMT_GBRP12BE:
  209. case PIX_FMT_GBRP14LE:
  210. case PIX_FMT_GBRP14BE:
  211. w_align = 16; //FIXME assume 16 pixel per macroblock
  212. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  213. break;
  214. case PIX_FMT_YUV411P:
  215. case PIX_FMT_UYYVYY411:
  216. w_align = 32;
  217. h_align = 8;
  218. break;
  219. case PIX_FMT_YUV410P:
  220. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  221. w_align = 64;
  222. h_align = 64;
  223. }
  224. case PIX_FMT_RGB555:
  225. if (s->codec_id == AV_CODEC_ID_RPZA) {
  226. w_align = 4;
  227. h_align = 4;
  228. }
  229. case PIX_FMT_PAL8:
  230. case PIX_FMT_BGR8:
  231. case PIX_FMT_RGB8:
  232. if (s->codec_id == AV_CODEC_ID_SMC) {
  233. w_align = 4;
  234. h_align = 4;
  235. }
  236. break;
  237. case PIX_FMT_BGR24:
  238. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  239. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  240. w_align = 4;
  241. h_align = 4;
  242. }
  243. break;
  244. default:
  245. w_align = 1;
  246. h_align = 1;
  247. break;
  248. }
  249. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  250. w_align = FFMAX(w_align, 8);
  251. }
  252. *width = FFALIGN(*width, w_align);
  253. *height = FFALIGN(*height, h_align);
  254. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  255. // some of the optimized chroma MC reads one line too much
  256. // which is also done in mpeg decoders with lowres > 0
  257. *height += 2;
  258. for (i = 0; i < 4; i++)
  259. linesize_align[i] = STRIDE_ALIGN;
  260. }
  261. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  262. {
  263. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  264. int linesize_align[AV_NUM_DATA_POINTERS];
  265. int align;
  266. avcodec_align_dimensions2(s, width, height, linesize_align);
  267. align = FFMAX(linesize_align[0], linesize_align[3]);
  268. linesize_align[1] <<= chroma_shift;
  269. linesize_align[2] <<= chroma_shift;
  270. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  271. *width = FFALIGN(*width, align);
  272. }
  273. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  274. {
  275. if (s->pkt) {
  276. frame->pkt_pts = s->pkt->pts;
  277. frame->pkt_pos = s->pkt->pos;
  278. frame->pkt_duration = s->pkt->duration;
  279. } else {
  280. frame->pkt_pts = AV_NOPTS_VALUE;
  281. frame->pkt_pos = -1;
  282. frame->pkt_duration = 0;
  283. }
  284. frame->reordered_opaque = s->reordered_opaque;
  285. switch (s->codec->type) {
  286. case AVMEDIA_TYPE_VIDEO:
  287. frame->width = s->width;
  288. frame->height = s->height;
  289. frame->format = s->pix_fmt;
  290. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  291. break;
  292. case AVMEDIA_TYPE_AUDIO:
  293. frame->sample_rate = s->sample_rate;
  294. frame->format = s->sample_fmt;
  295. frame->channel_layout = s->channel_layout;
  296. frame->channels = s->channels;
  297. break;
  298. }
  299. }
  300. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  301. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  302. int buf_size, int align)
  303. {
  304. int ch, planar, needed_size, ret = 0;
  305. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  306. frame->nb_samples, sample_fmt,
  307. align);
  308. if (buf_size < needed_size)
  309. return AVERROR(EINVAL);
  310. planar = av_sample_fmt_is_planar(sample_fmt);
  311. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  312. if (!(frame->extended_data = av_mallocz(nb_channels *
  313. sizeof(*frame->extended_data))))
  314. return AVERROR(ENOMEM);
  315. } else {
  316. frame->extended_data = frame->data;
  317. }
  318. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  319. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  320. sample_fmt, align)) < 0) {
  321. if (frame->extended_data != frame->data)
  322. av_freep(&frame->extended_data);
  323. return ret;
  324. }
  325. if (frame->extended_data != frame->data) {
  326. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  327. frame->data[ch] = frame->extended_data[ch];
  328. }
  329. return ret;
  330. }
  331. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  332. {
  333. AVCodecInternal *avci = avctx->internal;
  334. InternalBuffer *buf;
  335. int buf_size, ret;
  336. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  337. frame->nb_samples, avctx->sample_fmt,
  338. 0);
  339. if (buf_size < 0)
  340. return AVERROR(EINVAL);
  341. /* allocate InternalBuffer if needed */
  342. if (!avci->buffer) {
  343. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  344. if (!avci->buffer)
  345. return AVERROR(ENOMEM);
  346. }
  347. buf = avci->buffer;
  348. /* if there is a previously-used internal buffer, check its size and
  349. * channel count to see if we can reuse it */
  350. if (buf->extended_data) {
  351. /* if current buffer is too small, free it */
  352. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  353. av_free(buf->extended_data[0]);
  354. if (buf->extended_data != buf->data)
  355. av_freep(&buf->extended_data);
  356. buf->extended_data = NULL;
  357. buf->data[0] = NULL;
  358. }
  359. /* if number of channels has changed, reset and/or free extended data
  360. * pointers but leave data buffer in buf->data[0] for reuse */
  361. if (buf->nb_channels != avctx->channels) {
  362. if (buf->extended_data != buf->data)
  363. av_free(buf->extended_data);
  364. buf->extended_data = NULL;
  365. }
  366. }
  367. /* if there is no previous buffer or the previous buffer cannot be used
  368. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  369. if (!buf->extended_data) {
  370. if (!buf->data[0]) {
  371. if (!(buf->data[0] = av_mallocz(buf_size)))
  372. return AVERROR(ENOMEM);
  373. buf->audio_data_size = buf_size;
  374. }
  375. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  376. avctx->sample_fmt, buf->data[0],
  377. buf->audio_data_size, 0)))
  378. return ret;
  379. if (frame->extended_data == frame->data)
  380. buf->extended_data = buf->data;
  381. else
  382. buf->extended_data = frame->extended_data;
  383. memcpy(buf->data, frame->data, sizeof(frame->data));
  384. buf->linesize[0] = frame->linesize[0];
  385. buf->nb_channels = avctx->channels;
  386. } else {
  387. /* copy InternalBuffer info to the AVFrame */
  388. frame->extended_data = buf->extended_data;
  389. frame->linesize[0] = buf->linesize[0];
  390. memcpy(frame->data, buf->data, sizeof(frame->data));
  391. }
  392. frame->type = FF_BUFFER_TYPE_INTERNAL;
  393. ff_init_buffer_info(avctx, frame);
  394. if (avctx->debug & FF_DEBUG_BUFFERS)
  395. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  396. "internal audio buffer used\n", frame);
  397. return 0;
  398. }
  399. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  400. {
  401. int i;
  402. int w = s->width;
  403. int h = s->height;
  404. InternalBuffer *buf;
  405. AVCodecInternal *avci = s->internal;
  406. if (pic->data[0] != NULL) {
  407. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  408. return -1;
  409. }
  410. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  411. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  412. return -1;
  413. }
  414. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  415. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  416. return -1;
  417. }
  418. if (!avci->buffer) {
  419. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  420. sizeof(InternalBuffer));
  421. }
  422. buf = &avci->buffer[avci->buffer_count];
  423. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  424. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  425. av_freep(&buf->base[i]);
  426. buf->data[i] = NULL;
  427. }
  428. }
  429. if (!buf->base[0]) {
  430. int h_chroma_shift, v_chroma_shift;
  431. int size[4] = { 0 };
  432. int tmpsize;
  433. int unaligned;
  434. AVPicture picture;
  435. int stride_align[AV_NUM_DATA_POINTERS];
  436. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1 + 1;
  437. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  438. avcodec_align_dimensions2(s, &w, &h, stride_align);
  439. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  440. w += EDGE_WIDTH * 2;
  441. h += EDGE_WIDTH * 2;
  442. }
  443. do {
  444. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  445. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  446. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  447. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  448. w += w & ~(w - 1);
  449. unaligned = 0;
  450. for (i = 0; i < 4; i++)
  451. unaligned |= picture.linesize[i] % stride_align[i];
  452. } while (unaligned);
  453. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  454. if (tmpsize < 0)
  455. return -1;
  456. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  457. size[i] = picture.data[i + 1] - picture.data[i];
  458. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  459. memset(buf->base, 0, sizeof(buf->base));
  460. memset(buf->data, 0, sizeof(buf->data));
  461. for (i = 0; i < 4 && size[i]; i++) {
  462. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  463. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  464. buf->linesize[i] = picture.linesize[i];
  465. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  466. if (buf->base[i] == NULL)
  467. return AVERROR(ENOMEM);
  468. memset(buf->base[i], 128, size[i]);
  469. // no edge if EDGE EMU or not planar YUV
  470. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  471. buf->data[i] = buf->base[i];
  472. else
  473. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  474. }
  475. for (; i < AV_NUM_DATA_POINTERS; i++) {
  476. buf->base[i] = buf->data[i] = NULL;
  477. buf->linesize[i] = 0;
  478. }
  479. if (size[1] && !size[2])
  480. ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  481. buf->width = s->width;
  482. buf->height = s->height;
  483. buf->pix_fmt = s->pix_fmt;
  484. }
  485. pic->type = FF_BUFFER_TYPE_INTERNAL;
  486. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  487. pic->base[i] = buf->base[i];
  488. pic->data[i] = buf->data[i];
  489. pic->linesize[i] = buf->linesize[i];
  490. }
  491. pic->extended_data = pic->data;
  492. avci->buffer_count++;
  493. pic->width = buf->width;
  494. pic->height = buf->height;
  495. pic->format = buf->pix_fmt;
  496. ff_init_buffer_info(s, pic);
  497. if (s->debug & FF_DEBUG_BUFFERS)
  498. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  499. "buffers used\n", pic, avci->buffer_count);
  500. return 0;
  501. }
  502. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  503. {
  504. switch (avctx->codec_type) {
  505. case AVMEDIA_TYPE_VIDEO:
  506. return video_get_buffer(avctx, frame);
  507. case AVMEDIA_TYPE_AUDIO:
  508. return audio_get_buffer(avctx, frame);
  509. default:
  510. return -1;
  511. }
  512. }
  513. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  514. {
  515. int i;
  516. InternalBuffer *buf, *last;
  517. AVCodecInternal *avci = s->internal;
  518. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  519. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  520. assert(avci->buffer_count);
  521. if (avci->buffer) {
  522. buf = NULL; /* avoids warning */
  523. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  524. buf = &avci->buffer[i];
  525. if (buf->data[0] == pic->data[0])
  526. break;
  527. }
  528. av_assert0(i < avci->buffer_count);
  529. avci->buffer_count--;
  530. last = &avci->buffer[avci->buffer_count];
  531. if (buf != last)
  532. FFSWAP(InternalBuffer, *buf, *last);
  533. }
  534. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  535. pic->data[i] = NULL;
  536. // pic->base[i]=NULL;
  537. //printf("R%X\n", pic->opaque);
  538. if (s->debug & FF_DEBUG_BUFFERS)
  539. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  540. "buffers used\n", pic, avci->buffer_count);
  541. }
  542. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  543. {
  544. AVFrame temp_pic;
  545. int i;
  546. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  547. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  548. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  549. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  550. s->release_buffer(s, pic);
  551. }
  552. ff_init_buffer_info(s, pic);
  553. /* If no picture return a new buffer */
  554. if (pic->data[0] == NULL) {
  555. /* We will copy from buffer, so must be readable */
  556. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  557. return s->get_buffer(s, pic);
  558. }
  559. assert(s->pix_fmt == pic->format);
  560. /* If internal buffer type return the same buffer */
  561. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  562. return 0;
  563. }
  564. /*
  565. * Not internal type and reget_buffer not overridden, emulate cr buffer
  566. */
  567. temp_pic = *pic;
  568. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  569. pic->data[i] = pic->base[i] = NULL;
  570. pic->opaque = NULL;
  571. /* Allocate new frame */
  572. if (s->get_buffer(s, pic))
  573. return -1;
  574. /* Copy image data from old buffer to new buffer */
  575. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  576. s->height);
  577. s->release_buffer(s, &temp_pic); // Release old frame
  578. return 0;
  579. }
  580. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  581. {
  582. int i;
  583. for (i = 0; i < count; i++) {
  584. int r = func(c, (char *)arg + i * size);
  585. if (ret)
  586. ret[i] = r;
  587. }
  588. return 0;
  589. }
  590. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  591. {
  592. int i;
  593. for (i = 0; i < count; i++) {
  594. int r = func(c, arg, i, 0);
  595. if (ret)
  596. ret[i] = r;
  597. }
  598. return 0;
  599. }
  600. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt)
  601. {
  602. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  603. ++fmt;
  604. return fmt[0];
  605. }
  606. void avcodec_get_frame_defaults(AVFrame *frame)
  607. {
  608. #if LIBAVCODEC_VERSION_MAJOR >= 55
  609. // extended_data should explicitly be freed when needed, this code is unsafe currently
  610. // also this is not compatible to the <55 ABI/API
  611. if (frame->extended_data != frame->data && 0)
  612. av_freep(&frame->extended_data);
  613. #endif
  614. memset(frame, 0, sizeof(AVFrame));
  615. frame->pts =
  616. frame->pkt_dts =
  617. frame->pkt_pts =
  618. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  619. frame->pkt_duration = 0;
  620. frame->pkt_pos = -1;
  621. frame->key_frame = 1;
  622. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  623. frame->format = -1; /* unknown */
  624. frame->extended_data = frame->data;
  625. }
  626. AVFrame *avcodec_alloc_frame(void)
  627. {
  628. AVFrame *frame = av_malloc(sizeof(AVFrame));
  629. if (frame == NULL)
  630. return NULL;
  631. frame->extended_data = NULL;
  632. avcodec_get_frame_defaults(frame);
  633. return frame;
  634. }
  635. void avcodec_free_frame(AVFrame **frame)
  636. {
  637. AVFrame *f;
  638. if (!frame || !*frame)
  639. return;
  640. f = *frame;
  641. if (f->extended_data != f->data)
  642. av_freep(&f->extended_data);
  643. av_freep(frame);
  644. }
  645. #define MAKE_ACCESSORS(str, name, type, field) \
  646. type av_##name##_get_##field(const str *s) { return s->field; } \
  647. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  648. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  649. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  650. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  651. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  652. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  653. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  654. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  655. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  656. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  657. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  658. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  659. {
  660. memset(sub, 0, sizeof(*sub));
  661. sub->pts = AV_NOPTS_VALUE;
  662. }
  663. static int get_bit_rate(AVCodecContext *ctx)
  664. {
  665. int bit_rate;
  666. int bits_per_sample;
  667. switch (ctx->codec_type) {
  668. case AVMEDIA_TYPE_VIDEO:
  669. case AVMEDIA_TYPE_DATA:
  670. case AVMEDIA_TYPE_SUBTITLE:
  671. case AVMEDIA_TYPE_ATTACHMENT:
  672. bit_rate = ctx->bit_rate;
  673. break;
  674. case AVMEDIA_TYPE_AUDIO:
  675. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  676. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  677. break;
  678. default:
  679. bit_rate = 0;
  680. break;
  681. }
  682. return bit_rate;
  683. }
  684. #if FF_API_AVCODEC_OPEN
  685. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  686. {
  687. return avcodec_open2(avctx, codec, NULL);
  688. }
  689. #endif
  690. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  691. {
  692. int ret = 0;
  693. AVDictionary *tmp = NULL;
  694. if (avcodec_is_open(avctx))
  695. return 0;
  696. if ((!codec && !avctx->codec)) {
  697. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  698. return AVERROR(EINVAL);
  699. }
  700. if ((codec && avctx->codec && codec != avctx->codec)) {
  701. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  702. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  703. return AVERROR(EINVAL);
  704. }
  705. if (!codec)
  706. codec = avctx->codec;
  707. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  708. return AVERROR(EINVAL);
  709. if (options)
  710. av_dict_copy(&tmp, *options, 0);
  711. /* If there is a user-supplied mutex locking routine, call it. */
  712. if (ff_lockmgr_cb) {
  713. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  714. return -1;
  715. }
  716. entangled_thread_counter++;
  717. if (entangled_thread_counter != 1) {
  718. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  719. ret = -1;
  720. goto end;
  721. }
  722. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  723. if (!avctx->internal) {
  724. ret = AVERROR(ENOMEM);
  725. goto end;
  726. }
  727. if (codec->priv_data_size > 0) {
  728. if (!avctx->priv_data) {
  729. avctx->priv_data = av_mallocz(codec->priv_data_size);
  730. if (!avctx->priv_data) {
  731. ret = AVERROR(ENOMEM);
  732. goto end;
  733. }
  734. if (codec->priv_class) {
  735. *(const AVClass **)avctx->priv_data = codec->priv_class;
  736. av_opt_set_defaults(avctx->priv_data);
  737. }
  738. }
  739. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  740. goto free_and_end;
  741. } else {
  742. avctx->priv_data = NULL;
  743. }
  744. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  745. goto free_and_end;
  746. if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
  747. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  748. av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
  749. ret = -1;
  750. goto free_and_end;
  751. }
  752. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  753. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  754. if (avctx->coded_width && avctx->coded_height)
  755. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  756. else if (avctx->width && avctx->height)
  757. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  758. }
  759. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  760. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  761. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  762. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  763. avcodec_set_dimensions(avctx, 0, 0);
  764. }
  765. /* if the decoder init function was already called previously,
  766. * free the already allocated subtitle_header before overwriting it */
  767. if (av_codec_is_decoder(codec))
  768. av_freep(&avctx->subtitle_header);
  769. #define SANE_NB_CHANNELS 128U
  770. if (avctx->channels > SANE_NB_CHANNELS) {
  771. ret = AVERROR(EINVAL);
  772. goto free_and_end;
  773. }
  774. avctx->codec = codec;
  775. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  776. avctx->codec_id == AV_CODEC_ID_NONE) {
  777. avctx->codec_type = codec->type;
  778. avctx->codec_id = codec->id;
  779. }
  780. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  781. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  782. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  783. ret = AVERROR(EINVAL);
  784. goto free_and_end;
  785. }
  786. avctx->frame_number = 0;
  787. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  788. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  789. (!avctx->time_base.num || !avctx->time_base.den)) {
  790. avctx->time_base.num = 1;
  791. avctx->time_base.den = avctx->sample_rate;
  792. }
  793. if (!HAVE_THREADS)
  794. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  795. if (HAVE_THREADS) {
  796. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  797. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  798. entangled_thread_counter++;
  799. if (ret < 0)
  800. goto free_and_end;
  801. }
  802. if (HAVE_THREADS && !avctx->thread_opaque
  803. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  804. ret = ff_thread_init(avctx);
  805. if (ret < 0) {
  806. goto free_and_end;
  807. }
  808. }
  809. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  810. avctx->thread_count = 1;
  811. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  812. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  813. avctx->codec->max_lowres);
  814. ret = AVERROR(EINVAL);
  815. goto free_and_end;
  816. }
  817. if (av_codec_is_encoder(avctx->codec)) {
  818. int i;
  819. if (avctx->codec->sample_fmts) {
  820. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  821. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  822. break;
  823. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  824. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  825. ret = AVERROR(EINVAL);
  826. goto free_and_end;
  827. }
  828. }
  829. if (avctx->codec->pix_fmts) {
  830. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  831. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  832. break;
  833. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
  834. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  835. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  836. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  837. ret = AVERROR(EINVAL);
  838. goto free_and_end;
  839. }
  840. }
  841. if (avctx->codec->supported_samplerates) {
  842. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  843. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  844. break;
  845. if (avctx->codec->supported_samplerates[i] == 0) {
  846. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  847. ret = AVERROR(EINVAL);
  848. goto free_and_end;
  849. }
  850. }
  851. if (avctx->codec->channel_layouts) {
  852. if (!avctx->channel_layout) {
  853. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  854. } else {
  855. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  856. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  857. break;
  858. if (avctx->codec->channel_layouts[i] == 0) {
  859. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  860. ret = AVERROR(EINVAL);
  861. goto free_and_end;
  862. }
  863. }
  864. }
  865. if (avctx->channel_layout && avctx->channels) {
  866. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  867. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  868. ret = AVERROR(EINVAL);
  869. goto free_and_end;
  870. }
  871. } else if (avctx->channel_layout) {
  872. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  873. }
  874. }
  875. avctx->pts_correction_num_faulty_pts =
  876. avctx->pts_correction_num_faulty_dts = 0;
  877. avctx->pts_correction_last_pts =
  878. avctx->pts_correction_last_dts = INT64_MIN;
  879. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  880. || avctx->internal->frame_thread_encoder)) {
  881. ret = avctx->codec->init(avctx);
  882. if (ret < 0) {
  883. goto free_and_end;
  884. }
  885. }
  886. ret=0;
  887. if (av_codec_is_decoder(avctx->codec)) {
  888. if (!avctx->bit_rate)
  889. avctx->bit_rate = get_bit_rate(avctx);
  890. /* validate channel layout from the decoder */
  891. if (avctx->channel_layout &&
  892. av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  893. av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
  894. avctx->channel_layout = 0;
  895. }
  896. }
  897. end:
  898. entangled_thread_counter--;
  899. /* Release any user-supplied mutex. */
  900. if (ff_lockmgr_cb) {
  901. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  902. }
  903. if (options) {
  904. av_dict_free(options);
  905. *options = tmp;
  906. }
  907. return ret;
  908. free_and_end:
  909. av_dict_free(&tmp);
  910. av_freep(&avctx->priv_data);
  911. av_freep(&avctx->internal);
  912. avctx->codec = NULL;
  913. goto end;
  914. }
  915. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  916. {
  917. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  918. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  919. return AVERROR(EINVAL);
  920. }
  921. if (avctx) {
  922. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  923. if (!avpkt->data || avpkt->size < size) {
  924. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  925. avpkt->data = avctx->internal->byte_buffer;
  926. avpkt->size = avctx->internal->byte_buffer_size;
  927. avpkt->destruct = NULL;
  928. }
  929. }
  930. if (avpkt->data) {
  931. void *destruct = avpkt->destruct;
  932. if (avpkt->size < size) {
  933. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  934. return AVERROR(EINVAL);
  935. }
  936. av_init_packet(avpkt);
  937. avpkt->destruct = destruct;
  938. avpkt->size = size;
  939. return 0;
  940. } else {
  941. int ret = av_new_packet(avpkt, size);
  942. if (ret < 0)
  943. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  944. return ret;
  945. }
  946. }
  947. int ff_alloc_packet(AVPacket *avpkt, int size)
  948. {
  949. return ff_alloc_packet2(NULL, avpkt, size);
  950. }
  951. /**
  952. * Pad last frame with silence.
  953. */
  954. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  955. {
  956. AVFrame *frame = NULL;
  957. uint8_t *buf = NULL;
  958. int ret;
  959. if (!(frame = avcodec_alloc_frame()))
  960. return AVERROR(ENOMEM);
  961. *frame = *src;
  962. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  963. s->frame_size, s->sample_fmt, 0)) < 0)
  964. goto fail;
  965. if (!(buf = av_malloc(ret))) {
  966. ret = AVERROR(ENOMEM);
  967. goto fail;
  968. }
  969. frame->nb_samples = s->frame_size;
  970. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  971. buf, ret, 0)) < 0)
  972. goto fail;
  973. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  974. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  975. goto fail;
  976. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  977. frame->nb_samples - src->nb_samples,
  978. s->channels, s->sample_fmt)) < 0)
  979. goto fail;
  980. *dst = frame;
  981. return 0;
  982. fail:
  983. if (frame->extended_data != frame->data)
  984. av_freep(&frame->extended_data);
  985. av_freep(&buf);
  986. av_freep(&frame);
  987. return ret;
  988. }
  989. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  990. AVPacket *avpkt,
  991. const AVFrame *frame,
  992. int *got_packet_ptr)
  993. {
  994. AVFrame tmp;
  995. AVFrame *padded_frame = NULL;
  996. int ret;
  997. AVPacket user_pkt = *avpkt;
  998. int needs_realloc = !user_pkt.data;
  999. *got_packet_ptr = 0;
  1000. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1001. av_free_packet(avpkt);
  1002. av_init_packet(avpkt);
  1003. return 0;
  1004. }
  1005. /* ensure that extended_data is properly set */
  1006. if (frame && !frame->extended_data) {
  1007. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1008. avctx->channels > AV_NUM_DATA_POINTERS) {
  1009. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1010. "with more than %d channels, but extended_data is not set.\n",
  1011. AV_NUM_DATA_POINTERS);
  1012. return AVERROR(EINVAL);
  1013. }
  1014. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1015. tmp = *frame;
  1016. tmp.extended_data = tmp.data;
  1017. frame = &tmp;
  1018. }
  1019. /* check for valid frame size */
  1020. if (frame) {
  1021. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1022. if (frame->nb_samples > avctx->frame_size) {
  1023. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1024. return AVERROR(EINVAL);
  1025. }
  1026. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1027. if (frame->nb_samples < avctx->frame_size &&
  1028. !avctx->internal->last_audio_frame) {
  1029. ret = pad_last_frame(avctx, &padded_frame, frame);
  1030. if (ret < 0)
  1031. return ret;
  1032. frame = padded_frame;
  1033. avctx->internal->last_audio_frame = 1;
  1034. }
  1035. if (frame->nb_samples != avctx->frame_size) {
  1036. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1037. ret = AVERROR(EINVAL);
  1038. goto end;
  1039. }
  1040. }
  1041. }
  1042. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1043. if (!ret) {
  1044. if (*got_packet_ptr) {
  1045. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1046. if (avpkt->pts == AV_NOPTS_VALUE)
  1047. avpkt->pts = frame->pts;
  1048. if (!avpkt->duration)
  1049. avpkt->duration = ff_samples_to_time_base(avctx,
  1050. frame->nb_samples);
  1051. }
  1052. avpkt->dts = avpkt->pts;
  1053. } else {
  1054. avpkt->size = 0;
  1055. }
  1056. }
  1057. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1058. needs_realloc = 0;
  1059. if (user_pkt.data) {
  1060. if (user_pkt.size >= avpkt->size) {
  1061. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1062. } else {
  1063. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1064. avpkt->size = user_pkt.size;
  1065. ret = -1;
  1066. }
  1067. avpkt->data = user_pkt.data;
  1068. avpkt->destruct = user_pkt.destruct;
  1069. } else {
  1070. if (av_dup_packet(avpkt) < 0) {
  1071. ret = AVERROR(ENOMEM);
  1072. }
  1073. }
  1074. }
  1075. if (!ret) {
  1076. if (needs_realloc && avpkt->data) {
  1077. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1078. if (new_data)
  1079. avpkt->data = new_data;
  1080. }
  1081. avctx->frame_number++;
  1082. }
  1083. if (ret < 0 || !*got_packet_ptr) {
  1084. av_free_packet(avpkt);
  1085. av_init_packet(avpkt);
  1086. goto end;
  1087. }
  1088. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1089. * this needs to be moved to the encoders, but for now we can do it
  1090. * here to simplify things */
  1091. avpkt->flags |= AV_PKT_FLAG_KEY;
  1092. end:
  1093. if (padded_frame) {
  1094. av_freep(&padded_frame->data[0]);
  1095. if (padded_frame->extended_data != padded_frame->data)
  1096. av_freep(&padded_frame->extended_data);
  1097. av_freep(&padded_frame);
  1098. }
  1099. return ret;
  1100. }
  1101. #if FF_API_OLD_DECODE_AUDIO
  1102. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1103. uint8_t *buf, int buf_size,
  1104. const short *samples)
  1105. {
  1106. AVPacket pkt;
  1107. AVFrame frame0;
  1108. AVFrame *frame;
  1109. int ret, samples_size, got_packet;
  1110. av_init_packet(&pkt);
  1111. pkt.data = buf;
  1112. pkt.size = buf_size;
  1113. if (samples) {
  1114. frame = &frame0;
  1115. avcodec_get_frame_defaults(frame);
  1116. if (avctx->frame_size) {
  1117. frame->nb_samples = avctx->frame_size;
  1118. } else {
  1119. /* if frame_size is not set, the number of samples must be
  1120. * calculated from the buffer size */
  1121. int64_t nb_samples;
  1122. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1123. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1124. "support this codec\n");
  1125. return AVERROR(EINVAL);
  1126. }
  1127. nb_samples = (int64_t)buf_size * 8 /
  1128. (av_get_bits_per_sample(avctx->codec_id) *
  1129. avctx->channels);
  1130. if (nb_samples >= INT_MAX)
  1131. return AVERROR(EINVAL);
  1132. frame->nb_samples = nb_samples;
  1133. }
  1134. /* it is assumed that the samples buffer is large enough based on the
  1135. * relevant parameters */
  1136. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1137. frame->nb_samples,
  1138. avctx->sample_fmt, 1);
  1139. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1140. avctx->sample_fmt,
  1141. (const uint8_t *)samples,
  1142. samples_size, 1)))
  1143. return ret;
  1144. /* fabricate frame pts from sample count.
  1145. * this is needed because the avcodec_encode_audio() API does not have
  1146. * a way for the user to provide pts */
  1147. if (avctx->sample_rate && avctx->time_base.num)
  1148. frame->pts = ff_samples_to_time_base(avctx,
  1149. avctx->internal->sample_count);
  1150. else
  1151. frame->pts = AV_NOPTS_VALUE;
  1152. avctx->internal->sample_count += frame->nb_samples;
  1153. } else {
  1154. frame = NULL;
  1155. }
  1156. got_packet = 0;
  1157. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1158. if (!ret && got_packet && avctx->coded_frame) {
  1159. avctx->coded_frame->pts = pkt.pts;
  1160. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1161. }
  1162. /* free any side data since we cannot return it */
  1163. ff_packet_free_side_data(&pkt);
  1164. if (frame && frame->extended_data != frame->data)
  1165. av_freep(&frame->extended_data);
  1166. return ret ? ret : pkt.size;
  1167. }
  1168. #endif
  1169. #if FF_API_OLD_ENCODE_VIDEO
  1170. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1171. const AVFrame *pict)
  1172. {
  1173. AVPacket pkt;
  1174. int ret, got_packet = 0;
  1175. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1176. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1177. return -1;
  1178. }
  1179. av_init_packet(&pkt);
  1180. pkt.data = buf;
  1181. pkt.size = buf_size;
  1182. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1183. if (!ret && got_packet && avctx->coded_frame) {
  1184. avctx->coded_frame->pts = pkt.pts;
  1185. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1186. }
  1187. /* free any side data since we cannot return it */
  1188. if (pkt.side_data_elems > 0) {
  1189. int i;
  1190. for (i = 0; i < pkt.side_data_elems; i++)
  1191. av_free(pkt.side_data[i].data);
  1192. av_freep(&pkt.side_data);
  1193. pkt.side_data_elems = 0;
  1194. }
  1195. return ret ? ret : pkt.size;
  1196. }
  1197. #endif
  1198. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1199. AVPacket *avpkt,
  1200. const AVFrame *frame,
  1201. int *got_packet_ptr)
  1202. {
  1203. int ret;
  1204. AVPacket user_pkt = *avpkt;
  1205. int needs_realloc = !user_pkt.data;
  1206. *got_packet_ptr = 0;
  1207. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1208. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1209. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1210. av_free_packet(avpkt);
  1211. av_init_packet(avpkt);
  1212. avpkt->size = 0;
  1213. return 0;
  1214. }
  1215. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1216. return AVERROR(EINVAL);
  1217. av_assert0(avctx->codec->encode2);
  1218. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1219. av_assert0(ret <= 0);
  1220. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1221. needs_realloc = 0;
  1222. if (user_pkt.data) {
  1223. if (user_pkt.size >= avpkt->size) {
  1224. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1225. } else {
  1226. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1227. avpkt->size = user_pkt.size;
  1228. ret = -1;
  1229. }
  1230. avpkt->data = user_pkt.data;
  1231. avpkt->destruct = user_pkt.destruct;
  1232. } else {
  1233. if (av_dup_packet(avpkt) < 0) {
  1234. ret = AVERROR(ENOMEM);
  1235. }
  1236. }
  1237. }
  1238. if (!ret) {
  1239. if (!*got_packet_ptr)
  1240. avpkt->size = 0;
  1241. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1242. avpkt->pts = avpkt->dts = frame->pts;
  1243. if (needs_realloc && avpkt->data &&
  1244. avpkt->destruct == av_destruct_packet) {
  1245. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1246. if (new_data)
  1247. avpkt->data = new_data;
  1248. }
  1249. avctx->frame_number++;
  1250. }
  1251. if (ret < 0 || !*got_packet_ptr)
  1252. av_free_packet(avpkt);
  1253. emms_c();
  1254. return ret;
  1255. }
  1256. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1257. const AVSubtitle *sub)
  1258. {
  1259. int ret;
  1260. if (sub->start_display_time) {
  1261. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1262. return -1;
  1263. }
  1264. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1265. avctx->frame_number++;
  1266. return ret;
  1267. }
  1268. /**
  1269. * Attempt to guess proper monotonic timestamps for decoded video frames
  1270. * which might have incorrect times. Input timestamps may wrap around, in
  1271. * which case the output will as well.
  1272. *
  1273. * @param pts the pts field of the decoded AVPacket, as passed through
  1274. * AVFrame.pkt_pts
  1275. * @param dts the dts field of the decoded AVPacket
  1276. * @return one of the input values, may be AV_NOPTS_VALUE
  1277. */
  1278. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1279. int64_t reordered_pts, int64_t dts)
  1280. {
  1281. int64_t pts = AV_NOPTS_VALUE;
  1282. if (dts != AV_NOPTS_VALUE) {
  1283. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1284. ctx->pts_correction_last_dts = dts;
  1285. }
  1286. if (reordered_pts != AV_NOPTS_VALUE) {
  1287. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1288. ctx->pts_correction_last_pts = reordered_pts;
  1289. }
  1290. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1291. && reordered_pts != AV_NOPTS_VALUE)
  1292. pts = reordered_pts;
  1293. else
  1294. pts = dts;
  1295. return pts;
  1296. }
  1297. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1298. {
  1299. int size = 0;
  1300. const uint8_t *data;
  1301. uint32_t flags;
  1302. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1303. return;
  1304. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1305. if (!data || size < 4)
  1306. return;
  1307. flags = bytestream_get_le32(&data);
  1308. size -= 4;
  1309. if (size < 4) /* Required for any of the changes */
  1310. return;
  1311. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1312. avctx->channels = bytestream_get_le32(&data);
  1313. size -= 4;
  1314. }
  1315. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1316. if (size < 8)
  1317. return;
  1318. avctx->channel_layout = bytestream_get_le64(&data);
  1319. size -= 8;
  1320. }
  1321. if (size < 4)
  1322. return;
  1323. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1324. avctx->sample_rate = bytestream_get_le32(&data);
  1325. size -= 4;
  1326. }
  1327. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1328. if (size < 8)
  1329. return;
  1330. avctx->width = bytestream_get_le32(&data);
  1331. avctx->height = bytestream_get_le32(&data);
  1332. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1333. size -= 8;
  1334. }
  1335. }
  1336. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1337. int *got_picture_ptr,
  1338. const AVPacket *avpkt)
  1339. {
  1340. int ret;
  1341. // copy to ensure we do not change avpkt
  1342. AVPacket tmp = *avpkt;
  1343. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1344. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1345. return AVERROR(EINVAL);
  1346. }
  1347. *got_picture_ptr = 0;
  1348. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1349. return AVERROR(EINVAL);
  1350. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1351. int did_split = av_packet_split_side_data(&tmp);
  1352. apply_param_change(avctx, &tmp);
  1353. avctx->pkt = &tmp;
  1354. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1355. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1356. &tmp);
  1357. else {
  1358. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1359. &tmp);
  1360. picture->pkt_dts = avpkt->dts;
  1361. if(!avctx->has_b_frames){
  1362. picture->pkt_pos = avpkt->pos;
  1363. }
  1364. //FIXME these should be under if(!avctx->has_b_frames)
  1365. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1366. if (!picture->width) picture->width = avctx->width;
  1367. if (!picture->height) picture->height = avctx->height;
  1368. if (picture->format == PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1369. }
  1370. emms_c(); //needed to avoid an emms_c() call before every return;
  1371. avctx->pkt = NULL;
  1372. if (did_split) {
  1373. ff_packet_free_side_data(&tmp);
  1374. if(ret == tmp.size)
  1375. ret = avpkt->size;
  1376. }
  1377. if (*got_picture_ptr){
  1378. avctx->frame_number++;
  1379. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1380. picture->pkt_pts,
  1381. picture->pkt_dts);
  1382. }
  1383. } else
  1384. ret = 0;
  1385. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1386. * make sure it's set correctly */
  1387. picture->extended_data = picture->data;
  1388. return ret;
  1389. }
  1390. #if FF_API_OLD_DECODE_AUDIO
  1391. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1392. int *frame_size_ptr,
  1393. AVPacket *avpkt)
  1394. {
  1395. AVFrame frame;
  1396. int ret, got_frame = 0;
  1397. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1398. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1399. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1400. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1401. "avcodec_decode_audio4()\n");
  1402. avctx->get_buffer = avcodec_default_get_buffer;
  1403. avctx->release_buffer = avcodec_default_release_buffer;
  1404. }
  1405. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1406. if (ret >= 0 && got_frame) {
  1407. int ch, plane_size;
  1408. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1409. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1410. frame.nb_samples,
  1411. avctx->sample_fmt, 1);
  1412. if (*frame_size_ptr < data_size) {
  1413. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1414. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1415. return AVERROR(EINVAL);
  1416. }
  1417. memcpy(samples, frame.extended_data[0], plane_size);
  1418. if (planar && avctx->channels > 1) {
  1419. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1420. for (ch = 1; ch < avctx->channels; ch++) {
  1421. memcpy(out, frame.extended_data[ch], plane_size);
  1422. out += plane_size;
  1423. }
  1424. }
  1425. *frame_size_ptr = data_size;
  1426. } else {
  1427. *frame_size_ptr = 0;
  1428. }
  1429. return ret;
  1430. }
  1431. #endif
  1432. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1433. AVFrame *frame,
  1434. int *got_frame_ptr,
  1435. const AVPacket *avpkt)
  1436. {
  1437. int planar, channels;
  1438. int ret = 0;
  1439. *got_frame_ptr = 0;
  1440. if (!avpkt->data && avpkt->size) {
  1441. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1442. return AVERROR(EINVAL);
  1443. }
  1444. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1445. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1446. return AVERROR(EINVAL);
  1447. }
  1448. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1449. uint8_t *side;
  1450. int side_size;
  1451. // copy to ensure we do not change avpkt
  1452. AVPacket tmp = *avpkt;
  1453. int did_split = av_packet_split_side_data(&tmp);
  1454. apply_param_change(avctx, &tmp);
  1455. avctx->pkt = &tmp;
  1456. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1457. if (ret >= 0 && *got_frame_ptr) {
  1458. avctx->frame_number++;
  1459. frame->pkt_dts = avpkt->dts;
  1460. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1461. frame->pkt_pts,
  1462. frame->pkt_dts);
  1463. if (frame->format == AV_SAMPLE_FMT_NONE)
  1464. frame->format = avctx->sample_fmt;
  1465. if (!frame->channel_layout)
  1466. frame->channel_layout = avctx->channel_layout;
  1467. if (!frame->channels)
  1468. frame->channels = avctx->channels;
  1469. if (!frame->sample_rate)
  1470. frame->sample_rate = avctx->sample_rate;
  1471. }
  1472. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1473. if(side && side_size>=10) {
  1474. avctx->internal->skip_samples = AV_RL32(side);
  1475. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1476. avctx->internal->skip_samples);
  1477. }
  1478. if (avctx->internal->skip_samples) {
  1479. if(frame->nb_samples <= avctx->internal->skip_samples){
  1480. *got_frame_ptr = 0;
  1481. avctx->internal->skip_samples -= frame->nb_samples;
  1482. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1483. avctx->internal->skip_samples);
  1484. } else {
  1485. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1486. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1487. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1488. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1489. (AVRational){1, avctx->sample_rate},
  1490. avctx->pkt_timebase);
  1491. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1492. frame->pkt_pts += diff_ts;
  1493. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1494. frame->pkt_dts += diff_ts;
  1495. if (frame->pkt_duration >= diff_ts)
  1496. frame->pkt_duration -= diff_ts;
  1497. } else {
  1498. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1499. }
  1500. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1501. avctx->internal->skip_samples, frame->nb_samples);
  1502. frame->nb_samples -= avctx->internal->skip_samples;
  1503. avctx->internal->skip_samples = 0;
  1504. }
  1505. }
  1506. avctx->pkt = NULL;
  1507. if (did_split) {
  1508. ff_packet_free_side_data(&tmp);
  1509. if(ret == tmp.size)
  1510. ret = avpkt->size;
  1511. }
  1512. }
  1513. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1514. * make sure it's set correctly; assume decoders that actually use
  1515. * extended_data are doing it correctly */
  1516. planar = av_sample_fmt_is_planar(frame->format);
  1517. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1518. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1519. frame->extended_data = frame->data;
  1520. return ret;
  1521. }
  1522. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1523. int *got_sub_ptr,
  1524. AVPacket *avpkt)
  1525. {
  1526. int ret;
  1527. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1528. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1529. return AVERROR(EINVAL);
  1530. }
  1531. avctx->pkt = avpkt;
  1532. *got_sub_ptr = 0;
  1533. avcodec_get_subtitle_defaults(sub);
  1534. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1535. sub->pts = av_rescale_q(avpkt->pts,
  1536. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1537. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1538. if (*got_sub_ptr)
  1539. avctx->frame_number++;
  1540. return ret;
  1541. }
  1542. void avsubtitle_free(AVSubtitle *sub)
  1543. {
  1544. int i;
  1545. for (i = 0; i < sub->num_rects; i++) {
  1546. av_freep(&sub->rects[i]->pict.data[0]);
  1547. av_freep(&sub->rects[i]->pict.data[1]);
  1548. av_freep(&sub->rects[i]->pict.data[2]);
  1549. av_freep(&sub->rects[i]->pict.data[3]);
  1550. av_freep(&sub->rects[i]->text);
  1551. av_freep(&sub->rects[i]->ass);
  1552. av_freep(&sub->rects[i]);
  1553. }
  1554. av_freep(&sub->rects);
  1555. memset(sub, 0, sizeof(AVSubtitle));
  1556. }
  1557. av_cold int avcodec_close(AVCodecContext *avctx)
  1558. {
  1559. /* If there is a user-supplied mutex locking routine, call it. */
  1560. if (ff_lockmgr_cb) {
  1561. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1562. return -1;
  1563. }
  1564. entangled_thread_counter++;
  1565. if (entangled_thread_counter != 1) {
  1566. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1567. entangled_thread_counter--;
  1568. return -1;
  1569. }
  1570. if (avcodec_is_open(avctx)) {
  1571. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1572. entangled_thread_counter --;
  1573. ff_frame_thread_encoder_free(avctx);
  1574. entangled_thread_counter ++;
  1575. }
  1576. if (HAVE_THREADS && avctx->thread_opaque)
  1577. ff_thread_free(avctx);
  1578. if (avctx->codec && avctx->codec->close)
  1579. avctx->codec->close(avctx);
  1580. avcodec_default_free_buffers(avctx);
  1581. avctx->coded_frame = NULL;
  1582. avctx->internal->byte_buffer_size = 0;
  1583. av_freep(&avctx->internal->byte_buffer);
  1584. av_freep(&avctx->internal);
  1585. }
  1586. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1587. av_opt_free(avctx->priv_data);
  1588. av_opt_free(avctx);
  1589. av_freep(&avctx->priv_data);
  1590. if (av_codec_is_encoder(avctx->codec))
  1591. av_freep(&avctx->extradata);
  1592. avctx->codec = NULL;
  1593. avctx->active_thread_type = 0;
  1594. entangled_thread_counter--;
  1595. /* Release any user-supplied mutex. */
  1596. if (ff_lockmgr_cb) {
  1597. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1598. }
  1599. return 0;
  1600. }
  1601. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1602. {
  1603. switch(id){
  1604. //This is for future deprecatec codec ids, its empty since
  1605. //last major bump but will fill up again over time, please don't remove it
  1606. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  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. }