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.

396 lines
12KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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. #include "avformat.h"
  22. #define Y4M_MAGIC "YUV4MPEG2"
  23. #define Y4M_FRAME_MAGIC "FRAME"
  24. #define Y4M_LINE_MAX 256
  25. struct frame_attributes {
  26. int interlaced_frame;
  27. int top_field_first;
  28. };
  29. #if CONFIG_YUV4MPEGPIPE_MUXER
  30. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  31. {
  32. AVStream *st;
  33. int width, height;
  34. int raten, rated, aspectn, aspectd, n;
  35. char inter;
  36. const char *colorspace = "";
  37. st = s->streams[0];
  38. width = st->codec->width;
  39. height = st->codec->height;
  40. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  41. aspectn = st->sample_aspect_ratio.num;
  42. aspectd = st->sample_aspect_ratio.den;
  43. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  44. inter = 'p'; /* progressive is the default */
  45. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  46. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  47. }
  48. switch(st->codec->pix_fmt) {
  49. case PIX_FMT_GRAY8:
  50. colorspace = " Cmono";
  51. break;
  52. case PIX_FMT_YUV411P:
  53. colorspace = " C411 XYSCSS=411";
  54. break;
  55. case PIX_FMT_YUV420P:
  56. colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
  57. break;
  58. case PIX_FMT_YUV422P:
  59. colorspace = " C422 XYSCSS=422";
  60. break;
  61. case PIX_FMT_YUV444P:
  62. colorspace = " C444 XYSCSS=444";
  63. break;
  64. }
  65. /* construct stream header, if this is the first frame */
  66. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  67. Y4M_MAGIC,
  68. width,
  69. height,
  70. raten, rated,
  71. inter,
  72. aspectn, aspectd,
  73. colorspace);
  74. return n;
  75. }
  76. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. AVStream *st = s->streams[pkt->stream_index];
  79. ByteIOContext *pb = s->pb;
  80. AVPicture *picture;
  81. int* first_pkt = s->priv_data;
  82. int width, height, h_chroma_shift, v_chroma_shift;
  83. int i, m;
  84. char buf2[Y4M_LINE_MAX+1];
  85. char buf1[20];
  86. uint8_t *ptr, *ptr1, *ptr2;
  87. picture = (AVPicture *)pkt->data;
  88. /* for the first packet we have to output the header as well */
  89. if (*first_pkt) {
  90. *first_pkt = 0;
  91. if (yuv4_generate_header(s, buf2) < 0) {
  92. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  93. return AVERROR(EIO);
  94. } else {
  95. put_buffer(pb, buf2, strlen(buf2));
  96. }
  97. }
  98. /* construct frame header */
  99. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  100. put_buffer(pb, buf1, strlen(buf1));
  101. width = st->codec->width;
  102. height = st->codec->height;
  103. ptr = picture->data[0];
  104. for(i=0;i<height;i++) {
  105. put_buffer(pb, ptr, width);
  106. ptr += picture->linesize[0];
  107. }
  108. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  109. // Adjust for smaller Cb and Cr planes
  110. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  111. width >>= h_chroma_shift;
  112. height >>= v_chroma_shift;
  113. ptr1 = picture->data[1];
  114. ptr2 = picture->data[2];
  115. for(i=0;i<height;i++) { /* Cb */
  116. put_buffer(pb, ptr1, width);
  117. ptr1 += picture->linesize[1];
  118. }
  119. for(i=0;i<height;i++) { /* Cr */
  120. put_buffer(pb, ptr2, width);
  121. ptr2 += picture->linesize[2];
  122. }
  123. }
  124. put_flush_packet(pb);
  125. return 0;
  126. }
  127. static int yuv4_write_header(AVFormatContext *s)
  128. {
  129. int* first_pkt = s->priv_data;
  130. if (s->nb_streams != 1)
  131. return AVERROR(EIO);
  132. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  133. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  134. }
  135. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  136. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  137. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  138. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  139. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  140. return AVERROR(EIO);
  141. }
  142. *first_pkt = 1;
  143. return 0;
  144. }
  145. AVOutputFormat yuv4mpegpipe_muxer = {
  146. "yuv4mpegpipe",
  147. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  148. "",
  149. "y4m",
  150. sizeof(int),
  151. CODEC_ID_NONE,
  152. CODEC_ID_RAWVIDEO,
  153. yuv4_write_header,
  154. yuv4_write_packet,
  155. .flags = AVFMT_RAWPICTURE,
  156. };
  157. #endif
  158. /* Header size increased to allow room for optional flags */
  159. #define MAX_YUV4_HEADER 80
  160. #define MAX_FRAME_HEADER 80
  161. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  162. {
  163. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  164. char *tokstart,*tokend,*header_end;
  165. int i;
  166. ByteIOContext *pb = s->pb;
  167. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  168. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  169. AVStream *st;
  170. struct frame_attributes *s1 = s->priv_data;
  171. for (i=0; i<MAX_YUV4_HEADER; i++) {
  172. header[i] = get_byte(pb);
  173. if (header[i] == '\n') {
  174. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  175. header[i+2] = 0;
  176. break;
  177. }
  178. }
  179. if (i == MAX_YUV4_HEADER) return -1;
  180. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  181. s1->interlaced_frame = 0;
  182. s1->top_field_first = 0;
  183. header_end = &header[i+1]; // Include space
  184. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  185. if (*tokstart==0x20) continue;
  186. switch (*tokstart++) {
  187. case 'W': // Width. Required.
  188. width = strtol(tokstart, &tokend, 10);
  189. tokstart=tokend;
  190. break;
  191. case 'H': // Height. Required.
  192. height = strtol(tokstart, &tokend, 10);
  193. tokstart=tokend;
  194. break;
  195. case 'C': // Color space
  196. if (strncmp("420jpeg",tokstart,7)==0)
  197. pix_fmt = PIX_FMT_YUV420P;
  198. else if (strncmp("420mpeg2",tokstart,8)==0)
  199. pix_fmt = PIX_FMT_YUV420P;
  200. else if (strncmp("420paldv", tokstart, 8)==0)
  201. pix_fmt = PIX_FMT_YUV420P;
  202. else if (strncmp("411", tokstart, 3)==0)
  203. pix_fmt = PIX_FMT_YUV411P;
  204. else if (strncmp("422", tokstart, 3)==0)
  205. pix_fmt = PIX_FMT_YUV422P;
  206. else if (strncmp("444alpha", tokstart, 8)==0) {
  207. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  208. return -1;
  209. } else if (strncmp("444", tokstart, 3)==0)
  210. pix_fmt = PIX_FMT_YUV444P;
  211. else if (strncmp("mono",tokstart, 4)==0) {
  212. pix_fmt = PIX_FMT_GRAY8;
  213. } else {
  214. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  215. return -1;
  216. }
  217. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  218. break;
  219. case 'I': // Interlace type
  220. switch (*tokstart++){
  221. case '?':
  222. break;
  223. case 'p':
  224. s1->interlaced_frame=0;
  225. break;
  226. case 't':
  227. s1->interlaced_frame=1;
  228. s1->top_field_first=1;
  229. break;
  230. case 'b':
  231. s1->interlaced_frame=1;
  232. s1->top_field_first=0;
  233. break;
  234. case 'm':
  235. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  236. return -1;
  237. default:
  238. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  239. return -1;
  240. }
  241. break;
  242. case 'F': // Frame rate
  243. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  244. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  245. break;
  246. case 'A': // Pixel aspect
  247. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  248. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  249. break;
  250. case 'X': // Vendor extensions
  251. if (strncmp("YSCSS=",tokstart,6)==0) {
  252. // Older nonstandard pixel format representation
  253. tokstart+=6;
  254. if (strncmp("420JPEG",tokstart,7)==0)
  255. alt_pix_fmt=PIX_FMT_YUV420P;
  256. else if (strncmp("420MPEG2",tokstart,8)==0)
  257. alt_pix_fmt=PIX_FMT_YUV420P;
  258. else if (strncmp("420PALDV",tokstart,8)==0)
  259. alt_pix_fmt=PIX_FMT_YUV420P;
  260. else if (strncmp("411",tokstart,3)==0)
  261. alt_pix_fmt=PIX_FMT_YUV411P;
  262. else if (strncmp("422",tokstart,3)==0)
  263. alt_pix_fmt=PIX_FMT_YUV422P;
  264. else if (strncmp("444",tokstart,3)==0)
  265. alt_pix_fmt=PIX_FMT_YUV444P;
  266. }
  267. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  268. break;
  269. }
  270. }
  271. if ((width == -1) || (height == -1)) {
  272. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  273. return -1;
  274. }
  275. if (pix_fmt == PIX_FMT_NONE) {
  276. if (alt_pix_fmt == PIX_FMT_NONE)
  277. pix_fmt = PIX_FMT_YUV420P;
  278. else
  279. pix_fmt = alt_pix_fmt;
  280. }
  281. if (raten == 0 && rated == 0) {
  282. // Frame rate unknown
  283. raten = 25;
  284. rated = 1;
  285. }
  286. if (aspectn == 0 && aspectd == 0) {
  287. // Pixel aspect unknown
  288. aspectd = 1;
  289. }
  290. st = av_new_stream(s, 0);
  291. if(!st)
  292. return AVERROR(ENOMEM);
  293. st->codec->width = width;
  294. st->codec->height = height;
  295. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  296. av_set_pts_info(st, 64, rated, raten);
  297. st->codec->pix_fmt = pix_fmt;
  298. st->codec->codec_type = CODEC_TYPE_VIDEO;
  299. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  300. st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  301. return 0;
  302. }
  303. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  304. {
  305. int i;
  306. char header[MAX_FRAME_HEADER+1];
  307. int packet_size, width, height;
  308. AVStream *st = s->streams[0];
  309. struct frame_attributes *s1 = s->priv_data;
  310. for (i=0; i<MAX_FRAME_HEADER; i++) {
  311. header[i] = get_byte(s->pb);
  312. if (header[i] == '\n') {
  313. header[i+1] = 0;
  314. break;
  315. }
  316. }
  317. if (i == MAX_FRAME_HEADER) return -1;
  318. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  319. width = st->codec->width;
  320. height = st->codec->height;
  321. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  322. if (packet_size < 0)
  323. return -1;
  324. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  325. return AVERROR(EIO);
  326. if (s->streams[0]->codec->coded_frame) {
  327. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  328. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  329. }
  330. pkt->stream_index = 0;
  331. return 0;
  332. }
  333. static int yuv4_probe(AVProbeData *pd)
  334. {
  335. /* check file header */
  336. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  337. return AVPROBE_SCORE_MAX;
  338. else
  339. return 0;
  340. }
  341. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  342. AVInputFormat yuv4mpegpipe_demuxer = {
  343. "yuv4mpegpipe",
  344. NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  345. sizeof(struct frame_attributes),
  346. yuv4_probe,
  347. yuv4_read_header,
  348. yuv4_read_packet,
  349. .extensions = "y4m"
  350. };
  351. #endif