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.

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