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.

1385 lines
41KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  49. {
  50. if(min_size < *size)
  51. return ptr;
  52. min_size= FFMAX(17*min_size/16 + 32, min_size);
  53. ptr= av_realloc(ptr, min_size);
  54. 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
  55. min_size= 0;
  56. *size= min_size;
  57. return ptr;
  58. }
  59. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  60. {
  61. void **p = ptr;
  62. if (min_size < *size)
  63. return;
  64. min_size= FFMAX(17*min_size/16 + 32, min_size);
  65. av_free(*p);
  66. *p = av_malloc(min_size);
  67. if (!*p) min_size = 0;
  68. *size= min_size;
  69. }
  70. /* encoder management */
  71. static AVCodec *first_avcodec = NULL;
  72. AVCodec *av_codec_next(AVCodec *c){
  73. if(c) return c->next;
  74. else return first_avcodec;
  75. }
  76. void avcodec_register(AVCodec *codec)
  77. {
  78. AVCodec **p;
  79. avcodec_init();
  80. p = &first_avcodec;
  81. while (*p != NULL) p = &(*p)->next;
  82. *p = codec;
  83. codec->next = NULL;
  84. }
  85. unsigned avcodec_get_edge_width(void)
  86. {
  87. return EDGE_WIDTH;
  88. }
  89. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  90. s->coded_width = width;
  91. s->coded_height= height;
  92. s->width = -((-width )>>s->lowres);
  93. s->height= -((-height)>>s->lowres);
  94. }
  95. typedef struct InternalBuffer{
  96. int last_pic_num;
  97. uint8_t *base[4];
  98. uint8_t *data[4];
  99. int linesize[4];
  100. int width, height;
  101. enum PixelFormat pix_fmt;
  102. }InternalBuffer;
  103. #define INTERNAL_BUFFER_SIZE (32+1)
  104. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  105. int w_align= 1;
  106. int h_align= 1;
  107. switch(s->pix_fmt){
  108. case PIX_FMT_YUV420P:
  109. case PIX_FMT_YUYV422:
  110. case PIX_FMT_UYVY422:
  111. case PIX_FMT_YUV422P:
  112. case PIX_FMT_YUV440P:
  113. case PIX_FMT_YUV444P:
  114. case PIX_FMT_GRAY8:
  115. case PIX_FMT_GRAY16BE:
  116. case PIX_FMT_GRAY16LE:
  117. case PIX_FMT_YUVJ420P:
  118. case PIX_FMT_YUVJ422P:
  119. case PIX_FMT_YUVJ440P:
  120. case PIX_FMT_YUVJ444P:
  121. case PIX_FMT_YUVA420P:
  122. case PIX_FMT_YUV420P9LE:
  123. case PIX_FMT_YUV420P9BE:
  124. case PIX_FMT_YUV420P10LE:
  125. case PIX_FMT_YUV420P10BE:
  126. case PIX_FMT_YUV422P10LE:
  127. case PIX_FMT_YUV422P10BE:
  128. case PIX_FMT_YUV444P9LE:
  129. case PIX_FMT_YUV444P9BE:
  130. case PIX_FMT_YUV444P10LE:
  131. case PIX_FMT_YUV444P10BE:
  132. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  133. h_align= 16;
  134. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  135. h_align= 32; // interlaced is rounded up to 2 MBs
  136. break;
  137. case PIX_FMT_YUV411P:
  138. case PIX_FMT_UYYVYY411:
  139. w_align=32;
  140. h_align=8;
  141. break;
  142. case PIX_FMT_YUV410P:
  143. if(s->codec_id == CODEC_ID_SVQ1){
  144. w_align=64;
  145. h_align=64;
  146. }
  147. case PIX_FMT_RGB555:
  148. if(s->codec_id == CODEC_ID_RPZA){
  149. w_align=4;
  150. h_align=4;
  151. }
  152. case PIX_FMT_PAL8:
  153. case PIX_FMT_BGR8:
  154. case PIX_FMT_RGB8:
  155. if(s->codec_id == CODEC_ID_SMC){
  156. w_align=4;
  157. h_align=4;
  158. }
  159. break;
  160. case PIX_FMT_BGR24:
  161. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  162. w_align=4;
  163. h_align=4;
  164. }
  165. break;
  166. default:
  167. w_align= 1;
  168. h_align= 1;
  169. break;
  170. }
  171. *width = FFALIGN(*width , w_align);
  172. *height= FFALIGN(*height, h_align);
  173. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  174. *height+=2; // some of the optimized chroma MC reads one line too much
  175. // which is also done in mpeg decoders with lowres > 0
  176. linesize_align[0] =
  177. linesize_align[1] =
  178. linesize_align[2] =
  179. linesize_align[3] = STRIDE_ALIGN;
  180. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  181. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  182. //picture size unneccessarily in some cases. The solution here is not
  183. //pretty and better ideas are welcome!
  184. #if HAVE_MMX
  185. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  186. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  187. s->codec_id == CODEC_ID_VP6A) {
  188. linesize_align[0] =
  189. linesize_align[1] =
  190. linesize_align[2] = 16;
  191. }
  192. #endif
  193. }
  194. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  195. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  196. int linesize_align[4];
  197. int align;
  198. avcodec_align_dimensions2(s, width, height, linesize_align);
  199. align = FFMAX(linesize_align[0], linesize_align[3]);
  200. linesize_align[1] <<= chroma_shift;
  201. linesize_align[2] <<= chroma_shift;
  202. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  203. *width=FFALIGN(*width, align);
  204. }
  205. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  206. int i;
  207. int w= s->width;
  208. int h= s->height;
  209. InternalBuffer *buf;
  210. int *picture_number;
  211. if(pic->data[0]!=NULL) {
  212. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  213. return -1;
  214. }
  215. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  216. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  217. return -1;
  218. }
  219. if(av_image_check_size(w, h, 0, s))
  220. return -1;
  221. if(s->internal_buffer==NULL){
  222. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  223. }
  224. #if 0
  225. s->internal_buffer= av_fast_realloc(
  226. s->internal_buffer,
  227. &s->internal_buffer_size,
  228. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  229. );
  230. #endif
  231. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  232. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  233. (*picture_number)++;
  234. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  235. if(s->active_thread_type&FF_THREAD_FRAME) {
  236. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  237. return -1;
  238. }
  239. for(i=0; i<4; i++){
  240. av_freep(&buf->base[i]);
  241. buf->data[i]= NULL;
  242. }
  243. }
  244. if(buf->base[0]){
  245. pic->age= *picture_number - buf->last_pic_num;
  246. buf->last_pic_num= *picture_number;
  247. }else{
  248. int h_chroma_shift, v_chroma_shift;
  249. int size[4] = {0};
  250. int tmpsize;
  251. int unaligned;
  252. AVPicture picture;
  253. int stride_align[4];
  254. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  255. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  256. avcodec_align_dimensions2(s, &w, &h, stride_align);
  257. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  258. w+= EDGE_WIDTH*2;
  259. h+= EDGE_WIDTH*2;
  260. }
  261. do {
  262. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  263. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  264. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  265. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  266. w += w & ~(w-1);
  267. unaligned = 0;
  268. for (i=0; i<4; i++){
  269. unaligned |= picture.linesize[i] % stride_align[i];
  270. }
  271. } while (unaligned);
  272. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  273. if (tmpsize < 0)
  274. return -1;
  275. for (i=0; i<3 && picture.data[i+1]; i++)
  276. size[i] = picture.data[i+1] - picture.data[i];
  277. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  278. buf->last_pic_num= -256*256*256*64;
  279. memset(buf->base, 0, sizeof(buf->base));
  280. memset(buf->data, 0, sizeof(buf->data));
  281. for(i=0; i<4 && size[i]; i++){
  282. const int h_shift= i==0 ? 0 : h_chroma_shift;
  283. const int v_shift= i==0 ? 0 : v_chroma_shift;
  284. buf->linesize[i]= picture.linesize[i];
  285. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  286. if(buf->base[i]==NULL) return -1;
  287. memset(buf->base[i], 128, size[i]);
  288. // no edge if EDGE EMU or not planar YUV
  289. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  290. buf->data[i] = buf->base[i];
  291. else
  292. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  293. }
  294. if(size[1] && !size[2])
  295. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  296. buf->width = s->width;
  297. buf->height = s->height;
  298. buf->pix_fmt= s->pix_fmt;
  299. pic->age= 256*256*256*64;
  300. }
  301. pic->type= FF_BUFFER_TYPE_INTERNAL;
  302. for(i=0; i<4; i++){
  303. pic->base[i]= buf->base[i];
  304. pic->data[i]= buf->data[i];
  305. pic->linesize[i]= buf->linesize[i];
  306. }
  307. s->internal_buffer_count++;
  308. if (s->pkt) {
  309. pic->pkt_pts = s->pkt->pts;
  310. pic->pkt_pos = s->pkt->pos;
  311. } else {
  312. pic->pkt_pts = AV_NOPTS_VALUE;
  313. pic->pkt_pos = -1;
  314. }
  315. pic->reordered_opaque= s->reordered_opaque;
  316. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  317. pic->width = s->width;
  318. pic->height = s->height;
  319. pic->format = s->pix_fmt;
  320. if(s->debug&FF_DEBUG_BUFFERS)
  321. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  322. return 0;
  323. }
  324. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  325. int i;
  326. InternalBuffer *buf, *last;
  327. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  328. assert(s->internal_buffer_count);
  329. if(s->internal_buffer){
  330. buf = NULL; /* avoids warning */
  331. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  332. buf= &((InternalBuffer*)s->internal_buffer)[i];
  333. if(buf->data[0] == pic->data[0])
  334. break;
  335. }
  336. assert(i < s->internal_buffer_count);
  337. s->internal_buffer_count--;
  338. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  339. FFSWAP(InternalBuffer, *buf, *last);
  340. }
  341. for(i=0; i<4; i++){
  342. pic->data[i]=NULL;
  343. // pic->base[i]=NULL;
  344. }
  345. //printf("R%X\n", pic->opaque);
  346. if(s->debug&FF_DEBUG_BUFFERS)
  347. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  348. }
  349. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  350. AVFrame temp_pic;
  351. int i;
  352. /* If no picture return a new buffer */
  353. if(pic->data[0] == NULL) {
  354. /* We will copy from buffer, so must be readable */
  355. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  356. return s->get_buffer(s, pic);
  357. }
  358. /* If internal buffer type return the same buffer */
  359. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  360. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  361. else pic->pkt_pts= AV_NOPTS_VALUE;
  362. pic->reordered_opaque= s->reordered_opaque;
  363. return 0;
  364. }
  365. /*
  366. * Not internal type and reget_buffer not overridden, emulate cr buffer
  367. */
  368. temp_pic = *pic;
  369. for(i = 0; i < 4; i++)
  370. pic->data[i] = pic->base[i] = NULL;
  371. pic->opaque = NULL;
  372. /* Allocate new frame */
  373. if (s->get_buffer(s, pic))
  374. return -1;
  375. /* Copy image data from old buffer to new buffer */
  376. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  377. s->height);
  378. s->release_buffer(s, &temp_pic); // Release old frame
  379. return 0;
  380. }
  381. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  382. int i;
  383. for(i=0; i<count; i++){
  384. int r= func(c, (char*)arg + i*size);
  385. if(ret) ret[i]= r;
  386. }
  387. return 0;
  388. }
  389. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  390. int i;
  391. for(i=0; i<count; i++){
  392. int r= func(c, arg, i, 0);
  393. if(ret) ret[i]= r;
  394. }
  395. return 0;
  396. }
  397. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  398. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  399. ++fmt;
  400. return fmt[0];
  401. }
  402. void avcodec_get_frame_defaults(AVFrame *pic){
  403. memset(pic, 0, sizeof(AVFrame));
  404. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  405. pic->pkt_pos = -1;
  406. pic->key_frame= 1;
  407. pic->sample_aspect_ratio = (AVRational){0, 1};
  408. pic->format = -1; /* unknown */
  409. }
  410. AVFrame *avcodec_alloc_frame(void){
  411. AVFrame *pic= av_malloc(sizeof(AVFrame));
  412. if(pic==NULL) return NULL;
  413. avcodec_get_frame_defaults(pic);
  414. return pic;
  415. }
  416. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  417. {
  418. memset(sub, 0, sizeof(*sub));
  419. sub->pts = AV_NOPTS_VALUE;
  420. }
  421. #if FF_API_AVCODEC_OPEN
  422. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  423. {
  424. return avcodec_open2(avctx, codec, NULL);
  425. }
  426. #endif
  427. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  428. {
  429. int ret = 0;
  430. AVDictionary *tmp = NULL;
  431. if (options)
  432. av_dict_copy(&tmp, *options, 0);
  433. /* If there is a user-supplied mutex locking routine, call it. */
  434. if (ff_lockmgr_cb) {
  435. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  436. return -1;
  437. }
  438. entangled_thread_counter++;
  439. if(entangled_thread_counter != 1){
  440. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  441. ret = -1;
  442. goto end;
  443. }
  444. if(avctx->codec || !codec) {
  445. ret = AVERROR(EINVAL);
  446. goto end;
  447. }
  448. if (codec->priv_data_size > 0) {
  449. if(!avctx->priv_data){
  450. avctx->priv_data = av_mallocz(codec->priv_data_size);
  451. if (!avctx->priv_data) {
  452. ret = AVERROR(ENOMEM);
  453. goto end;
  454. }
  455. if (codec->priv_class) {
  456. *(AVClass**)avctx->priv_data= codec->priv_class;
  457. av_opt_set_defaults(avctx->priv_data);
  458. }
  459. }
  460. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  461. goto free_and_end;
  462. } else {
  463. avctx->priv_data = NULL;
  464. }
  465. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  466. goto free_and_end;
  467. if(avctx->coded_width && avctx->coded_height)
  468. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  469. else if(avctx->width && avctx->height)
  470. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  471. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  472. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  473. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  474. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  475. avcodec_set_dimensions(avctx, 0, 0);
  476. }
  477. /* if the decoder init function was already called previously,
  478. free the already allocated subtitle_header before overwriting it */
  479. if (codec->decode)
  480. av_freep(&avctx->subtitle_header);
  481. #define SANE_NB_CHANNELS 128U
  482. if (avctx->channels > SANE_NB_CHANNELS) {
  483. ret = AVERROR(EINVAL);
  484. goto free_and_end;
  485. }
  486. avctx->codec = codec;
  487. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  488. avctx->codec_id == CODEC_ID_NONE) {
  489. avctx->codec_type = codec->type;
  490. avctx->codec_id = codec->id;
  491. }
  492. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  493. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  494. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  495. ret = AVERROR(EINVAL);
  496. goto free_and_end;
  497. }
  498. avctx->frame_number = 0;
  499. if (HAVE_THREADS && !avctx->thread_opaque) {
  500. ret = ff_thread_init(avctx);
  501. if (ret < 0) {
  502. goto free_and_end;
  503. }
  504. }
  505. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  506. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  507. avctx->codec->max_lowres);
  508. ret = AVERROR(EINVAL);
  509. goto free_and_end;
  510. }
  511. if (avctx->codec->encode) {
  512. int i;
  513. if (avctx->codec->sample_fmts) {
  514. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  515. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  516. break;
  517. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  518. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  519. ret = AVERROR(EINVAL);
  520. goto free_and_end;
  521. }
  522. }
  523. if (avctx->codec->supported_samplerates) {
  524. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  525. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  526. break;
  527. if (avctx->codec->supported_samplerates[i] == 0) {
  528. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  529. ret = AVERROR(EINVAL);
  530. goto free_and_end;
  531. }
  532. }
  533. if (avctx->codec->channel_layouts) {
  534. if (!avctx->channel_layout) {
  535. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  536. } else {
  537. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  538. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  539. break;
  540. if (avctx->codec->channel_layouts[i] == 0) {
  541. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  542. ret = AVERROR(EINVAL);
  543. goto free_and_end;
  544. }
  545. }
  546. }
  547. if (avctx->channel_layout && avctx->channels) {
  548. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  549. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  550. ret = AVERROR(EINVAL);
  551. goto free_and_end;
  552. }
  553. } else if (avctx->channel_layout) {
  554. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  555. }
  556. }
  557. avctx->pts_correction_num_faulty_pts =
  558. avctx->pts_correction_num_faulty_dts = 0;
  559. avctx->pts_correction_last_pts =
  560. avctx->pts_correction_last_dts = INT64_MIN;
  561. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  562. ret = avctx->codec->init(avctx);
  563. if (ret < 0) {
  564. goto free_and_end;
  565. }
  566. }
  567. ret=0;
  568. end:
  569. entangled_thread_counter--;
  570. /* Release any user-supplied mutex. */
  571. if (ff_lockmgr_cb) {
  572. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  573. }
  574. if (options) {
  575. av_dict_free(options);
  576. *options = tmp;
  577. }
  578. return ret;
  579. free_and_end:
  580. av_dict_free(&tmp);
  581. av_freep(&avctx->priv_data);
  582. avctx->codec= NULL;
  583. goto end;
  584. }
  585. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  586. const short *samples)
  587. {
  588. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  589. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  590. return -1;
  591. }
  592. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  593. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  594. avctx->frame_number++;
  595. return ret;
  596. }else
  597. return 0;
  598. }
  599. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  600. const AVFrame *pict)
  601. {
  602. if(buf_size < FF_MIN_BUFFER_SIZE){
  603. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  604. return -1;
  605. }
  606. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  607. return -1;
  608. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  609. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  610. avctx->frame_number++;
  611. emms_c(); //needed to avoid an emms_c() call before every return;
  612. return ret;
  613. }else
  614. return 0;
  615. }
  616. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  617. const AVSubtitle *sub)
  618. {
  619. int ret;
  620. if(sub->start_display_time) {
  621. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  622. return -1;
  623. }
  624. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  625. avctx->frame_number++;
  626. return ret;
  627. }
  628. /**
  629. * Attempt to guess proper monotonic timestamps for decoded video frames
  630. * which might have incorrect times. Input timestamps may wrap around, in
  631. * which case the output will as well.
  632. *
  633. * @param pts the pts field of the decoded AVPacket, as passed through
  634. * AVFrame.pkt_pts
  635. * @param dts the dts field of the decoded AVPacket
  636. * @return one of the input values, may be AV_NOPTS_VALUE
  637. */
  638. static int64_t guess_correct_pts(AVCodecContext *ctx,
  639. int64_t reordered_pts, int64_t dts)
  640. {
  641. int64_t pts = AV_NOPTS_VALUE;
  642. if (dts != AV_NOPTS_VALUE) {
  643. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  644. ctx->pts_correction_last_dts = dts;
  645. }
  646. if (reordered_pts != AV_NOPTS_VALUE) {
  647. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  648. ctx->pts_correction_last_pts = reordered_pts;
  649. }
  650. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  651. && reordered_pts != AV_NOPTS_VALUE)
  652. pts = reordered_pts;
  653. else
  654. pts = dts;
  655. return pts;
  656. }
  657. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  658. int *got_picture_ptr,
  659. AVPacket *avpkt)
  660. {
  661. int ret;
  662. *got_picture_ptr= 0;
  663. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  664. return -1;
  665. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  666. av_packet_split_side_data(avpkt);
  667. avctx->pkt = avpkt;
  668. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  669. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  670. avpkt);
  671. else {
  672. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  673. avpkt);
  674. picture->pkt_dts= avpkt->dts;
  675. if(!avctx->has_b_frames){
  676. picture->pkt_pos= avpkt->pos;
  677. }
  678. //FIXME these should be under if(!avctx->has_b_frames)
  679. if (!picture->sample_aspect_ratio.num)
  680. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  681. if (!picture->width)
  682. picture->width = avctx->width;
  683. if (!picture->height)
  684. picture->height = avctx->height;
  685. if (picture->format == PIX_FMT_NONE)
  686. picture->format = avctx->pix_fmt;
  687. }
  688. emms_c(); //needed to avoid an emms_c() call before every return;
  689. if (*got_picture_ptr){
  690. avctx->frame_number++;
  691. picture->best_effort_timestamp = guess_correct_pts(avctx,
  692. picture->pkt_pts,
  693. picture->pkt_dts);
  694. }
  695. }else
  696. ret= 0;
  697. return ret;
  698. }
  699. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  700. int *frame_size_ptr,
  701. AVPacket *avpkt)
  702. {
  703. int ret;
  704. avctx->pkt = avpkt;
  705. if (!avpkt->data && avpkt->size) {
  706. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  707. return AVERROR(EINVAL);
  708. }
  709. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  710. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  711. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  712. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  713. return -1;
  714. }
  715. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  716. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  717. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  718. return -1;
  719. }
  720. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  721. avctx->frame_number++;
  722. }else{
  723. ret= 0;
  724. *frame_size_ptr=0;
  725. }
  726. return ret;
  727. }
  728. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  729. int *got_sub_ptr,
  730. AVPacket *avpkt)
  731. {
  732. int ret;
  733. avctx->pkt = avpkt;
  734. *got_sub_ptr = 0;
  735. avcodec_get_subtitle_defaults(sub);
  736. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  737. if (*got_sub_ptr)
  738. avctx->frame_number++;
  739. return ret;
  740. }
  741. void avsubtitle_free(AVSubtitle *sub)
  742. {
  743. int i;
  744. for (i = 0; i < sub->num_rects; i++)
  745. {
  746. av_freep(&sub->rects[i]->pict.data[0]);
  747. av_freep(&sub->rects[i]->pict.data[1]);
  748. av_freep(&sub->rects[i]->pict.data[2]);
  749. av_freep(&sub->rects[i]->pict.data[3]);
  750. av_freep(&sub->rects[i]->text);
  751. av_freep(&sub->rects[i]->ass);
  752. av_freep(&sub->rects[i]);
  753. }
  754. av_freep(&sub->rects);
  755. memset(sub, 0, sizeof(AVSubtitle));
  756. }
  757. av_cold int avcodec_close(AVCodecContext *avctx)
  758. {
  759. /* If there is a user-supplied mutex locking routine, call it. */
  760. if (ff_lockmgr_cb) {
  761. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  762. return -1;
  763. }
  764. entangled_thread_counter++;
  765. if(entangled_thread_counter != 1){
  766. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  767. entangled_thread_counter--;
  768. return -1;
  769. }
  770. if (HAVE_THREADS && avctx->thread_opaque)
  771. ff_thread_free(avctx);
  772. if (avctx->codec && avctx->codec->close)
  773. avctx->codec->close(avctx);
  774. avcodec_default_free_buffers(avctx);
  775. avctx->coded_frame = NULL;
  776. if (avctx->codec && avctx->codec->priv_class)
  777. av_opt_free(avctx->priv_data);
  778. av_opt_free(avctx);
  779. av_freep(&avctx->priv_data);
  780. if(avctx->codec && avctx->codec->encode)
  781. av_freep(&avctx->extradata);
  782. avctx->codec = NULL;
  783. avctx->active_thread_type = 0;
  784. entangled_thread_counter--;
  785. /* Release any user-supplied mutex. */
  786. if (ff_lockmgr_cb) {
  787. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  788. }
  789. return 0;
  790. }
  791. AVCodec *avcodec_find_encoder(enum CodecID id)
  792. {
  793. AVCodec *p, *experimental=NULL;
  794. p = first_avcodec;
  795. while (p) {
  796. if (p->encode != NULL && p->id == id) {
  797. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  798. experimental = p;
  799. } else
  800. return p;
  801. }
  802. p = p->next;
  803. }
  804. return experimental;
  805. }
  806. AVCodec *avcodec_find_encoder_by_name(const char *name)
  807. {
  808. AVCodec *p;
  809. if (!name)
  810. return NULL;
  811. p = first_avcodec;
  812. while (p) {
  813. if (p->encode != NULL && strcmp(name,p->name) == 0)
  814. return p;
  815. p = p->next;
  816. }
  817. return NULL;
  818. }
  819. AVCodec *avcodec_find_decoder(enum CodecID id)
  820. {
  821. AVCodec *p, *experimental=NULL;
  822. p = first_avcodec;
  823. while (p) {
  824. if (p->decode != NULL && p->id == id) {
  825. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  826. experimental = p;
  827. } else
  828. return p;
  829. }
  830. p = p->next;
  831. }
  832. return experimental;
  833. }
  834. AVCodec *avcodec_find_decoder_by_name(const char *name)
  835. {
  836. AVCodec *p;
  837. if (!name)
  838. return NULL;
  839. p = first_avcodec;
  840. while (p) {
  841. if (p->decode != NULL && strcmp(name,p->name) == 0)
  842. return p;
  843. p = p->next;
  844. }
  845. return NULL;
  846. }
  847. static int get_bit_rate(AVCodecContext *ctx)
  848. {
  849. int bit_rate;
  850. int bits_per_sample;
  851. switch(ctx->codec_type) {
  852. case AVMEDIA_TYPE_VIDEO:
  853. case AVMEDIA_TYPE_DATA:
  854. case AVMEDIA_TYPE_SUBTITLE:
  855. case AVMEDIA_TYPE_ATTACHMENT:
  856. bit_rate = ctx->bit_rate;
  857. break;
  858. case AVMEDIA_TYPE_AUDIO:
  859. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  860. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  861. break;
  862. default:
  863. bit_rate = 0;
  864. break;
  865. }
  866. return bit_rate;
  867. }
  868. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  869. {
  870. int i, len, ret = 0;
  871. for (i = 0; i < 4; i++) {
  872. len = snprintf(buf, buf_size,
  873. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  874. buf += len;
  875. buf_size = buf_size > len ? buf_size - len : 0;
  876. ret += len;
  877. codec_tag>>=8;
  878. }
  879. return ret;
  880. }
  881. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  882. {
  883. const char *codec_name;
  884. const char *profile = NULL;
  885. AVCodec *p;
  886. char buf1[32];
  887. int bitrate;
  888. AVRational display_aspect_ratio;
  889. if (encode)
  890. p = avcodec_find_encoder(enc->codec_id);
  891. else
  892. p = avcodec_find_decoder(enc->codec_id);
  893. if (p) {
  894. codec_name = p->name;
  895. profile = av_get_profile_name(p, enc->profile);
  896. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  897. /* fake mpeg2 transport stream codec (currently not
  898. registered) */
  899. codec_name = "mpeg2ts";
  900. } else if (enc->codec_name[0] != '\0') {
  901. codec_name = enc->codec_name;
  902. } else {
  903. /* output avi tags */
  904. char tag_buf[32];
  905. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  906. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  907. codec_name = buf1;
  908. }
  909. switch(enc->codec_type) {
  910. case AVMEDIA_TYPE_VIDEO:
  911. snprintf(buf, buf_size,
  912. "Video: %s%s",
  913. codec_name, enc->mb_decision ? " (hq)" : "");
  914. if (profile)
  915. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  916. " (%s)", profile);
  917. if (enc->pix_fmt != PIX_FMT_NONE) {
  918. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  919. ", %s",
  920. av_get_pix_fmt_name(enc->pix_fmt));
  921. }
  922. if (enc->width) {
  923. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  924. ", %dx%d",
  925. enc->width, enc->height);
  926. if (enc->sample_aspect_ratio.num) {
  927. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  928. enc->width*enc->sample_aspect_ratio.num,
  929. enc->height*enc->sample_aspect_ratio.den,
  930. 1024*1024);
  931. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  932. " [PAR %d:%d DAR %d:%d]",
  933. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  934. display_aspect_ratio.num, display_aspect_ratio.den);
  935. }
  936. if(av_log_get_level() >= AV_LOG_DEBUG){
  937. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  938. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  939. ", %d/%d",
  940. enc->time_base.num/g, enc->time_base.den/g);
  941. }
  942. }
  943. if (encode) {
  944. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  945. ", q=%d-%d", enc->qmin, enc->qmax);
  946. }
  947. break;
  948. case AVMEDIA_TYPE_AUDIO:
  949. snprintf(buf, buf_size,
  950. "Audio: %s",
  951. codec_name);
  952. if (profile)
  953. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  954. " (%s)", profile);
  955. if (enc->sample_rate) {
  956. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  957. ", %d Hz", enc->sample_rate);
  958. }
  959. av_strlcat(buf, ", ", buf_size);
  960. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  961. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  962. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  963. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  964. }
  965. break;
  966. case AVMEDIA_TYPE_DATA:
  967. snprintf(buf, buf_size, "Data: %s", codec_name);
  968. break;
  969. case AVMEDIA_TYPE_SUBTITLE:
  970. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  971. break;
  972. case AVMEDIA_TYPE_ATTACHMENT:
  973. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  974. break;
  975. default:
  976. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  977. return;
  978. }
  979. if (encode) {
  980. if (enc->flags & CODEC_FLAG_PASS1)
  981. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  982. ", pass 1");
  983. if (enc->flags & CODEC_FLAG_PASS2)
  984. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  985. ", pass 2");
  986. }
  987. bitrate = get_bit_rate(enc);
  988. if (bitrate != 0) {
  989. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  990. ", %d kb/s", bitrate / 1000);
  991. }
  992. }
  993. const char *av_get_profile_name(const AVCodec *codec, int profile)
  994. {
  995. const AVProfile *p;
  996. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  997. return NULL;
  998. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  999. if (p->profile == profile)
  1000. return p->name;
  1001. return NULL;
  1002. }
  1003. unsigned avcodec_version( void )
  1004. {
  1005. return LIBAVCODEC_VERSION_INT;
  1006. }
  1007. const char *avcodec_configuration(void)
  1008. {
  1009. return FFMPEG_CONFIGURATION;
  1010. }
  1011. const char *avcodec_license(void)
  1012. {
  1013. #define LICENSE_PREFIX "libavcodec license: "
  1014. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1015. }
  1016. void avcodec_init(void)
  1017. {
  1018. static int initialized = 0;
  1019. if (initialized != 0)
  1020. return;
  1021. initialized = 1;
  1022. dsputil_static_init();
  1023. }
  1024. void avcodec_flush_buffers(AVCodecContext *avctx)
  1025. {
  1026. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1027. ff_thread_flush(avctx);
  1028. else if(avctx->codec->flush)
  1029. avctx->codec->flush(avctx);
  1030. }
  1031. void avcodec_default_free_buffers(AVCodecContext *s){
  1032. int i, j;
  1033. if(s->internal_buffer==NULL) return;
  1034. if (s->internal_buffer_count)
  1035. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1036. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1037. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1038. for(j=0; j<4; j++){
  1039. av_freep(&buf->base[j]);
  1040. buf->data[j]= NULL;
  1041. }
  1042. }
  1043. av_freep(&s->internal_buffer);
  1044. s->internal_buffer_count=0;
  1045. }
  1046. #if FF_API_OLD_FF_PICT_TYPES
  1047. char av_get_pict_type_char(int pict_type){
  1048. return av_get_picture_type_char(pict_type);
  1049. }
  1050. #endif
  1051. int av_get_bits_per_sample(enum CodecID codec_id){
  1052. switch(codec_id){
  1053. case CODEC_ID_ADPCM_SBPRO_2:
  1054. return 2;
  1055. case CODEC_ID_ADPCM_SBPRO_3:
  1056. return 3;
  1057. case CODEC_ID_ADPCM_SBPRO_4:
  1058. case CODEC_ID_ADPCM_CT:
  1059. case CODEC_ID_ADPCM_IMA_WAV:
  1060. case CODEC_ID_ADPCM_MS:
  1061. case CODEC_ID_ADPCM_YAMAHA:
  1062. return 4;
  1063. case CODEC_ID_ADPCM_G722:
  1064. case CODEC_ID_PCM_ALAW:
  1065. case CODEC_ID_PCM_MULAW:
  1066. case CODEC_ID_PCM_S8:
  1067. case CODEC_ID_PCM_U8:
  1068. case CODEC_ID_PCM_ZORK:
  1069. return 8;
  1070. case CODEC_ID_PCM_S16BE:
  1071. case CODEC_ID_PCM_S16LE:
  1072. case CODEC_ID_PCM_S16LE_PLANAR:
  1073. case CODEC_ID_PCM_U16BE:
  1074. case CODEC_ID_PCM_U16LE:
  1075. return 16;
  1076. case CODEC_ID_PCM_S24DAUD:
  1077. case CODEC_ID_PCM_S24BE:
  1078. case CODEC_ID_PCM_S24LE:
  1079. case CODEC_ID_PCM_U24BE:
  1080. case CODEC_ID_PCM_U24LE:
  1081. return 24;
  1082. case CODEC_ID_PCM_S32BE:
  1083. case CODEC_ID_PCM_S32LE:
  1084. case CODEC_ID_PCM_U32BE:
  1085. case CODEC_ID_PCM_U32LE:
  1086. case CODEC_ID_PCM_F32BE:
  1087. case CODEC_ID_PCM_F32LE:
  1088. return 32;
  1089. case CODEC_ID_PCM_F64BE:
  1090. case CODEC_ID_PCM_F64LE:
  1091. return 64;
  1092. default:
  1093. return 0;
  1094. }
  1095. }
  1096. #if FF_API_OLD_SAMPLE_FMT
  1097. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1098. return av_get_bytes_per_sample(sample_fmt) << 3;
  1099. }
  1100. #endif
  1101. #if !HAVE_THREADS
  1102. int ff_thread_init(AVCodecContext *s){
  1103. return -1;
  1104. }
  1105. #endif
  1106. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1107. {
  1108. unsigned int n = 0;
  1109. while(v >= 0xff) {
  1110. *s++ = 0xff;
  1111. v -= 0xff;
  1112. n++;
  1113. }
  1114. *s = v;
  1115. n++;
  1116. return n;
  1117. }
  1118. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1119. int i;
  1120. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1121. return i;
  1122. }
  1123. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1124. {
  1125. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1126. "version to the newest one from Git. If the problem still "
  1127. "occurs, it means that your file has a feature which has not "
  1128. "been implemented.\n", feature);
  1129. if(want_sample)
  1130. av_log_ask_for_sample(avc, NULL);
  1131. }
  1132. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1133. {
  1134. va_list argument_list;
  1135. va_start(argument_list, msg);
  1136. if (msg)
  1137. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1138. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1139. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1140. "and contact the ffmpeg-devel mailing list.\n");
  1141. va_end(argument_list);
  1142. }
  1143. static AVHWAccel *first_hwaccel = NULL;
  1144. void av_register_hwaccel(AVHWAccel *hwaccel)
  1145. {
  1146. AVHWAccel **p = &first_hwaccel;
  1147. while (*p)
  1148. p = &(*p)->next;
  1149. *p = hwaccel;
  1150. hwaccel->next = NULL;
  1151. }
  1152. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1153. {
  1154. return hwaccel ? hwaccel->next : first_hwaccel;
  1155. }
  1156. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1157. {
  1158. AVHWAccel *hwaccel=NULL;
  1159. while((hwaccel= av_hwaccel_next(hwaccel))){
  1160. if ( hwaccel->id == codec_id
  1161. && hwaccel->pix_fmt == pix_fmt)
  1162. return hwaccel;
  1163. }
  1164. return NULL;
  1165. }
  1166. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1167. {
  1168. if (ff_lockmgr_cb) {
  1169. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1170. return -1;
  1171. }
  1172. ff_lockmgr_cb = cb;
  1173. if (ff_lockmgr_cb) {
  1174. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1175. return -1;
  1176. }
  1177. return 0;
  1178. }
  1179. unsigned int ff_toupper4(unsigned int x)
  1180. {
  1181. return toupper( x &0xFF)
  1182. + (toupper((x>>8 )&0xFF)<<8 )
  1183. + (toupper((x>>16)&0xFF)<<16)
  1184. + (toupper((x>>24)&0xFF)<<24);
  1185. }
  1186. #if !HAVE_PTHREADS
  1187. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1188. {
  1189. f->owner = avctx;
  1190. return avctx->get_buffer(avctx, f);
  1191. }
  1192. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1193. {
  1194. f->owner->release_buffer(f->owner, f);
  1195. }
  1196. void ff_thread_finish_setup(AVCodecContext *avctx)
  1197. {
  1198. }
  1199. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1200. {
  1201. }
  1202. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1203. {
  1204. }
  1205. #endif
  1206. #if FF_API_THREAD_INIT
  1207. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1208. {
  1209. s->thread_count = thread_count;
  1210. return ff_thread_init(s);
  1211. }
  1212. #endif