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.

2126 lines
64KB

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