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.

193 lines
5.6KB

  1. /*
  2. * Raw Video Codec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file raw.c
  21. * Raw Video Codec
  22. */
  23. #include "avcodec.h"
  24. typedef struct RawVideoContext {
  25. unsigned char * buffer; /* block of memory for holding one frame */
  26. int length; /* number of bytes in buffer */
  27. AVFrame pic; ///< AVCodecContext.coded_frame
  28. } RawVideoContext;
  29. typedef struct PixelFormatTag {
  30. int pix_fmt;
  31. unsigned int fourcc;
  32. } PixelFormatTag;
  33. const PixelFormatTag pixelFormatTags[] = {
  34. { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
  35. { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
  36. { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
  37. { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
  38. { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
  39. { PIX_FMT_GRAY8, MKTAG('Y', '8', '0', '0') },
  40. { PIX_FMT_GRAY8, MKTAG(' ', ' ', 'Y', '8') },
  41. { PIX_FMT_YUV422, MKTAG('Y', 'U', 'Y', '2') }, /* Packed formats */
  42. { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') },
  43. { PIX_FMT_UYVY422, MKTAG('U', 'Y', 'V', 'Y') },
  44. { PIX_FMT_GRAY8, MKTAG('G', 'R', 'E', 'Y') },
  45. { -1, 0 },
  46. };
  47. static int findPixelFormat(unsigned int fourcc)
  48. {
  49. const PixelFormatTag * tags = pixelFormatTags;
  50. while (tags->pix_fmt >= 0) {
  51. if (tags->fourcc == fourcc)
  52. return tags->pix_fmt;
  53. tags++;
  54. }
  55. return PIX_FMT_YUV420P;
  56. }
  57. unsigned int avcodec_pix_fmt_to_codec_tag(enum PixelFormat fmt)
  58. {
  59. const PixelFormatTag * tags = pixelFormatTags;
  60. while (tags->pix_fmt >= 0) {
  61. if (tags->pix_fmt == fmt)
  62. return tags->fourcc;
  63. tags++;
  64. }
  65. return 0;
  66. }
  67. /* RAW Decoder Implementation */
  68. static int raw_init_decoder(AVCodecContext *avctx)
  69. {
  70. RawVideoContext *context = avctx->priv_data;
  71. if (avctx->codec_tag)
  72. avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
  73. else if (avctx->bits_per_sample){
  74. switch(avctx->bits_per_sample){
  75. case 8: avctx->pix_fmt= PIX_FMT_PAL8 ; break;
  76. case 15: avctx->pix_fmt= PIX_FMT_RGB555; break;
  77. case 16: avctx->pix_fmt= PIX_FMT_RGB565; break;
  78. case 24: avctx->pix_fmt= PIX_FMT_BGR24 ; break;
  79. case 32: avctx->pix_fmt= PIX_FMT_RGBA32; break;
  80. }
  81. }
  82. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  83. context->buffer = av_malloc(context->length);
  84. context->pic.pict_type = FF_I_TYPE;
  85. context->pic.key_frame = 1;
  86. avctx->coded_frame= &context->pic;
  87. if (!context->buffer)
  88. return -1;
  89. return 0;
  90. }
  91. static void flip(AVCodecContext *avctx, AVPicture * picture){
  92. if(!avctx->codec_tag && avctx->bits_per_sample && picture->linesize[2]==0){
  93. picture->data[0] += picture->linesize[0] * (avctx->height-1);
  94. picture->linesize[0] *= -1;
  95. }
  96. }
  97. static int raw_decode(AVCodecContext *avctx,
  98. void *data, int *data_size,
  99. uint8_t *buf, int buf_size)
  100. {
  101. RawVideoContext *context = avctx->priv_data;
  102. AVFrame * frame = (AVFrame *) data;
  103. AVPicture * picture = (AVPicture *) data;
  104. frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
  105. frame->top_field_first = avctx->coded_frame->top_field_first;
  106. if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
  107. return -1;
  108. avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height);
  109. if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){
  110. frame->data[1]= context->buffer;
  111. }
  112. if (avctx->palctrl && avctx->palctrl->palette_changed) {
  113. memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  114. avctx->palctrl->palette_changed = 0;
  115. }
  116. flip(avctx, picture);
  117. *data_size = sizeof(AVPicture);
  118. return buf_size;
  119. }
  120. static int raw_close_decoder(AVCodecContext *avctx)
  121. {
  122. RawVideoContext *context = avctx->priv_data;
  123. av_freep(&context->buffer);
  124. return 0;
  125. }
  126. /* RAW Encoder Implementation */
  127. static int raw_init_encoder(AVCodecContext *avctx)
  128. {
  129. avctx->coded_frame = (AVFrame *)avctx->priv_data;
  130. avctx->coded_frame->pict_type = FF_I_TYPE;
  131. avctx->coded_frame->key_frame = 1;
  132. if(!avctx->codec_tag)
  133. avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
  134. return 0;
  135. }
  136. static int raw_encode(AVCodecContext *avctx,
  137. unsigned char *frame, int buf_size, void *data)
  138. {
  139. return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
  140. avctx->height, frame, buf_size);
  141. }
  142. #ifdef CONFIG_RAWVIDEO_ENCODER
  143. AVCodec rawvideo_encoder = {
  144. "rawvideo",
  145. CODEC_TYPE_VIDEO,
  146. CODEC_ID_RAWVIDEO,
  147. sizeof(AVFrame),
  148. raw_init_encoder,
  149. raw_encode,
  150. };
  151. #endif // CONFIG_RAWVIDEO_ENCODER
  152. AVCodec rawvideo_decoder = {
  153. "rawvideo",
  154. CODEC_TYPE_VIDEO,
  155. CODEC_ID_RAWVIDEO,
  156. sizeof(RawVideoContext),
  157. raw_init_decoder,
  158. NULL,
  159. raw_close_decoder,
  160. raw_decode,
  161. };