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.

1427 lines
42KB

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