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.

1397 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. 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 || s->codec_id == CODEC_ID_PRORES)
  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)
  500. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  501. if (HAVE_THREADS && !avctx->thread_opaque) {
  502. ret = ff_thread_init(avctx);
  503. if (ret < 0) {
  504. goto free_and_end;
  505. }
  506. }
  507. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  508. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  509. avctx->codec->max_lowres);
  510. ret = AVERROR(EINVAL);
  511. goto free_and_end;
  512. }
  513. if (avctx->codec->encode) {
  514. int i;
  515. if (avctx->codec->sample_fmts) {
  516. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  517. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  518. break;
  519. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  520. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  521. ret = AVERROR(EINVAL);
  522. goto free_and_end;
  523. }
  524. }
  525. if (avctx->codec->supported_samplerates) {
  526. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  527. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  528. break;
  529. if (avctx->codec->supported_samplerates[i] == 0) {
  530. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  531. ret = AVERROR(EINVAL);
  532. goto free_and_end;
  533. }
  534. }
  535. if (avctx->codec->channel_layouts) {
  536. if (!avctx->channel_layout) {
  537. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  538. } else {
  539. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  540. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  541. break;
  542. if (avctx->codec->channel_layouts[i] == 0) {
  543. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  544. ret = AVERROR(EINVAL);
  545. goto free_and_end;
  546. }
  547. }
  548. }
  549. if (avctx->channel_layout && avctx->channels) {
  550. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  551. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  552. ret = AVERROR(EINVAL);
  553. goto free_and_end;
  554. }
  555. } else if (avctx->channel_layout) {
  556. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  557. }
  558. }
  559. avctx->pts_correction_num_faulty_pts =
  560. avctx->pts_correction_num_faulty_dts = 0;
  561. avctx->pts_correction_last_pts =
  562. avctx->pts_correction_last_dts = INT64_MIN;
  563. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  564. ret = avctx->codec->init(avctx);
  565. if (ret < 0) {
  566. goto free_and_end;
  567. }
  568. }
  569. ret=0;
  570. end:
  571. entangled_thread_counter--;
  572. /* Release any user-supplied mutex. */
  573. if (ff_lockmgr_cb) {
  574. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  575. }
  576. if (options) {
  577. av_dict_free(options);
  578. *options = tmp;
  579. }
  580. return ret;
  581. free_and_end:
  582. av_dict_free(&tmp);
  583. av_freep(&avctx->priv_data);
  584. avctx->codec= NULL;
  585. goto end;
  586. }
  587. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  588. const short *samples)
  589. {
  590. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  591. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  592. return -1;
  593. }
  594. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  595. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  596. avctx->frame_number++;
  597. return ret;
  598. }else
  599. return 0;
  600. }
  601. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  602. const AVFrame *pict)
  603. {
  604. if(buf_size < FF_MIN_BUFFER_SIZE){
  605. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  606. return -1;
  607. }
  608. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  609. return -1;
  610. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  611. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  612. avctx->frame_number++;
  613. emms_c(); //needed to avoid an emms_c() call before every return;
  614. return ret;
  615. }else
  616. return 0;
  617. }
  618. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  619. const AVSubtitle *sub)
  620. {
  621. int ret;
  622. if(sub->start_display_time) {
  623. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  624. return -1;
  625. }
  626. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  627. avctx->frame_number++;
  628. return ret;
  629. }
  630. /**
  631. * Attempt to guess proper monotonic timestamps for decoded video frames
  632. * which might have incorrect times. Input timestamps may wrap around, in
  633. * which case the output will as well.
  634. *
  635. * @param pts the pts field of the decoded AVPacket, as passed through
  636. * AVFrame.pkt_pts
  637. * @param dts the dts field of the decoded AVPacket
  638. * @return one of the input values, may be AV_NOPTS_VALUE
  639. */
  640. static int64_t guess_correct_pts(AVCodecContext *ctx,
  641. int64_t reordered_pts, int64_t dts)
  642. {
  643. int64_t pts = AV_NOPTS_VALUE;
  644. if (dts != AV_NOPTS_VALUE) {
  645. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  646. ctx->pts_correction_last_dts = dts;
  647. }
  648. if (reordered_pts != AV_NOPTS_VALUE) {
  649. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  650. ctx->pts_correction_last_pts = reordered_pts;
  651. }
  652. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  653. && reordered_pts != AV_NOPTS_VALUE)
  654. pts = reordered_pts;
  655. else
  656. pts = dts;
  657. return pts;
  658. }
  659. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  660. int *got_picture_ptr,
  661. AVPacket *avpkt)
  662. {
  663. int ret;
  664. *got_picture_ptr= 0;
  665. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  666. return -1;
  667. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  668. av_packet_split_side_data(avpkt);
  669. avctx->pkt = avpkt;
  670. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  671. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  672. avpkt);
  673. else {
  674. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  675. avpkt);
  676. picture->pkt_dts= avpkt->dts;
  677. if(!avctx->has_b_frames){
  678. picture->pkt_pos= avpkt->pos;
  679. }
  680. //FIXME these should be under if(!avctx->has_b_frames)
  681. if (!picture->sample_aspect_ratio.num)
  682. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  683. if (!picture->width)
  684. picture->width = avctx->width;
  685. if (!picture->height)
  686. picture->height = avctx->height;
  687. if (picture->format == PIX_FMT_NONE)
  688. picture->format = avctx->pix_fmt;
  689. }
  690. emms_c(); //needed to avoid an emms_c() call before every return;
  691. if (*got_picture_ptr){
  692. avctx->frame_number++;
  693. picture->best_effort_timestamp = guess_correct_pts(avctx,
  694. picture->pkt_pts,
  695. picture->pkt_dts);
  696. }
  697. }else
  698. ret= 0;
  699. return ret;
  700. }
  701. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  702. int *frame_size_ptr,
  703. AVPacket *avpkt)
  704. {
  705. int ret;
  706. avctx->pkt = avpkt;
  707. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  708. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  709. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  710. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  711. return -1;
  712. }
  713. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  714. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  715. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  716. return -1;
  717. }
  718. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  719. avctx->frame_number++;
  720. }else{
  721. ret= 0;
  722. *frame_size_ptr=0;
  723. }
  724. return ret;
  725. }
  726. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  727. int *got_sub_ptr,
  728. AVPacket *avpkt)
  729. {
  730. int ret;
  731. avctx->pkt = avpkt;
  732. *got_sub_ptr = 0;
  733. avcodec_get_subtitle_defaults(sub);
  734. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  735. if (*got_sub_ptr)
  736. avctx->frame_number++;
  737. return ret;
  738. }
  739. void avsubtitle_free(AVSubtitle *sub)
  740. {
  741. int i;
  742. for (i = 0; i < sub->num_rects; i++)
  743. {
  744. av_freep(&sub->rects[i]->pict.data[0]);
  745. av_freep(&sub->rects[i]->pict.data[1]);
  746. av_freep(&sub->rects[i]->pict.data[2]);
  747. av_freep(&sub->rects[i]->pict.data[3]);
  748. av_freep(&sub->rects[i]->text);
  749. av_freep(&sub->rects[i]->ass);
  750. av_freep(&sub->rects[i]);
  751. }
  752. av_freep(&sub->rects);
  753. memset(sub, 0, sizeof(AVSubtitle));
  754. }
  755. av_cold int avcodec_close(AVCodecContext *avctx)
  756. {
  757. /* If there is a user-supplied mutex locking routine, call it. */
  758. if (ff_lockmgr_cb) {
  759. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  760. return -1;
  761. }
  762. entangled_thread_counter++;
  763. if(entangled_thread_counter != 1){
  764. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  765. entangled_thread_counter--;
  766. return -1;
  767. }
  768. if (HAVE_THREADS && avctx->thread_opaque)
  769. ff_thread_free(avctx);
  770. if (avctx->codec && avctx->codec->close)
  771. avctx->codec->close(avctx);
  772. avcodec_default_free_buffers(avctx);
  773. avctx->coded_frame = NULL;
  774. if (avctx->codec && avctx->codec->priv_class)
  775. av_opt_free(avctx->priv_data);
  776. av_opt_free(avctx);
  777. av_freep(&avctx->priv_data);
  778. if(avctx->codec && avctx->codec->encode)
  779. av_freep(&avctx->extradata);
  780. avctx->codec = NULL;
  781. avctx->active_thread_type = 0;
  782. entangled_thread_counter--;
  783. /* Release any user-supplied mutex. */
  784. if (ff_lockmgr_cb) {
  785. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  786. }
  787. return 0;
  788. }
  789. AVCodec *avcodec_find_encoder(enum CodecID id)
  790. {
  791. AVCodec *p, *experimental=NULL;
  792. p = first_avcodec;
  793. while (p) {
  794. if (p->encode != NULL && p->id == id) {
  795. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  796. experimental = p;
  797. } else
  798. return p;
  799. }
  800. p = p->next;
  801. }
  802. return experimental;
  803. }
  804. AVCodec *avcodec_find_encoder_by_name(const char *name)
  805. {
  806. AVCodec *p;
  807. if (!name)
  808. return NULL;
  809. p = first_avcodec;
  810. while (p) {
  811. if (p->encode != NULL && strcmp(name,p->name) == 0)
  812. return p;
  813. p = p->next;
  814. }
  815. return NULL;
  816. }
  817. AVCodec *avcodec_find_decoder(enum CodecID id)
  818. {
  819. AVCodec *p, *experimental=NULL;
  820. p = first_avcodec;
  821. while (p) {
  822. if (p->decode != NULL && p->id == id) {
  823. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  824. experimental = p;
  825. } else
  826. return p;
  827. }
  828. p = p->next;
  829. }
  830. return experimental;
  831. }
  832. AVCodec *avcodec_find_decoder_by_name(const char *name)
  833. {
  834. AVCodec *p;
  835. if (!name)
  836. return NULL;
  837. p = first_avcodec;
  838. while (p) {
  839. if (p->decode != NULL && strcmp(name,p->name) == 0)
  840. return p;
  841. p = p->next;
  842. }
  843. return NULL;
  844. }
  845. static int get_bit_rate(AVCodecContext *ctx)
  846. {
  847. int bit_rate;
  848. int bits_per_sample;
  849. switch(ctx->codec_type) {
  850. case AVMEDIA_TYPE_VIDEO:
  851. case AVMEDIA_TYPE_DATA:
  852. case AVMEDIA_TYPE_SUBTITLE:
  853. case AVMEDIA_TYPE_ATTACHMENT:
  854. bit_rate = ctx->bit_rate;
  855. break;
  856. case AVMEDIA_TYPE_AUDIO:
  857. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  858. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  859. break;
  860. default:
  861. bit_rate = 0;
  862. break;
  863. }
  864. return bit_rate;
  865. }
  866. const char *avcodec_get_name(enum CodecID id)
  867. {
  868. AVCodec *codec;
  869. #if !CONFIG_SMALL
  870. switch (id) {
  871. #include "libavcodec/codec_names.h"
  872. }
  873. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  874. #endif
  875. codec = avcodec_find_decoder(id);
  876. if (codec)
  877. return codec->name;
  878. codec = avcodec_find_encoder(id);
  879. if (codec)
  880. return codec->name;
  881. return "unknown_codec";
  882. }
  883. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  884. {
  885. int i, len, ret = 0;
  886. for (i = 0; i < 4; i++) {
  887. len = snprintf(buf, buf_size,
  888. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  889. buf += len;
  890. buf_size = buf_size > len ? buf_size - len : 0;
  891. ret += len;
  892. codec_tag>>=8;
  893. }
  894. return ret;
  895. }
  896. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  897. {
  898. const char *codec_type;
  899. const char *codec_name;
  900. const char *profile = NULL;
  901. AVCodec *p;
  902. int bitrate;
  903. AVRational display_aspect_ratio;
  904. if (!buf || buf_size <= 0)
  905. return;
  906. codec_type = av_get_media_type_string(enc->codec_type);
  907. codec_name = avcodec_get_name(enc->codec_id);
  908. if (enc->profile != FF_PROFILE_UNKNOWN) {
  909. p = encode ? avcodec_find_encoder(enc->codec_id) :
  910. avcodec_find_decoder(enc->codec_id);
  911. if (p)
  912. profile = av_get_profile_name(p, enc->profile);
  913. }
  914. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  915. codec_name, enc->mb_decision ? " (hq)" : "");
  916. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  917. if (profile)
  918. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  919. if (enc->codec_tag) {
  920. char tag_buf[32];
  921. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  922. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  923. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  924. }
  925. switch(enc->codec_type) {
  926. case AVMEDIA_TYPE_VIDEO:
  927. if (enc->pix_fmt != PIX_FMT_NONE) {
  928. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  929. ", %s",
  930. av_get_pix_fmt_name(enc->pix_fmt));
  931. }
  932. if (enc->width) {
  933. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  934. ", %dx%d",
  935. enc->width, enc->height);
  936. if (enc->sample_aspect_ratio.num) {
  937. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  938. enc->width*enc->sample_aspect_ratio.num,
  939. enc->height*enc->sample_aspect_ratio.den,
  940. 1024*1024);
  941. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  942. " [SAR %d:%d DAR %d:%d]",
  943. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  944. display_aspect_ratio.num, display_aspect_ratio.den);
  945. }
  946. if(av_log_get_level() >= AV_LOG_DEBUG){
  947. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  948. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  949. ", %d/%d",
  950. enc->time_base.num/g, enc->time_base.den/g);
  951. }
  952. }
  953. if (encode) {
  954. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  955. ", q=%d-%d", enc->qmin, enc->qmax);
  956. }
  957. break;
  958. case AVMEDIA_TYPE_AUDIO:
  959. if (enc->sample_rate) {
  960. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  961. ", %d Hz", enc->sample_rate);
  962. }
  963. av_strlcat(buf, ", ", buf_size);
  964. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  965. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  966. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  967. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  968. }
  969. break;
  970. default:
  971. return;
  972. }
  973. if (encode) {
  974. if (enc->flags & CODEC_FLAG_PASS1)
  975. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  976. ", pass 1");
  977. if (enc->flags & CODEC_FLAG_PASS2)
  978. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  979. ", pass 2");
  980. }
  981. bitrate = get_bit_rate(enc);
  982. if (bitrate != 0) {
  983. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  984. ", %d kb/s", bitrate / 1000);
  985. }
  986. }
  987. const char *av_get_profile_name(const AVCodec *codec, int profile)
  988. {
  989. const AVProfile *p;
  990. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  991. return NULL;
  992. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  993. if (p->profile == profile)
  994. return p->name;
  995. return NULL;
  996. }
  997. unsigned avcodec_version( void )
  998. {
  999. return LIBAVCODEC_VERSION_INT;
  1000. }
  1001. const char *avcodec_configuration(void)
  1002. {
  1003. return FFMPEG_CONFIGURATION;
  1004. }
  1005. const char *avcodec_license(void)
  1006. {
  1007. #define LICENSE_PREFIX "libavcodec license: "
  1008. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1009. }
  1010. #if !FF_API_AVCODEC_INIT
  1011. static
  1012. #endif
  1013. void avcodec_init(void)
  1014. {
  1015. static int initialized = 0;
  1016. if (initialized != 0)
  1017. return;
  1018. initialized = 1;
  1019. dsputil_static_init();
  1020. }
  1021. void avcodec_flush_buffers(AVCodecContext *avctx)
  1022. {
  1023. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1024. ff_thread_flush(avctx);
  1025. else if(avctx->codec->flush)
  1026. avctx->codec->flush(avctx);
  1027. }
  1028. void avcodec_default_free_buffers(AVCodecContext *s){
  1029. int i, j;
  1030. if(s->internal_buffer==NULL) return;
  1031. if (s->internal_buffer_count)
  1032. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  1033. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1034. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  1035. for(j=0; j<4; j++){
  1036. av_freep(&buf->base[j]);
  1037. buf->data[j]= NULL;
  1038. }
  1039. }
  1040. av_freep(&s->internal_buffer);
  1041. s->internal_buffer_count=0;
  1042. }
  1043. #if FF_API_OLD_FF_PICT_TYPES
  1044. char av_get_pict_type_char(int pict_type){
  1045. return av_get_picture_type_char(pict_type);
  1046. }
  1047. #endif
  1048. int av_get_bits_per_sample(enum CodecID codec_id){
  1049. switch(codec_id){
  1050. case CODEC_ID_ADPCM_SBPRO_2:
  1051. return 2;
  1052. case CODEC_ID_ADPCM_SBPRO_3:
  1053. return 3;
  1054. case CODEC_ID_ADPCM_SBPRO_4:
  1055. case CODEC_ID_ADPCM_CT:
  1056. case CODEC_ID_ADPCM_IMA_WAV:
  1057. case CODEC_ID_ADPCM_MS:
  1058. case CODEC_ID_ADPCM_YAMAHA:
  1059. return 4;
  1060. case CODEC_ID_ADPCM_G722:
  1061. case CODEC_ID_PCM_ALAW:
  1062. case CODEC_ID_PCM_MULAW:
  1063. case CODEC_ID_PCM_S8:
  1064. case CODEC_ID_PCM_U8:
  1065. case CODEC_ID_PCM_ZORK:
  1066. return 8;
  1067. case CODEC_ID_PCM_S16BE:
  1068. case CODEC_ID_PCM_S16LE:
  1069. case CODEC_ID_PCM_S16LE_PLANAR:
  1070. case CODEC_ID_PCM_U16BE:
  1071. case CODEC_ID_PCM_U16LE:
  1072. return 16;
  1073. case CODEC_ID_PCM_S24DAUD:
  1074. case CODEC_ID_PCM_S24BE:
  1075. case CODEC_ID_PCM_S24LE:
  1076. case CODEC_ID_PCM_U24BE:
  1077. case CODEC_ID_PCM_U24LE:
  1078. return 24;
  1079. case CODEC_ID_PCM_S32BE:
  1080. case CODEC_ID_PCM_S32LE:
  1081. case CODEC_ID_PCM_U32BE:
  1082. case CODEC_ID_PCM_U32LE:
  1083. case CODEC_ID_PCM_F32BE:
  1084. case CODEC_ID_PCM_F32LE:
  1085. return 32;
  1086. case CODEC_ID_PCM_F64BE:
  1087. case CODEC_ID_PCM_F64LE:
  1088. return 64;
  1089. default:
  1090. return 0;
  1091. }
  1092. }
  1093. #if FF_API_OLD_SAMPLE_FMT
  1094. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1095. return av_get_bytes_per_sample(sample_fmt) << 3;
  1096. }
  1097. #endif
  1098. #if !HAVE_THREADS
  1099. int ff_thread_init(AVCodecContext *s){
  1100. return -1;
  1101. }
  1102. #endif
  1103. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1104. {
  1105. unsigned int n = 0;
  1106. while(v >= 0xff) {
  1107. *s++ = 0xff;
  1108. v -= 0xff;
  1109. n++;
  1110. }
  1111. *s = v;
  1112. n++;
  1113. return n;
  1114. }
  1115. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1116. int i;
  1117. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1118. return i;
  1119. }
  1120. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1121. {
  1122. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1123. "version to the newest one from Git. If the problem still "
  1124. "occurs, it means that your file has a feature which has not "
  1125. "been implemented.\n", feature);
  1126. if(want_sample)
  1127. av_log_ask_for_sample(avc, NULL);
  1128. }
  1129. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1130. {
  1131. va_list argument_list;
  1132. va_start(argument_list, msg);
  1133. if (msg)
  1134. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1135. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1136. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1137. "and contact the ffmpeg-devel mailing list.\n");
  1138. va_end(argument_list);
  1139. }
  1140. static AVHWAccel *first_hwaccel = NULL;
  1141. void av_register_hwaccel(AVHWAccel *hwaccel)
  1142. {
  1143. AVHWAccel **p = &first_hwaccel;
  1144. while (*p)
  1145. p = &(*p)->next;
  1146. *p = hwaccel;
  1147. hwaccel->next = NULL;
  1148. }
  1149. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1150. {
  1151. return hwaccel ? hwaccel->next : first_hwaccel;
  1152. }
  1153. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1154. {
  1155. AVHWAccel *hwaccel=NULL;
  1156. while((hwaccel= av_hwaccel_next(hwaccel))){
  1157. if ( hwaccel->id == codec_id
  1158. && hwaccel->pix_fmt == pix_fmt)
  1159. return hwaccel;
  1160. }
  1161. return NULL;
  1162. }
  1163. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1164. {
  1165. if (ff_lockmgr_cb) {
  1166. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1167. return -1;
  1168. }
  1169. ff_lockmgr_cb = cb;
  1170. if (ff_lockmgr_cb) {
  1171. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1172. return -1;
  1173. }
  1174. return 0;
  1175. }
  1176. unsigned int ff_toupper4(unsigned int x)
  1177. {
  1178. return toupper( x &0xFF)
  1179. + (toupper((x>>8 )&0xFF)<<8 )
  1180. + (toupper((x>>16)&0xFF)<<16)
  1181. + (toupper((x>>24)&0xFF)<<24);
  1182. }
  1183. #if !HAVE_PTHREADS
  1184. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1185. {
  1186. f->owner = avctx;
  1187. return avctx->get_buffer(avctx, f);
  1188. }
  1189. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1190. {
  1191. f->owner->release_buffer(f->owner, f);
  1192. }
  1193. void ff_thread_finish_setup(AVCodecContext *avctx)
  1194. {
  1195. }
  1196. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1197. {
  1198. }
  1199. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1200. {
  1201. }
  1202. #endif
  1203. #if FF_API_THREAD_INIT
  1204. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1205. {
  1206. s->thread_count = thread_count;
  1207. return ff_thread_init(s);
  1208. }
  1209. #endif
  1210. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1211. {
  1212. if (codec_id <= CODEC_ID_NONE)
  1213. return AVMEDIA_TYPE_UNKNOWN;
  1214. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1215. return AVMEDIA_TYPE_VIDEO;
  1216. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1217. return AVMEDIA_TYPE_AUDIO;
  1218. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1219. return AVMEDIA_TYPE_SUBTITLE;
  1220. return AVMEDIA_TYPE_UNKNOWN;
  1221. }