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.

2165 lines
65KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include "bytestream.h"
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <limits.h>
  45. #include <float.h>
  46. static int volatile entangled_thread_counter = 0;
  47. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  48. static void *codec_mutex;
  49. static void *avformat_mutex;
  50. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  51. {
  52. if (min_size < *size)
  53. return ptr;
  54. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  55. ptr = av_realloc(ptr, min_size);
  56. /* we could set this to the unmodified min_size but this is safer
  57. * if the user lost the ptr and uses NULL now
  58. */
  59. if (!ptr)
  60. min_size = 0;
  61. *size = min_size;
  62. return ptr;
  63. }
  64. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  65. {
  66. void **p = ptr;
  67. if (min_size < *size)
  68. return;
  69. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  70. av_free(*p);
  71. *p = av_malloc(min_size);
  72. if (!*p)
  73. min_size = 0;
  74. *size = min_size;
  75. }
  76. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. void **p = ptr;
  79. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  85. if (*size)
  86. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  87. }
  88. /* encoder management */
  89. static AVCodec *first_avcodec = NULL;
  90. AVCodec *av_codec_next(const AVCodec *c)
  91. {
  92. if (c)
  93. return c->next;
  94. else
  95. return first_avcodec;
  96. }
  97. static void avcodec_init(void)
  98. {
  99. static int initialized = 0;
  100. if (initialized != 0)
  101. return;
  102. initialized = 1;
  103. ff_dsputil_static_init();
  104. }
  105. int av_codec_is_encoder(const AVCodec *codec)
  106. {
  107. return codec && (codec->encode_sub || codec->encode2);
  108. }
  109. int av_codec_is_decoder(const AVCodec *codec)
  110. {
  111. return codec && codec->decode;
  112. }
  113. void avcodec_register(AVCodec *codec)
  114. {
  115. AVCodec **p;
  116. avcodec_init();
  117. p = &first_avcodec;
  118. while (*p != NULL)
  119. p = &(*p)->next;
  120. *p = codec;
  121. codec->next = NULL;
  122. if (codec->init_static_data)
  123. codec->init_static_data(codec);
  124. }
  125. unsigned avcodec_get_edge_width(void)
  126. {
  127. return EDGE_WIDTH;
  128. }
  129. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  130. {
  131. s->coded_width = width;
  132. s->coded_height = height;
  133. s->width = width;
  134. s->height = height;
  135. }
  136. #define INTERNAL_BUFFER_SIZE (32 + 1)
  137. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  138. int linesize_align[AV_NUM_DATA_POINTERS])
  139. {
  140. int i;
  141. int w_align = 1;
  142. int h_align = 1;
  143. switch (s->pix_fmt) {
  144. case PIX_FMT_YUV420P:
  145. case PIX_FMT_YUYV422:
  146. case PIX_FMT_UYVY422:
  147. case PIX_FMT_YUV422P:
  148. case PIX_FMT_YUV440P:
  149. case PIX_FMT_YUV444P:
  150. case PIX_FMT_GBRP:
  151. case PIX_FMT_GRAY8:
  152. case PIX_FMT_GRAY16BE:
  153. case PIX_FMT_GRAY16LE:
  154. case PIX_FMT_YUVJ420P:
  155. case PIX_FMT_YUVJ422P:
  156. case PIX_FMT_YUVJ440P:
  157. case PIX_FMT_YUVJ444P:
  158. case PIX_FMT_YUVA420P:
  159. case PIX_FMT_YUV420P9LE:
  160. case PIX_FMT_YUV420P9BE:
  161. case PIX_FMT_YUV420P10LE:
  162. case PIX_FMT_YUV420P10BE:
  163. case PIX_FMT_YUV422P9LE:
  164. case PIX_FMT_YUV422P9BE:
  165. case PIX_FMT_YUV422P10LE:
  166. case PIX_FMT_YUV422P10BE:
  167. case PIX_FMT_YUV444P9LE:
  168. case PIX_FMT_YUV444P9BE:
  169. case PIX_FMT_YUV444P10LE:
  170. case PIX_FMT_YUV444P10BE:
  171. case PIX_FMT_GBRP9LE:
  172. case PIX_FMT_GBRP9BE:
  173. case PIX_FMT_GBRP10LE:
  174. case PIX_FMT_GBRP10BE:
  175. w_align = 16; //FIXME assume 16 pixel per macroblock
  176. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  177. break;
  178. case PIX_FMT_YUV411P:
  179. case PIX_FMT_UYYVYY411:
  180. w_align = 32;
  181. h_align = 8;
  182. break;
  183. case PIX_FMT_YUV410P:
  184. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  185. w_align = 64;
  186. h_align = 64;
  187. }
  188. case PIX_FMT_RGB555:
  189. if (s->codec_id == AV_CODEC_ID_RPZA) {
  190. w_align = 4;
  191. h_align = 4;
  192. }
  193. case PIX_FMT_PAL8:
  194. case PIX_FMT_BGR8:
  195. case PIX_FMT_RGB8:
  196. if (s->codec_id == AV_CODEC_ID_SMC) {
  197. w_align = 4;
  198. h_align = 4;
  199. }
  200. break;
  201. case PIX_FMT_BGR24:
  202. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  203. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  204. w_align = 4;
  205. h_align = 4;
  206. }
  207. break;
  208. default:
  209. w_align = 1;
  210. h_align = 1;
  211. break;
  212. }
  213. *width = FFALIGN(*width, w_align);
  214. *height = FFALIGN(*height, h_align);
  215. if (s->codec_id == AV_CODEC_ID_H264)
  216. // some of the optimized chroma MC reads one line too much
  217. *height += 2;
  218. for (i = 0; i < 4; i++)
  219. linesize_align[i] = STRIDE_ALIGN;
  220. }
  221. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  222. {
  223. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  224. int linesize_align[AV_NUM_DATA_POINTERS];
  225. int align;
  226. avcodec_align_dimensions2(s, width, height, linesize_align);
  227. align = FFMAX(linesize_align[0], linesize_align[3]);
  228. linesize_align[1] <<= chroma_shift;
  229. linesize_align[2] <<= chroma_shift;
  230. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  231. *width = FFALIGN(*width, align);
  232. }
  233. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  234. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  235. int buf_size, int align)
  236. {
  237. int ch, planar, needed_size, ret = 0;
  238. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  239. frame->nb_samples, sample_fmt,
  240. align);
  241. if (buf_size < needed_size)
  242. return AVERROR(EINVAL);
  243. planar = av_sample_fmt_is_planar(sample_fmt);
  244. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  245. if (!(frame->extended_data = av_mallocz(nb_channels *
  246. sizeof(*frame->extended_data))))
  247. return AVERROR(ENOMEM);
  248. } else {
  249. frame->extended_data = frame->data;
  250. }
  251. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  252. buf, nb_channels, frame->nb_samples,
  253. sample_fmt, align)) < 0) {
  254. if (frame->extended_data != frame->data)
  255. av_free(frame->extended_data);
  256. return ret;
  257. }
  258. if (frame->extended_data != frame->data) {
  259. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  260. frame->data[ch] = frame->extended_data[ch];
  261. }
  262. return ret;
  263. }
  264. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  265. {
  266. AVCodecInternal *avci = avctx->internal;
  267. InternalBuffer *buf;
  268. int buf_size, ret;
  269. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  270. frame->nb_samples, avctx->sample_fmt,
  271. 0);
  272. if (buf_size < 0)
  273. return AVERROR(EINVAL);
  274. /* allocate InternalBuffer if needed */
  275. if (!avci->buffer) {
  276. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  277. if (!avci->buffer)
  278. return AVERROR(ENOMEM);
  279. }
  280. buf = avci->buffer;
  281. /* if there is a previously-used internal buffer, check its size and
  282. * channel count to see if we can reuse it */
  283. if (buf->extended_data) {
  284. /* if current buffer is too small, free it */
  285. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  286. av_free(buf->extended_data[0]);
  287. if (buf->extended_data != buf->data)
  288. av_free(&buf->extended_data);
  289. buf->extended_data = NULL;
  290. buf->data[0] = NULL;
  291. }
  292. /* if number of channels has changed, reset and/or free extended data
  293. * pointers but leave data buffer in buf->data[0] for reuse */
  294. if (buf->nb_channels != avctx->channels) {
  295. if (buf->extended_data != buf->data)
  296. av_free(buf->extended_data);
  297. buf->extended_data = NULL;
  298. }
  299. }
  300. /* if there is no previous buffer or the previous buffer cannot be used
  301. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  302. if (!buf->extended_data) {
  303. if (!buf->data[0]) {
  304. if (!(buf->data[0] = av_mallocz(buf_size)))
  305. return AVERROR(ENOMEM);
  306. buf->audio_data_size = buf_size;
  307. }
  308. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  309. avctx->sample_fmt, buf->data[0],
  310. buf->audio_data_size, 0)))
  311. return ret;
  312. if (frame->extended_data == frame->data)
  313. buf->extended_data = buf->data;
  314. else
  315. buf->extended_data = frame->extended_data;
  316. memcpy(buf->data, frame->data, sizeof(frame->data));
  317. buf->linesize[0] = frame->linesize[0];
  318. buf->nb_channels = avctx->channels;
  319. } else {
  320. /* copy InternalBuffer info to the AVFrame */
  321. frame->extended_data = buf->extended_data;
  322. frame->linesize[0] = buf->linesize[0];
  323. memcpy(frame->data, buf->data, sizeof(frame->data));
  324. }
  325. frame->type = FF_BUFFER_TYPE_INTERNAL;
  326. if (avctx->pkt)
  327. frame->pkt_pts = avctx->pkt->pts;
  328. else
  329. frame->pkt_pts = AV_NOPTS_VALUE;
  330. frame->reordered_opaque = avctx->reordered_opaque;
  331. frame->sample_rate = avctx->sample_rate;
  332. frame->format = avctx->sample_fmt;
  333. frame->channel_layout = avctx->channel_layout;
  334. if (avctx->debug & FF_DEBUG_BUFFERS)
  335. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  336. "internal audio buffer used\n", frame);
  337. return 0;
  338. }
  339. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  340. {
  341. int i;
  342. int w = s->width;
  343. int h = s->height;
  344. InternalBuffer *buf;
  345. AVCodecInternal *avci = s->internal;
  346. if (pic->data[0] != NULL) {
  347. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  348. return -1;
  349. }
  350. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  351. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  352. return -1;
  353. }
  354. if (av_image_check_size(w, h, 0, s))
  355. return -1;
  356. if (!avci->buffer) {
  357. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  358. sizeof(InternalBuffer));
  359. }
  360. buf = &avci->buffer[avci->buffer_count];
  361. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  362. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  363. av_freep(&buf->base[i]);
  364. buf->data[i] = NULL;
  365. }
  366. }
  367. if (!buf->base[0]) {
  368. int h_chroma_shift, v_chroma_shift;
  369. int size[4] = { 0 };
  370. int tmpsize;
  371. int unaligned;
  372. AVPicture picture;
  373. int stride_align[AV_NUM_DATA_POINTERS];
  374. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1 + 1;
  375. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  376. avcodec_align_dimensions2(s, &w, &h, stride_align);
  377. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  378. w += EDGE_WIDTH * 2;
  379. h += EDGE_WIDTH * 2;
  380. }
  381. do {
  382. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  383. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  384. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  385. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  386. w += w & ~(w - 1);
  387. unaligned = 0;
  388. for (i = 0; i < 4; i++)
  389. unaligned |= picture.linesize[i] % stride_align[i];
  390. } while (unaligned);
  391. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  392. if (tmpsize < 0)
  393. return -1;
  394. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  395. size[i] = picture.data[i + 1] - picture.data[i];
  396. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  397. memset(buf->base, 0, sizeof(buf->base));
  398. memset(buf->data, 0, sizeof(buf->data));
  399. for (i = 0; i < 4 && size[i]; i++) {
  400. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  401. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  402. buf->linesize[i] = picture.linesize[i];
  403. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  404. if (buf->base[i] == NULL)
  405. return -1;
  406. memset(buf->base[i], 128, size[i]);
  407. // no edge if EDGE EMU or not planar YUV
  408. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  409. buf->data[i] = buf->base[i];
  410. else
  411. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  412. }
  413. for (; i < AV_NUM_DATA_POINTERS; i++) {
  414. buf->base[i] = buf->data[i] = NULL;
  415. buf->linesize[i] = 0;
  416. }
  417. if (size[1] && !size[2])
  418. ff_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  419. buf->width = s->width;
  420. buf->height = s->height;
  421. buf->pix_fmt = s->pix_fmt;
  422. }
  423. pic->type = FF_BUFFER_TYPE_INTERNAL;
  424. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  425. pic->base[i] = buf->base[i];
  426. pic->data[i] = buf->data[i];
  427. pic->linesize[i] = buf->linesize[i];
  428. }
  429. pic->extended_data = pic->data;
  430. avci->buffer_count++;
  431. pic->width = buf->width;
  432. pic->height = buf->height;
  433. pic->format = buf->pix_fmt;
  434. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  435. if (s->pkt)
  436. pic->pkt_pts = s->pkt->pts;
  437. else
  438. pic->pkt_pts = AV_NOPTS_VALUE;
  439. pic->reordered_opaque = s->reordered_opaque;
  440. if (s->debug & FF_DEBUG_BUFFERS)
  441. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  442. "buffers used\n", pic, avci->buffer_count);
  443. return 0;
  444. }
  445. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  446. {
  447. switch (avctx->codec_type) {
  448. case AVMEDIA_TYPE_VIDEO:
  449. return video_get_buffer(avctx, frame);
  450. case AVMEDIA_TYPE_AUDIO:
  451. return audio_get_buffer(avctx, frame);
  452. default:
  453. return -1;
  454. }
  455. }
  456. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  457. {
  458. int i;
  459. InternalBuffer *buf, *last;
  460. AVCodecInternal *avci = s->internal;
  461. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  462. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  463. assert(avci->buffer_count);
  464. if (avci->buffer) {
  465. buf = NULL; /* avoids warning */
  466. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  467. buf = &avci->buffer[i];
  468. if (buf->data[0] == pic->data[0])
  469. break;
  470. }
  471. assert(i < avci->buffer_count);
  472. avci->buffer_count--;
  473. last = &avci->buffer[avci->buffer_count];
  474. if (buf != last)
  475. FFSWAP(InternalBuffer, *buf, *last);
  476. }
  477. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  478. pic->data[i] = NULL;
  479. // pic->base[i]=NULL;
  480. if (s->debug & FF_DEBUG_BUFFERS)
  481. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  482. "buffers used\n", pic, avci->buffer_count);
  483. }
  484. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  485. {
  486. AVFrame temp_pic;
  487. int i;
  488. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  489. /* If no picture return a new buffer */
  490. if (pic->data[0] == NULL) {
  491. /* We will copy from buffer, so must be readable */
  492. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  493. return s->get_buffer(s, pic);
  494. }
  495. assert(s->pix_fmt == pic->format);
  496. /* If internal buffer type return the same buffer */
  497. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  498. if (s->pkt)
  499. pic->pkt_pts = s->pkt->pts;
  500. else
  501. pic->pkt_pts = AV_NOPTS_VALUE;
  502. pic->reordered_opaque = s->reordered_opaque;
  503. return 0;
  504. }
  505. /*
  506. * Not internal type and reget_buffer not overridden, emulate cr buffer
  507. */
  508. temp_pic = *pic;
  509. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  510. pic->data[i] = pic->base[i] = NULL;
  511. pic->opaque = NULL;
  512. /* Allocate new frame */
  513. if (s->get_buffer(s, pic))
  514. return -1;
  515. /* Copy image data from old buffer to new buffer */
  516. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  517. s->height);
  518. s->release_buffer(s, &temp_pic); // Release old frame
  519. return 0;
  520. }
  521. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  522. {
  523. int i;
  524. for (i = 0; i < count; i++) {
  525. int r = func(c, (char *)arg + i * size);
  526. if (ret)
  527. ret[i] = r;
  528. }
  529. return 0;
  530. }
  531. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  532. {
  533. int i;
  534. for (i = 0; i < count; i++) {
  535. int r = func(c, arg, i, 0);
  536. if (ret)
  537. ret[i] = r;
  538. }
  539. return 0;
  540. }
  541. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt)
  542. {
  543. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  544. ++fmt;
  545. return fmt[0];
  546. }
  547. void avcodec_get_frame_defaults(AVFrame *frame)
  548. {
  549. if (frame->extended_data != frame->data)
  550. av_freep(&frame->extended_data);
  551. memset(frame, 0, sizeof(AVFrame));
  552. frame->pts = AV_NOPTS_VALUE;
  553. frame->key_frame = 1;
  554. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  555. frame->format = -1; /* unknown */
  556. frame->extended_data = frame->data;
  557. }
  558. AVFrame *avcodec_alloc_frame(void)
  559. {
  560. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  561. if (frame == NULL)
  562. return NULL;
  563. avcodec_get_frame_defaults(frame);
  564. return frame;
  565. }
  566. void avcodec_free_frame(AVFrame **frame)
  567. {
  568. AVFrame *f;
  569. if (!frame || !*frame)
  570. return;
  571. f = *frame;
  572. if (f->extended_data != f->data)
  573. av_freep(&f->extended_data);
  574. av_freep(frame);
  575. }
  576. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  577. {
  578. int ret = 0;
  579. AVDictionary *tmp = NULL;
  580. if (avcodec_is_open(avctx))
  581. return 0;
  582. if ((!codec && !avctx->codec)) {
  583. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  584. return AVERROR(EINVAL);
  585. }
  586. if ((codec && avctx->codec && codec != avctx->codec)) {
  587. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  588. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  589. return AVERROR(EINVAL);
  590. }
  591. if (!codec)
  592. codec = avctx->codec;
  593. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  594. return AVERROR(EINVAL);
  595. if (options)
  596. av_dict_copy(&tmp, *options, 0);
  597. /* If there is a user-supplied mutex locking routine, call it. */
  598. if (ff_lockmgr_cb) {
  599. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  600. return -1;
  601. }
  602. entangled_thread_counter++;
  603. if (entangled_thread_counter != 1) {
  604. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  605. ret = -1;
  606. goto end;
  607. }
  608. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  609. if (!avctx->internal) {
  610. ret = AVERROR(ENOMEM);
  611. goto end;
  612. }
  613. if (codec->priv_data_size > 0) {
  614. if (!avctx->priv_data) {
  615. avctx->priv_data = av_mallocz(codec->priv_data_size);
  616. if (!avctx->priv_data) {
  617. ret = AVERROR(ENOMEM);
  618. goto end;
  619. }
  620. if (codec->priv_class) {
  621. *(const AVClass **)avctx->priv_data = codec->priv_class;
  622. av_opt_set_defaults(avctx->priv_data);
  623. }
  624. }
  625. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  626. goto free_and_end;
  627. } else {
  628. avctx->priv_data = NULL;
  629. }
  630. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  631. goto free_and_end;
  632. if (avctx->coded_width && avctx->coded_height)
  633. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  634. else if (avctx->width && avctx->height)
  635. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  636. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  637. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  638. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  639. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  640. avcodec_set_dimensions(avctx, 0, 0);
  641. }
  642. /* if the decoder init function was already called previously,
  643. * free the already allocated subtitle_header before overwriting it */
  644. if (av_codec_is_decoder(codec))
  645. av_freep(&avctx->subtitle_header);
  646. #define SANE_NB_CHANNELS 128U
  647. if (avctx->channels > SANE_NB_CHANNELS) {
  648. ret = AVERROR(EINVAL);
  649. goto free_and_end;
  650. }
  651. avctx->codec = codec;
  652. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  653. avctx->codec_id == AV_CODEC_ID_NONE) {
  654. avctx->codec_type = codec->type;
  655. avctx->codec_id = codec->id;
  656. }
  657. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  658. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  659. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  660. ret = AVERROR(EINVAL);
  661. goto free_and_end;
  662. }
  663. avctx->frame_number = 0;
  664. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  665. (!avctx->time_base.num || !avctx->time_base.den)) {
  666. avctx->time_base.num = 1;
  667. avctx->time_base.den = avctx->sample_rate;
  668. }
  669. if (HAVE_THREADS && !avctx->thread_opaque) {
  670. ret = ff_thread_init(avctx);
  671. if (ret < 0) {
  672. goto free_and_end;
  673. }
  674. }
  675. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  676. avctx->thread_count = 1;
  677. if (av_codec_is_encoder(avctx->codec)) {
  678. int i;
  679. if (avctx->codec->sample_fmts) {
  680. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  681. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  682. break;
  683. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  684. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  685. ret = AVERROR(EINVAL);
  686. goto free_and_end;
  687. }
  688. }
  689. if (avctx->codec->pix_fmts) {
  690. for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
  691. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  692. break;
  693. if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
  694. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  695. ret = AVERROR(EINVAL);
  696. goto free_and_end;
  697. }
  698. }
  699. if (avctx->codec->supported_samplerates) {
  700. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  701. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  702. break;
  703. if (avctx->codec->supported_samplerates[i] == 0) {
  704. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  705. ret = AVERROR(EINVAL);
  706. goto free_and_end;
  707. }
  708. }
  709. if (avctx->codec->channel_layouts) {
  710. if (!avctx->channel_layout) {
  711. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  712. } else {
  713. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  714. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  715. break;
  716. if (avctx->codec->channel_layouts[i] == 0) {
  717. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  718. ret = AVERROR(EINVAL);
  719. goto free_and_end;
  720. }
  721. }
  722. }
  723. if (avctx->channel_layout && avctx->channels) {
  724. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  725. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  726. ret = AVERROR(EINVAL);
  727. goto free_and_end;
  728. }
  729. } else if (avctx->channel_layout) {
  730. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  731. }
  732. }
  733. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  734. ret = avctx->codec->init(avctx);
  735. if (ret < 0) {
  736. goto free_and_end;
  737. }
  738. }
  739. if (av_codec_is_decoder(avctx->codec)) {
  740. /* validate channel layout from the decoder */
  741. if (avctx->channel_layout) {
  742. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  743. if (!avctx->channels)
  744. avctx->channels = channels;
  745. else if (channels != avctx->channels) {
  746. av_log(avctx, AV_LOG_WARNING,
  747. "channel layout does not match number of channels\n");
  748. avctx->channel_layout = 0;
  749. }
  750. }
  751. }
  752. end:
  753. entangled_thread_counter--;
  754. /* Release any user-supplied mutex. */
  755. if (ff_lockmgr_cb) {
  756. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  757. }
  758. if (options) {
  759. av_dict_free(options);
  760. *options = tmp;
  761. }
  762. return ret;
  763. free_and_end:
  764. av_dict_free(&tmp);
  765. av_freep(&avctx->priv_data);
  766. av_freep(&avctx->internal);
  767. avctx->codec = NULL;
  768. goto end;
  769. }
  770. int ff_alloc_packet(AVPacket *avpkt, int size)
  771. {
  772. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  773. return AVERROR(EINVAL);
  774. if (avpkt->data) {
  775. void *destruct = avpkt->destruct;
  776. if (avpkt->size < size)
  777. return AVERROR(EINVAL);
  778. av_init_packet(avpkt);
  779. avpkt->destruct = destruct;
  780. avpkt->size = size;
  781. return 0;
  782. } else {
  783. return av_new_packet(avpkt, size);
  784. }
  785. }
  786. /**
  787. * Pad last frame with silence.
  788. */
  789. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  790. {
  791. AVFrame *frame = NULL;
  792. uint8_t *buf = NULL;
  793. int ret;
  794. if (!(frame = avcodec_alloc_frame()))
  795. return AVERROR(ENOMEM);
  796. *frame = *src;
  797. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  798. s->frame_size, s->sample_fmt, 0)) < 0)
  799. goto fail;
  800. if (!(buf = av_malloc(ret))) {
  801. ret = AVERROR(ENOMEM);
  802. goto fail;
  803. }
  804. frame->nb_samples = s->frame_size;
  805. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  806. buf, ret, 0)) < 0)
  807. goto fail;
  808. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  809. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  810. goto fail;
  811. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  812. frame->nb_samples - src->nb_samples,
  813. s->channels, s->sample_fmt)) < 0)
  814. goto fail;
  815. *dst = frame;
  816. return 0;
  817. fail:
  818. if (frame->extended_data != frame->data)
  819. av_freep(&frame->extended_data);
  820. av_freep(&buf);
  821. av_freep(&frame);
  822. return ret;
  823. }
  824. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  825. AVPacket *avpkt,
  826. const AVFrame *frame,
  827. int *got_packet_ptr)
  828. {
  829. AVFrame tmp;
  830. AVFrame *padded_frame = NULL;
  831. int ret;
  832. int user_packet = !!avpkt->data;
  833. *got_packet_ptr = 0;
  834. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  835. av_free_packet(avpkt);
  836. av_init_packet(avpkt);
  837. return 0;
  838. }
  839. /* ensure that extended_data is properly set */
  840. if (frame && !frame->extended_data) {
  841. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  842. avctx->channels > AV_NUM_DATA_POINTERS) {
  843. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  844. "with more than %d channels, but extended_data is not set.\n",
  845. AV_NUM_DATA_POINTERS);
  846. return AVERROR(EINVAL);
  847. }
  848. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  849. tmp = *frame;
  850. tmp.extended_data = tmp.data;
  851. frame = &tmp;
  852. }
  853. /* check for valid frame size */
  854. if (frame) {
  855. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  856. if (frame->nb_samples > avctx->frame_size)
  857. return AVERROR(EINVAL);
  858. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  859. if (frame->nb_samples < avctx->frame_size &&
  860. !avctx->internal->last_audio_frame) {
  861. ret = pad_last_frame(avctx, &padded_frame, frame);
  862. if (ret < 0)
  863. return ret;
  864. frame = padded_frame;
  865. avctx->internal->last_audio_frame = 1;
  866. }
  867. if (frame->nb_samples != avctx->frame_size) {
  868. ret = AVERROR(EINVAL);
  869. goto end;
  870. }
  871. }
  872. }
  873. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  874. if (!ret) {
  875. if (*got_packet_ptr) {
  876. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  877. if (avpkt->pts == AV_NOPTS_VALUE)
  878. avpkt->pts = frame->pts;
  879. if (!avpkt->duration)
  880. avpkt->duration = ff_samples_to_time_base(avctx,
  881. frame->nb_samples);
  882. }
  883. avpkt->dts = avpkt->pts;
  884. } else {
  885. avpkt->size = 0;
  886. }
  887. if (!user_packet && avpkt->size) {
  888. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  889. if (new_data)
  890. avpkt->data = new_data;
  891. }
  892. avctx->frame_number++;
  893. }
  894. if (ret < 0 || !*got_packet_ptr) {
  895. av_free_packet(avpkt);
  896. av_init_packet(avpkt);
  897. goto end;
  898. }
  899. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  900. * this needs to be moved to the encoders, but for now we can do it
  901. * here to simplify things */
  902. avpkt->flags |= AV_PKT_FLAG_KEY;
  903. end:
  904. if (padded_frame) {
  905. av_freep(&padded_frame->data[0]);
  906. if (padded_frame->extended_data != padded_frame->data)
  907. av_freep(&padded_frame->extended_data);
  908. av_freep(&padded_frame);
  909. }
  910. return ret;
  911. }
  912. #if FF_API_OLD_DECODE_AUDIO
  913. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  914. uint8_t *buf, int buf_size,
  915. const short *samples)
  916. {
  917. AVPacket pkt;
  918. AVFrame frame0;
  919. AVFrame *frame;
  920. int ret, samples_size, got_packet;
  921. av_init_packet(&pkt);
  922. pkt.data = buf;
  923. pkt.size = buf_size;
  924. if (samples) {
  925. frame = &frame0;
  926. avcodec_get_frame_defaults(frame);
  927. if (avctx->frame_size) {
  928. frame->nb_samples = avctx->frame_size;
  929. } else {
  930. /* if frame_size is not set, the number of samples must be
  931. * calculated from the buffer size */
  932. int64_t nb_samples;
  933. if (!av_get_bits_per_sample(avctx->codec_id)) {
  934. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  935. "support this codec\n");
  936. return AVERROR(EINVAL);
  937. }
  938. nb_samples = (int64_t)buf_size * 8 /
  939. (av_get_bits_per_sample(avctx->codec_id) *
  940. avctx->channels);
  941. if (nb_samples >= INT_MAX)
  942. return AVERROR(EINVAL);
  943. frame->nb_samples = nb_samples;
  944. }
  945. /* it is assumed that the samples buffer is large enough based on the
  946. * relevant parameters */
  947. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  948. frame->nb_samples,
  949. avctx->sample_fmt, 1);
  950. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  951. avctx->sample_fmt,
  952. (const uint8_t *)samples,
  953. samples_size, 1)))
  954. return ret;
  955. /* fabricate frame pts from sample count.
  956. * this is needed because the avcodec_encode_audio() API does not have
  957. * a way for the user to provide pts */
  958. frame->pts = ff_samples_to_time_base(avctx,
  959. avctx->internal->sample_count);
  960. avctx->internal->sample_count += frame->nb_samples;
  961. } else {
  962. frame = NULL;
  963. }
  964. got_packet = 0;
  965. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  966. if (!ret && got_packet && avctx->coded_frame) {
  967. avctx->coded_frame->pts = pkt.pts;
  968. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  969. }
  970. /* free any side data since we cannot return it */
  971. if (pkt.side_data_elems > 0) {
  972. int i;
  973. for (i = 0; i < pkt.side_data_elems; i++)
  974. av_free(pkt.side_data[i].data);
  975. av_freep(&pkt.side_data);
  976. pkt.side_data_elems = 0;
  977. }
  978. if (frame && frame->extended_data != frame->data)
  979. av_free(frame->extended_data);
  980. return ret ? ret : pkt.size;
  981. }
  982. #endif
  983. #if FF_API_OLD_ENCODE_VIDEO
  984. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  985. const AVFrame *pict)
  986. {
  987. AVPacket pkt;
  988. int ret, got_packet = 0;
  989. if (buf_size < FF_MIN_BUFFER_SIZE) {
  990. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  991. return -1;
  992. }
  993. av_init_packet(&pkt);
  994. pkt.data = buf;
  995. pkt.size = buf_size;
  996. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  997. if (!ret && got_packet && avctx->coded_frame) {
  998. avctx->coded_frame->pts = pkt.pts;
  999. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1000. }
  1001. /* free any side data since we cannot return it */
  1002. if (pkt.side_data_elems > 0) {
  1003. int i;
  1004. for (i = 0; i < pkt.side_data_elems; i++)
  1005. av_free(pkt.side_data[i].data);
  1006. av_freep(&pkt.side_data);
  1007. pkt.side_data_elems = 0;
  1008. }
  1009. return ret ? ret : pkt.size;
  1010. }
  1011. #endif
  1012. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1013. AVPacket *avpkt,
  1014. const AVFrame *frame,
  1015. int *got_packet_ptr)
  1016. {
  1017. int ret;
  1018. int user_packet = !!avpkt->data;
  1019. *got_packet_ptr = 0;
  1020. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1021. av_free_packet(avpkt);
  1022. av_init_packet(avpkt);
  1023. avpkt->size = 0;
  1024. return 0;
  1025. }
  1026. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1027. return AVERROR(EINVAL);
  1028. av_assert0(avctx->codec->encode2);
  1029. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1030. if (!ret) {
  1031. if (!*got_packet_ptr)
  1032. avpkt->size = 0;
  1033. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1034. avpkt->pts = avpkt->dts = frame->pts;
  1035. if (!user_packet && avpkt->size) {
  1036. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  1037. if (new_data)
  1038. avpkt->data = new_data;
  1039. }
  1040. avctx->frame_number++;
  1041. }
  1042. if (ret < 0 || !*got_packet_ptr)
  1043. av_free_packet(avpkt);
  1044. emms_c();
  1045. return ret;
  1046. }
  1047. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1048. const AVSubtitle *sub)
  1049. {
  1050. int ret;
  1051. if (sub->start_display_time) {
  1052. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1053. return -1;
  1054. }
  1055. if (sub->num_rects == 0 || !sub->rects)
  1056. return -1;
  1057. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1058. avctx->frame_number++;
  1059. return ret;
  1060. }
  1061. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1062. {
  1063. int size = 0;
  1064. const uint8_t *data;
  1065. uint32_t flags;
  1066. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1067. return;
  1068. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1069. if (!data || size < 4)
  1070. return;
  1071. flags = bytestream_get_le32(&data);
  1072. size -= 4;
  1073. if (size < 4) /* Required for any of the changes */
  1074. return;
  1075. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1076. avctx->channels = bytestream_get_le32(&data);
  1077. size -= 4;
  1078. }
  1079. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1080. if (size < 8)
  1081. return;
  1082. avctx->channel_layout = bytestream_get_le64(&data);
  1083. size -= 8;
  1084. }
  1085. if (size < 4)
  1086. return;
  1087. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1088. avctx->sample_rate = bytestream_get_le32(&data);
  1089. size -= 4;
  1090. }
  1091. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1092. if (size < 8)
  1093. return;
  1094. avctx->width = bytestream_get_le32(&data);
  1095. avctx->height = bytestream_get_le32(&data);
  1096. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1097. size -= 8;
  1098. }
  1099. }
  1100. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1101. int *got_picture_ptr,
  1102. AVPacket *avpkt)
  1103. {
  1104. int ret;
  1105. *got_picture_ptr = 0;
  1106. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1107. return -1;
  1108. avctx->pkt = avpkt;
  1109. apply_param_change(avctx, avpkt);
  1110. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1111. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1112. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1113. avpkt);
  1114. else {
  1115. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1116. avpkt);
  1117. picture->pkt_dts = avpkt->dts;
  1118. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1119. picture->width = avctx->width;
  1120. picture->height = avctx->height;
  1121. picture->format = avctx->pix_fmt;
  1122. }
  1123. emms_c(); //needed to avoid an emms_c() call before every return;
  1124. if (*got_picture_ptr)
  1125. avctx->frame_number++;
  1126. } else
  1127. ret = 0;
  1128. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1129. * make sure it's set correctly */
  1130. picture->extended_data = picture->data;
  1131. return ret;
  1132. }
  1133. #if FF_API_OLD_DECODE_AUDIO
  1134. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1135. int *frame_size_ptr,
  1136. AVPacket *avpkt)
  1137. {
  1138. AVFrame frame;
  1139. int ret, got_frame = 0;
  1140. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1141. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1142. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1143. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1144. "avcodec_decode_audio4()\n");
  1145. avctx->get_buffer = avcodec_default_get_buffer;
  1146. }
  1147. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1148. if (ret >= 0 && got_frame) {
  1149. int ch, plane_size;
  1150. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1151. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1152. frame.nb_samples,
  1153. avctx->sample_fmt, 1);
  1154. if (*frame_size_ptr < data_size) {
  1155. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1156. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1157. return AVERROR(EINVAL);
  1158. }
  1159. memcpy(samples, frame.extended_data[0], plane_size);
  1160. if (planar && avctx->channels > 1) {
  1161. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1162. for (ch = 1; ch < avctx->channels; ch++) {
  1163. memcpy(out, frame.extended_data[ch], plane_size);
  1164. out += plane_size;
  1165. }
  1166. }
  1167. *frame_size_ptr = data_size;
  1168. } else {
  1169. *frame_size_ptr = 0;
  1170. }
  1171. return ret;
  1172. }
  1173. #endif
  1174. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1175. AVFrame *frame,
  1176. int *got_frame_ptr,
  1177. AVPacket *avpkt)
  1178. {
  1179. int planar, channels;
  1180. int ret = 0;
  1181. *got_frame_ptr = 0;
  1182. avctx->pkt = avpkt;
  1183. if (!avpkt->data && avpkt->size) {
  1184. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1185. return AVERROR(EINVAL);
  1186. }
  1187. apply_param_change(avctx, avpkt);
  1188. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1189. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1190. if (ret >= 0 && *got_frame_ptr) {
  1191. avctx->frame_number++;
  1192. frame->pkt_dts = avpkt->dts;
  1193. if (frame->format == AV_SAMPLE_FMT_NONE)
  1194. frame->format = avctx->sample_fmt;
  1195. }
  1196. }
  1197. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1198. * make sure it's set correctly; assume decoders that actually use
  1199. * extended_data are doing it correctly */
  1200. planar = av_sample_fmt_is_planar(frame->format);
  1201. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1202. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1203. frame->extended_data = frame->data;
  1204. return ret;
  1205. }
  1206. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1207. int *got_sub_ptr,
  1208. AVPacket *avpkt)
  1209. {
  1210. int ret;
  1211. avctx->pkt = avpkt;
  1212. *got_sub_ptr = 0;
  1213. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1214. if (*got_sub_ptr)
  1215. avctx->frame_number++;
  1216. return ret;
  1217. }
  1218. void avsubtitle_free(AVSubtitle *sub)
  1219. {
  1220. int i;
  1221. for (i = 0; i < sub->num_rects; i++) {
  1222. av_freep(&sub->rects[i]->pict.data[0]);
  1223. av_freep(&sub->rects[i]->pict.data[1]);
  1224. av_freep(&sub->rects[i]->pict.data[2]);
  1225. av_freep(&sub->rects[i]->pict.data[3]);
  1226. av_freep(&sub->rects[i]->text);
  1227. av_freep(&sub->rects[i]->ass);
  1228. av_freep(&sub->rects[i]);
  1229. }
  1230. av_freep(&sub->rects);
  1231. memset(sub, 0, sizeof(AVSubtitle));
  1232. }
  1233. av_cold int avcodec_close(AVCodecContext *avctx)
  1234. {
  1235. /* If there is a user-supplied mutex locking routine, call it. */
  1236. if (ff_lockmgr_cb) {
  1237. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1238. return -1;
  1239. }
  1240. entangled_thread_counter++;
  1241. if (entangled_thread_counter != 1) {
  1242. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1243. entangled_thread_counter--;
  1244. return -1;
  1245. }
  1246. if (avcodec_is_open(avctx)) {
  1247. if (HAVE_THREADS && avctx->thread_opaque)
  1248. ff_thread_free(avctx);
  1249. if (avctx->codec && avctx->codec->close)
  1250. avctx->codec->close(avctx);
  1251. avcodec_default_free_buffers(avctx);
  1252. avctx->coded_frame = NULL;
  1253. av_freep(&avctx->internal);
  1254. }
  1255. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1256. av_opt_free(avctx->priv_data);
  1257. av_opt_free(avctx);
  1258. av_freep(&avctx->priv_data);
  1259. if (av_codec_is_encoder(avctx->codec))
  1260. av_freep(&avctx->extradata);
  1261. avctx->codec = NULL;
  1262. avctx->active_thread_type = 0;
  1263. entangled_thread_counter--;
  1264. /* Release any user-supplied mutex. */
  1265. if (ff_lockmgr_cb) {
  1266. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1267. }
  1268. return 0;
  1269. }
  1270. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1271. {
  1272. AVCodec *p, *experimental = NULL;
  1273. p = first_avcodec;
  1274. while (p) {
  1275. if (av_codec_is_encoder(p) && p->id == id) {
  1276. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1277. experimental = p;
  1278. } else
  1279. return p;
  1280. }
  1281. p = p->next;
  1282. }
  1283. return experimental;
  1284. }
  1285. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1286. {
  1287. AVCodec *p;
  1288. if (!name)
  1289. return NULL;
  1290. p = first_avcodec;
  1291. while (p) {
  1292. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1293. return p;
  1294. p = p->next;
  1295. }
  1296. return NULL;
  1297. }
  1298. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1299. {
  1300. AVCodec *p;
  1301. p = first_avcodec;
  1302. while (p) {
  1303. if (av_codec_is_decoder(p) && p->id == id)
  1304. return p;
  1305. p = p->next;
  1306. }
  1307. return NULL;
  1308. }
  1309. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1310. {
  1311. AVCodec *p;
  1312. if (!name)
  1313. return NULL;
  1314. p = first_avcodec;
  1315. while (p) {
  1316. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1317. return p;
  1318. p = p->next;
  1319. }
  1320. return NULL;
  1321. }
  1322. static int get_bit_rate(AVCodecContext *ctx)
  1323. {
  1324. int bit_rate;
  1325. int bits_per_sample;
  1326. switch (ctx->codec_type) {
  1327. case AVMEDIA_TYPE_VIDEO:
  1328. case AVMEDIA_TYPE_DATA:
  1329. case AVMEDIA_TYPE_SUBTITLE:
  1330. case AVMEDIA_TYPE_ATTACHMENT:
  1331. bit_rate = ctx->bit_rate;
  1332. break;
  1333. case AVMEDIA_TYPE_AUDIO:
  1334. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1335. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1336. break;
  1337. default:
  1338. bit_rate = 0;
  1339. break;
  1340. }
  1341. return bit_rate;
  1342. }
  1343. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1344. {
  1345. int i, len, ret = 0;
  1346. for (i = 0; i < 4; i++) {
  1347. len = snprintf(buf, buf_size,
  1348. isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1349. buf += len;
  1350. buf_size = buf_size > len ? buf_size - len : 0;
  1351. ret += len;
  1352. codec_tag >>= 8;
  1353. }
  1354. return ret;
  1355. }
  1356. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1357. {
  1358. const char *codec_name;
  1359. const char *profile = NULL;
  1360. const AVCodec *p;
  1361. char buf1[32];
  1362. int bitrate;
  1363. AVRational display_aspect_ratio;
  1364. if (enc->codec)
  1365. p = enc->codec;
  1366. else if (encode)
  1367. p = avcodec_find_encoder(enc->codec_id);
  1368. else
  1369. p = avcodec_find_decoder(enc->codec_id);
  1370. if (p) {
  1371. codec_name = p->name;
  1372. profile = av_get_profile_name(p, enc->profile);
  1373. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1374. /* fake mpeg2 transport stream codec (currently not
  1375. * registered) */
  1376. codec_name = "mpeg2ts";
  1377. } else if (enc->codec_name[0] != '\0') {
  1378. codec_name = enc->codec_name;
  1379. } else {
  1380. /* output avi tags */
  1381. char tag_buf[32];
  1382. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1383. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1384. codec_name = buf1;
  1385. }
  1386. switch (enc->codec_type) {
  1387. case AVMEDIA_TYPE_VIDEO:
  1388. snprintf(buf, buf_size,
  1389. "Video: %s%s",
  1390. codec_name, enc->mb_decision ? " (hq)" : "");
  1391. if (profile)
  1392. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1393. " (%s)", profile);
  1394. if (enc->pix_fmt != PIX_FMT_NONE) {
  1395. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1396. ", %s",
  1397. av_get_pix_fmt_name(enc->pix_fmt));
  1398. }
  1399. if (enc->width) {
  1400. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1401. ", %dx%d",
  1402. enc->width, enc->height);
  1403. if (enc->sample_aspect_ratio.num) {
  1404. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1405. enc->width * enc->sample_aspect_ratio.num,
  1406. enc->height * enc->sample_aspect_ratio.den,
  1407. 1024 * 1024);
  1408. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1409. " [PAR %d:%d DAR %d:%d]",
  1410. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1411. display_aspect_ratio.num, display_aspect_ratio.den);
  1412. }
  1413. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1414. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1415. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1416. ", %d/%d",
  1417. enc->time_base.num / g, enc->time_base.den / g);
  1418. }
  1419. }
  1420. if (encode) {
  1421. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1422. ", q=%d-%d", enc->qmin, enc->qmax);
  1423. }
  1424. break;
  1425. case AVMEDIA_TYPE_AUDIO:
  1426. snprintf(buf, buf_size,
  1427. "Audio: %s",
  1428. codec_name);
  1429. if (profile)
  1430. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1431. " (%s)", profile);
  1432. if (enc->sample_rate) {
  1433. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1434. ", %d Hz", enc->sample_rate);
  1435. }
  1436. av_strlcat(buf, ", ", buf_size);
  1437. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1438. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1439. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1440. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1441. }
  1442. break;
  1443. case AVMEDIA_TYPE_DATA:
  1444. snprintf(buf, buf_size, "Data: %s", codec_name);
  1445. break;
  1446. case AVMEDIA_TYPE_SUBTITLE:
  1447. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1448. break;
  1449. case AVMEDIA_TYPE_ATTACHMENT:
  1450. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1451. break;
  1452. default:
  1453. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1454. return;
  1455. }
  1456. if (encode) {
  1457. if (enc->flags & CODEC_FLAG_PASS1)
  1458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1459. ", pass 1");
  1460. if (enc->flags & CODEC_FLAG_PASS2)
  1461. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1462. ", pass 2");
  1463. }
  1464. bitrate = get_bit_rate(enc);
  1465. if (bitrate != 0) {
  1466. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1467. ", %d kb/s", bitrate / 1000);
  1468. }
  1469. }
  1470. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1471. {
  1472. const AVProfile *p;
  1473. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1474. return NULL;
  1475. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1476. if (p->profile == profile)
  1477. return p->name;
  1478. return NULL;
  1479. }
  1480. unsigned avcodec_version(void)
  1481. {
  1482. return LIBAVCODEC_VERSION_INT;
  1483. }
  1484. const char *avcodec_configuration(void)
  1485. {
  1486. return LIBAV_CONFIGURATION;
  1487. }
  1488. const char *avcodec_license(void)
  1489. {
  1490. #define LICENSE_PREFIX "libavcodec license: "
  1491. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1492. }
  1493. void avcodec_flush_buffers(AVCodecContext *avctx)
  1494. {
  1495. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1496. ff_thread_flush(avctx);
  1497. else if (avctx->codec->flush)
  1498. avctx->codec->flush(avctx);
  1499. }
  1500. static void video_free_buffers(AVCodecContext *s)
  1501. {
  1502. AVCodecInternal *avci = s->internal;
  1503. int i, j;
  1504. if (!avci->buffer)
  1505. return;
  1506. if (avci->buffer_count)
  1507. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1508. avci->buffer_count);
  1509. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1510. InternalBuffer *buf = &avci->buffer[i];
  1511. for (j = 0; j < 4; j++) {
  1512. av_freep(&buf->base[j]);
  1513. buf->data[j] = NULL;
  1514. }
  1515. }
  1516. av_freep(&avci->buffer);
  1517. avci->buffer_count = 0;
  1518. }
  1519. static void audio_free_buffers(AVCodecContext *avctx)
  1520. {
  1521. AVCodecInternal *avci = avctx->internal;
  1522. InternalBuffer *buf;
  1523. if (!avci->buffer)
  1524. return;
  1525. buf = avci->buffer;
  1526. if (buf->extended_data) {
  1527. av_free(buf->extended_data[0]);
  1528. if (buf->extended_data != buf->data)
  1529. av_free(buf->extended_data);
  1530. }
  1531. av_freep(&avci->buffer);
  1532. }
  1533. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1534. {
  1535. switch (avctx->codec_type) {
  1536. case AVMEDIA_TYPE_VIDEO:
  1537. video_free_buffers(avctx);
  1538. break;
  1539. case AVMEDIA_TYPE_AUDIO:
  1540. audio_free_buffers(avctx);
  1541. break;
  1542. default:
  1543. break;
  1544. }
  1545. }
  1546. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1547. {
  1548. switch (codec_id) {
  1549. case AV_CODEC_ID_ADPCM_CT:
  1550. case AV_CODEC_ID_ADPCM_IMA_APC:
  1551. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1552. case AV_CODEC_ID_ADPCM_IMA_WS:
  1553. case AV_CODEC_ID_ADPCM_G722:
  1554. case AV_CODEC_ID_ADPCM_YAMAHA:
  1555. return 4;
  1556. case AV_CODEC_ID_PCM_ALAW:
  1557. case AV_CODEC_ID_PCM_MULAW:
  1558. case AV_CODEC_ID_PCM_S8:
  1559. case AV_CODEC_ID_PCM_U8:
  1560. case AV_CODEC_ID_PCM_ZORK:
  1561. return 8;
  1562. case AV_CODEC_ID_PCM_S16BE:
  1563. case AV_CODEC_ID_PCM_S16LE:
  1564. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1565. case AV_CODEC_ID_PCM_U16BE:
  1566. case AV_CODEC_ID_PCM_U16LE:
  1567. return 16;
  1568. case AV_CODEC_ID_PCM_S24DAUD:
  1569. case AV_CODEC_ID_PCM_S24BE:
  1570. case AV_CODEC_ID_PCM_S24LE:
  1571. case AV_CODEC_ID_PCM_U24BE:
  1572. case AV_CODEC_ID_PCM_U24LE:
  1573. return 24;
  1574. case AV_CODEC_ID_PCM_S32BE:
  1575. case AV_CODEC_ID_PCM_S32LE:
  1576. case AV_CODEC_ID_PCM_U32BE:
  1577. case AV_CODEC_ID_PCM_U32LE:
  1578. case AV_CODEC_ID_PCM_F32BE:
  1579. case AV_CODEC_ID_PCM_F32LE:
  1580. return 32;
  1581. case AV_CODEC_ID_PCM_F64BE:
  1582. case AV_CODEC_ID_PCM_F64LE:
  1583. return 64;
  1584. default:
  1585. return 0;
  1586. }
  1587. }
  1588. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1589. {
  1590. switch (codec_id) {
  1591. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1592. return 2;
  1593. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1594. return 3;
  1595. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1596. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1597. case AV_CODEC_ID_ADPCM_IMA_QT:
  1598. case AV_CODEC_ID_ADPCM_SWF:
  1599. case AV_CODEC_ID_ADPCM_MS:
  1600. return 4;
  1601. default:
  1602. return av_get_exact_bits_per_sample(codec_id);
  1603. }
  1604. }
  1605. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1606. {
  1607. int id, sr, ch, ba, tag, bps;
  1608. id = avctx->codec_id;
  1609. sr = avctx->sample_rate;
  1610. ch = avctx->channels;
  1611. ba = avctx->block_align;
  1612. tag = avctx->codec_tag;
  1613. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1614. /* codecs with an exact constant bits per sample */
  1615. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1616. return (frame_bytes * 8) / (bps * ch);
  1617. bps = avctx->bits_per_coded_sample;
  1618. /* codecs with a fixed packet duration */
  1619. switch (id) {
  1620. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1621. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1622. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1623. case AV_CODEC_ID_AMR_NB:
  1624. case AV_CODEC_ID_GSM:
  1625. case AV_CODEC_ID_QCELP:
  1626. case AV_CODEC_ID_RA_144:
  1627. case AV_CODEC_ID_RA_288: return 160;
  1628. case AV_CODEC_ID_IMC: return 256;
  1629. case AV_CODEC_ID_AMR_WB:
  1630. case AV_CODEC_ID_GSM_MS: return 320;
  1631. case AV_CODEC_ID_MP1: return 384;
  1632. case AV_CODEC_ID_ATRAC1: return 512;
  1633. case AV_CODEC_ID_ATRAC3: return 1024;
  1634. case AV_CODEC_ID_MP2:
  1635. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1636. case AV_CODEC_ID_AC3: return 1536;
  1637. }
  1638. if (sr > 0) {
  1639. /* calc from sample rate */
  1640. if (id == AV_CODEC_ID_TTA)
  1641. return 256 * sr / 245;
  1642. if (ch > 0) {
  1643. /* calc from sample rate and channels */
  1644. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1645. return (480 << (sr / 22050)) / ch;
  1646. }
  1647. }
  1648. if (ba > 0) {
  1649. /* calc from block_align */
  1650. if (id == AV_CODEC_ID_SIPR) {
  1651. switch (ba) {
  1652. case 20: return 160;
  1653. case 19: return 144;
  1654. case 29: return 288;
  1655. case 37: return 480;
  1656. }
  1657. } else if (id == AV_CODEC_ID_ILBC) {
  1658. switch (ba) {
  1659. case 38: return 160;
  1660. case 50: return 240;
  1661. }
  1662. }
  1663. }
  1664. if (frame_bytes > 0) {
  1665. /* calc from frame_bytes only */
  1666. if (id == AV_CODEC_ID_TRUESPEECH)
  1667. return 240 * (frame_bytes / 32);
  1668. if (id == AV_CODEC_ID_NELLYMOSER)
  1669. return 256 * (frame_bytes / 64);
  1670. if (bps > 0) {
  1671. /* calc from frame_bytes and bits_per_coded_sample */
  1672. if (id == AV_CODEC_ID_ADPCM_G726)
  1673. return frame_bytes * 8 / bps;
  1674. }
  1675. if (ch > 0) {
  1676. /* calc from frame_bytes and channels */
  1677. switch (id) {
  1678. case AV_CODEC_ID_ADPCM_4XM:
  1679. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1680. return (frame_bytes - 4 * ch) * 2 / ch;
  1681. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1682. return (frame_bytes - 4) * 2 / ch;
  1683. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1684. return (frame_bytes - 8) * 2 / ch;
  1685. case AV_CODEC_ID_ADPCM_XA:
  1686. return (frame_bytes / 128) * 224 / ch;
  1687. case AV_CODEC_ID_INTERPLAY_DPCM:
  1688. return (frame_bytes - 6 - ch) / ch;
  1689. case AV_CODEC_ID_ROQ_DPCM:
  1690. return (frame_bytes - 8) / ch;
  1691. case AV_CODEC_ID_XAN_DPCM:
  1692. return (frame_bytes - 2 * ch) / ch;
  1693. case AV_CODEC_ID_MACE3:
  1694. return 3 * frame_bytes / ch;
  1695. case AV_CODEC_ID_MACE6:
  1696. return 6 * frame_bytes / ch;
  1697. case AV_CODEC_ID_PCM_LXF:
  1698. return 2 * (frame_bytes / (5 * ch));
  1699. }
  1700. if (tag) {
  1701. /* calc from frame_bytes, channels, and codec_tag */
  1702. if (id == AV_CODEC_ID_SOL_DPCM) {
  1703. if (tag == 3)
  1704. return frame_bytes / ch;
  1705. else
  1706. return frame_bytes * 2 / ch;
  1707. }
  1708. }
  1709. if (ba > 0) {
  1710. /* calc from frame_bytes, channels, and block_align */
  1711. int blocks = frame_bytes / ba;
  1712. switch (avctx->codec_id) {
  1713. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1714. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1715. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1716. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1717. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1718. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1719. case AV_CODEC_ID_ADPCM_MS:
  1720. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1721. }
  1722. }
  1723. if (bps > 0) {
  1724. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1725. switch (avctx->codec_id) {
  1726. case AV_CODEC_ID_PCM_DVD:
  1727. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1728. case AV_CODEC_ID_PCM_BLURAY:
  1729. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1730. case AV_CODEC_ID_S302M:
  1731. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1732. }
  1733. }
  1734. }
  1735. }
  1736. return 0;
  1737. }
  1738. #if !HAVE_THREADS
  1739. int ff_thread_init(AVCodecContext *s)
  1740. {
  1741. return -1;
  1742. }
  1743. #endif
  1744. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1745. {
  1746. unsigned int n = 0;
  1747. while (v >= 0xff) {
  1748. *s++ = 0xff;
  1749. v -= 0xff;
  1750. n++;
  1751. }
  1752. *s = v;
  1753. n++;
  1754. return n;
  1755. }
  1756. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1757. {
  1758. int i;
  1759. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1760. return i;
  1761. }
  1762. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1763. {
  1764. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1765. "version to the newest one from Git. If the problem still "
  1766. "occurs, it means that your file has a feature which has not "
  1767. "been implemented.\n", feature);
  1768. if(want_sample)
  1769. av_log_ask_for_sample(avc, NULL);
  1770. }
  1771. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1772. {
  1773. va_list argument_list;
  1774. va_start(argument_list, msg);
  1775. if (msg)
  1776. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1777. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1778. "of this file to ftp://upload.libav.org/incoming/ "
  1779. "and contact the libav-devel mailing list.\n");
  1780. va_end(argument_list);
  1781. }
  1782. static AVHWAccel *first_hwaccel = NULL;
  1783. void av_register_hwaccel(AVHWAccel *hwaccel)
  1784. {
  1785. AVHWAccel **p = &first_hwaccel;
  1786. while (*p)
  1787. p = &(*p)->next;
  1788. *p = hwaccel;
  1789. hwaccel->next = NULL;
  1790. }
  1791. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1792. {
  1793. return hwaccel ? hwaccel->next : first_hwaccel;
  1794. }
  1795. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
  1796. {
  1797. AVHWAccel *hwaccel = NULL;
  1798. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1799. if (hwaccel->id == codec_id
  1800. && hwaccel->pix_fmt == pix_fmt)
  1801. return hwaccel;
  1802. return NULL;
  1803. }
  1804. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1805. {
  1806. if (ff_lockmgr_cb) {
  1807. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1808. return -1;
  1809. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1810. return -1;
  1811. }
  1812. ff_lockmgr_cb = cb;
  1813. if (ff_lockmgr_cb) {
  1814. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1815. return -1;
  1816. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1817. return -1;
  1818. }
  1819. return 0;
  1820. }
  1821. int avpriv_lock_avformat(void)
  1822. {
  1823. if (ff_lockmgr_cb) {
  1824. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1825. return -1;
  1826. }
  1827. return 0;
  1828. }
  1829. int avpriv_unlock_avformat(void)
  1830. {
  1831. if (ff_lockmgr_cb) {
  1832. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1833. return -1;
  1834. }
  1835. return 0;
  1836. }
  1837. unsigned int avpriv_toupper4(unsigned int x)
  1838. {
  1839. return toupper(x & 0xFF)
  1840. + (toupper((x >> 8) & 0xFF) << 8)
  1841. + (toupper((x >> 16) & 0xFF) << 16)
  1842. + (toupper((x >> 24) & 0xFF) << 24);
  1843. }
  1844. #if !HAVE_THREADS
  1845. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1846. {
  1847. f->owner = avctx;
  1848. return avctx->get_buffer(avctx, f);
  1849. }
  1850. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1851. {
  1852. f->owner->release_buffer(f->owner, f);
  1853. }
  1854. void ff_thread_finish_setup(AVCodecContext *avctx)
  1855. {
  1856. }
  1857. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1858. {
  1859. }
  1860. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1861. {
  1862. }
  1863. #endif
  1864. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1865. {
  1866. if (codec_id <= AV_CODEC_ID_NONE)
  1867. return AVMEDIA_TYPE_UNKNOWN;
  1868. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1869. return AVMEDIA_TYPE_VIDEO;
  1870. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1871. return AVMEDIA_TYPE_AUDIO;
  1872. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1873. return AVMEDIA_TYPE_SUBTITLE;
  1874. return AVMEDIA_TYPE_UNKNOWN;
  1875. }
  1876. int avcodec_is_open(AVCodecContext *s)
  1877. {
  1878. return !!s->internal;
  1879. }