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.

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