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.

2176 lines
66KB

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