rawdec.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Raw Video Decoder
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/rawdec.c
  23. * Raw Video Decoder
  24. */
  25. #include "avcodec.h"
  26. #include "raw.h"
  27. #include "libavutil/intreadwrite.h"
  28. typedef struct RawVideoContext {
  29. unsigned char * buffer; /* block of memory for holding one frame */
  30. int length; /* number of bytes in buffer */
  31. int flip;
  32. AVFrame pic; ///< AVCodecContext.coded_frame
  33. } RawVideoContext;
  34. static const PixelFormatTag pixelFormatBpsAVI[] = {
  35. { PIX_FMT_PAL8, 4 },
  36. { PIX_FMT_PAL8, 8 },
  37. { PIX_FMT_RGB555, 15 },
  38. { PIX_FMT_RGB555, 16 },
  39. { PIX_FMT_BGR24, 24 },
  40. { PIX_FMT_RGB32, 32 },
  41. { PIX_FMT_NONE, 0 },
  42. };
  43. static const PixelFormatTag pixelFormatBpsMOV[] = {
  44. { PIX_FMT_MONOWHITE, 1 },
  45. { PIX_FMT_PAL8, 4 },
  46. { PIX_FMT_PAL8, 8 },
  47. // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
  48. // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
  49. { PIX_FMT_BGR555, 16 },
  50. { PIX_FMT_RGB24, 24 },
  51. { PIX_FMT_ARGB, 32 },
  52. { PIX_FMT_NONE, 0 },
  53. };
  54. static enum PixelFormat findPixelFormat(const PixelFormatTag *tags, unsigned int fourcc)
  55. {
  56. while (tags->pix_fmt >= 0) {
  57. if (tags->fourcc == fourcc)
  58. return tags->pix_fmt;
  59. tags++;
  60. }
  61. return PIX_FMT_YUV420P;
  62. }
  63. static av_cold int raw_init_decoder(AVCodecContext *avctx)
  64. {
  65. RawVideoContext *context = avctx->priv_data;
  66. if (avctx->codec_tag == MKTAG('r','a','w',' '))
  67. avctx->pix_fmt = findPixelFormat(pixelFormatBpsMOV, avctx->bits_per_coded_sample);
  68. else if (avctx->codec_tag)
  69. avctx->pix_fmt = findPixelFormat(ff_raw_pixelFormatTags, avctx->codec_tag);
  70. else if (avctx->bits_per_coded_sample)
  71. avctx->pix_fmt = findPixelFormat(pixelFormatBpsAVI, avctx->bits_per_coded_sample);
  72. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  73. context->buffer = av_malloc(context->length);
  74. context->pic.pict_type = FF_I_TYPE;
  75. context->pic.key_frame = 1;
  76. avctx->coded_frame= &context->pic;
  77. if (!context->buffer)
  78. return -1;
  79. if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
  80. avctx->codec_tag == MKTAG( 3 , 0 , 0 , 0 ))
  81. context->flip=1;
  82. return 0;
  83. }
  84. static void flip(AVCodecContext *avctx, AVPicture * picture){
  85. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  86. picture->linesize[0] *= -1;
  87. }
  88. static int raw_decode(AVCodecContext *avctx,
  89. void *data, int *data_size,
  90. AVPacket *avpkt)
  91. {
  92. const uint8_t *buf = avpkt->data;
  93. int buf_size = avpkt->size;
  94. RawVideoContext *context = avctx->priv_data;
  95. AVFrame * frame = (AVFrame *) data;
  96. AVPicture * picture = (AVPicture *) data;
  97. frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
  98. frame->top_field_first = avctx->coded_frame->top_field_first;
  99. //4bpp raw in avi and mov (yes this is ugly ...)
  100. if(avctx->bits_per_coded_sample == 4 && avctx->pix_fmt==PIX_FMT_PAL8 &&
  101. (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
  102. int i;
  103. for(i=256*2; i+1 < context->length>>1; i++){
  104. context->buffer[2*i+0]= buf[i-256*2]>>4;
  105. context->buffer[2*i+1]= buf[i-256*2]&15;
  106. }
  107. buf= context->buffer + 256*4;
  108. buf_size= context->length - 256*4;
  109. }
  110. if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
  111. return -1;
  112. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  113. if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
  114. frame->data[1]= context->buffer;
  115. }
  116. if (avctx->palctrl && avctx->palctrl->palette_changed) {
  117. memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  118. avctx->palctrl->palette_changed = 0;
  119. }
  120. if(context->flip)
  121. flip(avctx, picture);
  122. if ( avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
  123. || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
  124. FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
  125. if(avctx->codec_tag == AV_RL32("yuv2") &&
  126. avctx->pix_fmt == PIX_FMT_YUYV422) {
  127. int x, y;
  128. uint8_t *line = picture->data[0];
  129. for(y = 0; y < avctx->height; y++) {
  130. for(x = 0; x < avctx->width; x++)
  131. line[2*x + 1] ^= 0x80;
  132. line += picture->linesize[0];
  133. }
  134. }
  135. *data_size = sizeof(AVPicture);
  136. return buf_size;
  137. }
  138. static av_cold int raw_close_decoder(AVCodecContext *avctx)
  139. {
  140. RawVideoContext *context = avctx->priv_data;
  141. av_freep(&context->buffer);
  142. return 0;
  143. }
  144. AVCodec rawvideo_decoder = {
  145. "rawvideo",
  146. CODEC_TYPE_VIDEO,
  147. CODEC_ID_RAWVIDEO,
  148. sizeof(RawVideoContext),
  149. raw_init_decoder,
  150. NULL,
  151. raw_close_decoder,
  152. raw_decode,
  153. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  154. };