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.

1230 lines
35KB

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