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.

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