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.

2072 lines
63KB

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