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.

2067 lines
62KB

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